Perl help?

Discussion in 'OT Technology' started by trouphaz, May 7, 2008.

  1. trouphaz

    trouphaz New Member

    Joined:
    Sep 22, 2003
    Messages:
    2,666
    Likes Received:
    0
    Ok, so I'm trying to write something in shell and it is just pissing me off. I know it'll be much easier in perl, but I don't know perl for shit.

    I have the output of a script that is giving me a list of over 5500 lines, each line is composed of 2 numbers each with one digit after the decimal. So, it is in the format like this:
    982.7 657.0
    1263.3 551.8
    24.7 54.7


    I want to sort this list by the total of these two. I was trying to use something like the following:
    Code:
    # cat file | while read X Y
    do
       SUM=`expr $X + $Y`
       echo $SUM
    done
    I tried replacing expr with $(( $X + $Y )) which I think is the same. Either way, it is not happy about adding non-integers. I also tried to just drop the . and figured I'd just remember to divide the final numbers by 10 in my head, but it bitched about that as well.

    Can anyone perlish help a brother out?
     
  2. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    no perl expert and theres probably an easier way of doing this with regular expressions but this is literally the 3rd script ive wrote LOL

    Code:
    my ($sum, @array, $line, @line_vals);
    
    $log_file_name = $ARGV[0];
    print "Log file name = $ARGV[0]\n";
    
    
    open(MYINPUTFILE, $log_file_name);    
    
    @array = <MYINPUTFILE>;
    
    foreach $line (@array)
    {
      chomp($line);
      @line_vals = split(/ /, $line);
      $sum = $line_vals[0] + $line_vals[1];
      print "$sum\n";
    }
    
    
    
    close(MYINPUTFILE);
    
     
  3. crontab

    crontab (uid = 0)

    Joined:
    Nov 14, 2000
    Messages:
    23,458
    Likes Received:
    12
    I don't know perl, but here's a one-liner with awk:

    cat file | awk '{sum=$1 + $2} {print sum}'
     
  4. trouphaz

    trouphaz New Member

    Joined:
    Sep 22, 2003
    Messages:
    2,666
    Likes Received:
    0
    Wirelessly posted via wap.offtopic.com (Mozilla/2.0 (compatible; MSIE 3.02; Windows CE; PPC; 240x320) BlackBerry8703e/4.1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/105)

    Ooh, I didn't think of awk. I'll try that too. I also want to test lolzilla's perl script to mess around with perl some. Thanks guys.
     
  5. trouphaz

    trouphaz New Member

    Joined:
    Sep 22, 2003
    Messages:
    2,666
    Likes Received:
    0
    nice, both of those worked. thanks for the help guys.
     
  6. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    np brah
     
  7. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Wait, but you guys didn't sort?
     
  8. trouphaz

    trouphaz New Member

    Joined:
    Sep 22, 2003
    Messages:
    2,666
    Likes Received:
    0
    i just piped the output into sort -n. :)
     
  9. Mike99TA

    Mike99TA I don't have anything clever to put here right now

    Joined:
    Oct 3, 2001
    Messages:
    4,553
    Likes Received:
    0
    Location:
    Greenville, SC
    Had you wanted to use perl in a short way, you could have done:

    Code:
    #!/usr/bin/perl
    
    while (<STDIN>) {
        push @sums, (split(/\s+/))[0] + (split(/\s+/))[1];
    }
    
    print join("\n", sort { $a <=> $b } @sums) . "\n";
    
    And then

    cat file | ./perlscript

    Or if you wanted to be really tricky:

    Code:
    cat file | perl -e 'while (<STDIN>) { push @sums, (split(/\s+/, $_))[0] + (split(/\s+/, $_))[1]; } print join("\n", sort { $a <=> $b } @sums) . "\n";'
    
    However I'm not an advocate for running perl scripts without -w and use strict, especially for beginners, but for quick and dirty it would get the job done.
     
  10. crontab

    crontab (uid = 0)

    Joined:
    Nov 14, 2000
    Messages:
    23,458
    Likes Received:
    12
    what does sorting have to do with this
     
  11. Mike99TA

    Mike99TA I don't have anything clever to put here right now

    Joined:
    Oct 3, 2001
    Messages:
    4,553
    Likes Received:
    0
    Location:
    Greenville, SC
    he asked to sort the output by the sum of the two numbers, thats why it was brought up.
     
  12. crontab

    crontab (uid = 0)

    Joined:
    Nov 14, 2000
    Messages:
    23,458
    Likes Received:
    12
    i didn't see the sort part in his question
     
  13. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    oh yeah sort LOL!!! i have nowhere near the unixification of some of the bros in this thread.
     
  14. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Noone has yet answered his question, actually. The requirement was: sort by the sum. That doesn't mean OUTPUT the sum. It means sort by it and then output the original values.

    I would do it, but I'm feeling too lazy. I prefer to manage anyway :big grin:
     
  15. Mike99TA

    Mike99TA I don't have anything clever to put here right now

    Joined:
    Oct 3, 2001
    Messages:
    4,553
    Likes Received:
    0
    Location:
    Greenville, SC
    Actually thats not what he asked based on the snippet of code he included with the original question (he obviously wanted the sum to be printed).
     
  16. trouphaz

    trouphaz New Member

    Joined:
    Sep 22, 2003
    Messages:
    2,666
    Likes Received:
    0
    well, ultimately i wanted to sort, but what i was stuck on was adding the numbers together.
     
  17. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    But his code wasn't working was it?

    He said: "I want to sort this list by the total of these two."

    That means he wants to retain the list, just sort it. Not totally redo the list.
     
  18. Mike99TA

    Mike99TA I don't have anything clever to put here right now

    Joined:
    Oct 3, 2001
    Messages:
    4,553
    Likes Received:
    0
    Location:
    Greenville, SC
    Forgot to update with a fix to the intial code in the OP that would have made it work:

    Code:
    # cat testfile | while read X Y
    do
        echo "$X + $Y" | bc
    done
    
    Note: this is pretty slow as calling bc for every line is slowtastic - the perl or awk way is much better.

    Also if you wanted to do it with bash strictly, the correct way to add variables would be:

    let SUM=($X+$Y)
    (Note: no spaces - this only works with integers)
     
  19. trouphaz

    trouphaz New Member

    Joined:
    Sep 22, 2003
    Messages:
    2,666
    Likes Received:
    0
    lol, listen... you can continue to argue if you want since i have my answer, but i got what i needed.
     
  20. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Did you? Your list still isn't ordered.
     
  21. trouphaz

    trouphaz New Member

    Joined:
    Sep 22, 2003
    Messages:
    2,666
    Likes Received:
    0
    well... it is. all i needed was help adding up non-integer numbers really. whether using perl or awk, i can dump the output into sort -n (which i mentioned a few posts back). :)
     
  22. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    sort -n > newlist.txt
     
  23. Mike99TA

    Mike99TA I don't have anything clever to put here right now

    Joined:
    Oct 3, 2001
    Messages:
    4,553
    Likes Received:
    0
    Location:
    Greenville, SC
    Not arguing - just giving you multiple ways to accomplish what you want for perusal purposes and as you said you didn't know perl well and would like to see examples.
     
  24. trouphaz

    trouphaz New Member

    Joined:
    Sep 22, 2003
    Messages:
    2,666
    Likes Received:
    0
    i just meant arguing about whether or not i asked for it to be sorted. i certainly appreciate the various examples because i'd like to get more involved with perl. in the past i've worked with 2 really great perl scripters who ended up rewriting my 50 line shell script with 15 characters, but i couldn't even begin to decrypt their use of regular expressions and stuff. :)
     
  25. Mike99TA

    Mike99TA I don't have anything clever to put here right now

    Joined:
    Oct 3, 2001
    Messages:
    4,553
    Likes Received:
    0
    Location:
    Greenville, SC
    Regular expressions are the reason I got into perl in the first place. As nerdy as this sounds, a really complex regular expression makes me giddy :big grin:
     

Share This Page