Find the answer to your Linux question:
Page 2 of 2 FirstFirst 1 2
Results 11 to 17 of 17
Like Tree1Likes
i don't understand why embedding my script into yours would make it run any faster. can you explain more thoroughly exactly what it is your script is supposed to do, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #11
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353

    i don't understand why embedding my script into yours would make it run any faster. can you explain more thoroughly exactly what it is your script is supposed to do, or show more of your code that would explain it?

    i don't understand your second question either. can you use the output from my version of the script to explain it better?

    Code:
    3 5451||1511 
    3 8000||1506 
    1 5900||1503 2 5900||1502

  2. #12
    Just Joined!
    Join Date
    Apr 2012
    Posts
    56
    Hi,
    Thanks for your reply to my message. I would like to embed your script in my in order to run only one script. In that way it will be faster.
    Here is an explanation on how my script works. I have two loops (loop in a loop) in order to compare every line of array1 with every line of array2. The result of this comparison is var1 and var2. Without shorting, the result will look like:
    Code:
    var1 var2   var1 var2   var1 var2   var1 var2   var1 var2  Ö.
    1 1     1 2      0 1       0 4
    0 2     1 1      2  4      0 3
    0 0     4 4      1 1       4 5
    0 0     0 1      00        1 1
    I want to get only the max var1(first short) with its var2 (second short) and the number of line. In case that there are two or more equal results I want all of them.

    Code:
    2   1 2
    3   2 4
    4   4 5   2  4 4 (first 4 5 and then 4 4)
    2   0 1
    Here is the code

    Code:
    for my $i (0 .. $#array1) {
    for my $j (0 .. $#array2) {
    
    ....
    
    my $var1 = ......
    my $var2 = ......
    
    print "$var1 $var2\t"
    
    }
    Print \n"
    }
    So it is the same logic with your script but I want to merge them. Thanks again for your help.

  3. #13
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    I can't understand what you want. can't you show your actual data instead of pseudo-code? it is very confusing. what is in @array1 and @array2 and how do they get populated?

    what do you mean by "comparing"?

    the perl code you posted is essentially empty loops.

  4. #14
    Just Joined!
    Join Date
    Apr 2012
    Posts
    56
    hi,
    Thanks for your email. It is a bit complicated that's why I can explain it. Ok, so is it possble to modify the second script that you wrote and do it to shor also the second number (after ||)? The ideal should be:

    Code:
    3 5451||1511 
    3 8000||1506 
    1 5900||1503 2 5900||1502
    Unfortunately, the result of your script is:

    Code:
    3 5451||1511 
    3 8000||1506 
    1 5900||1502 2 5900||1503
    given a txt file:

    Code:
    9000||1520      1212||1515      5451||1511      1111||1510      1000||1509
    5600||1508      9000||1507      8000||1506      3600||1505      5500||1504
    5900||1502      5900||1503      9000||1501      3600||1500      2500||1499
    Thanks in advance.

  5. #15
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    okay, the thing to do here would be, instead of just printing the values we have on a given line, save them to name=value pairs in a hash, using the 2nd number in the pair as the hash key. then after all, columns of data have been saved to the hash, iterate over the keys of the hash in reverse (hi-to-low) numerical order:

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    my $file = shift || die "Give me the file\n";
    open(FH,'<',$file) or die "can't read '$file': $!\n";
    while(<FH>){
      chomp;
      my @nums = split(' ',$_);
    
      my $big;
      for(my $n = 0;$n<=$#nums;$n++){
        next if($n+1 == $.);
        my($val,$foo) = split('\|\|',$nums[$n]);
    
        if(defined($big)){
          $big = $val if($val > $big);
        }else{
          $big = $val;
        }
      }
    
      my %hash;
      my $col;
      for(my $n = 0;$n<=$#nums;$n++){
        next if($n+1 == $.);
        my($val,$foo) = split('\|\|',$nums[$n]);
        if($val == $big){
          $col = $n+1;
          $hash{$foo} = $val;
    #      print $n+1,' ',$val,'||',$foo,' ';
        }
      }
      for my $key(reverse sort keys %hash){
        print $col,' ',$hash{$key},'||',$key,' ';
      }
      print "\n";
    }
    close(FH);
    changes in bold.

  6. #16
    Just Joined!
    Join Date
    Apr 2012
    Posts
    56
    hi,
    thank you for your reply and I apologies for my late reply. I run it, but in case that there are more than one results in the same line I get:

    Code:
    3 5451||1511 
    3 8000||1506 
    5 5900||1503 5 5900||1502 5 5900||1000 5 5900||900
    How can I fix it?
    Thank you in advance.

  7. #17
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    okay, in that case, just print the column ($col) for the first name=value pair in each hash. change the last loop to look like this:

    Code:
      my $i = 0;
      for my $key(reverse sort keys %hash){
    #    print $col,' ',$hash{$key},'||',$key,' ';
        print $col,' ' unless($i);
        print $hash{$key},'||',$key,' ';
        $i+=1;
      }
      print "\n";

Page 2 of 2 FirstFirst 1 2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •