Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

extract only smallest numbers

by Anonymous Monk
on Jul 21, 2007 at 20:14 UTC ( #628023=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow monks,
I have a file with some lines, like
jim 15 jim 522 jim 5 george 8 george 3 george 12 nick 27 nick 29
Is there a way of reading the file and printing each name with the smallest number next to it? That is,
jim 5 george 3 nick 27
Thank you in advance

Comment on extract only smallest numbers
Select or Download Code
Re: extract only smallest numbers
by grep (Monsignor) on Jul 21, 2007 at 20:48 UTC
    Is there a way of reading the file and printing each name with the smallest number next to it?
    Yes, absolutely!

    Though, it seems you forgot to post the code you've worked on. I won't assume you wanted someone else to write it for you, so I'll give you some places to look. Then when you have some code that you have written, and have a problem you can ask a question here. (right after you read this)

    • Go take a look at open about reading the file
    • Look at foreach loops to go over the lines in the file
    • Look at split to get the number and name
    • Read about Hashes(associative arrays) to store your results
    • perlop will tell you how to evaluate 'less than'
    • And finally reading print will show you how to print your results
      Thank you grep, but my problem is exactly on how to compare the numbers.
      Assuming, for example, that I have the code:
      open FILE, $my_file; while (<FILE>) { $_=~/(.*)\t(.*)/; $name=$1; $code=$2; $hash{$name} = $code; }
      That is as far as I can go...
        ... if (!exists $hash{$name}) { # unconditionally assign the first entry $hash{$name} = $code; } else { # assign further entries only if smaller... $hash{$name} = $code if $code < $hash{$name}; }
        OK.

        Couple of things first.

        • Use strict and warnings
        • Check to see if your open succeeds
        • Use split not a regex when you want to split
        • Indent your code - It makes reading the code easier on you and us
        So your code should look something like (untested):
        use strict; use warnings; open FILE, $my_file or die "Can't open $my_file\n"; my %hash; while (<FILE>) { my ($name, $code) = split(/\t/); }
        Now you have some of the problems fixed. Now you need to test what's in the hash and $code.

        One thing to remember is, that if you have not assigned a value to the hash for that key you'll get undef returned, which evaluates to 0 numerically. Since you are looking for the lowest number you'll want avoid the undef. So check the value from your hash with defined first. If you see that it is not defined, then you know to assign the value of $code whatever it is.

        # Still untested if ( !defined( $hash{$name} ) ) { $hash{$name} = $code; }
        If you have a value defined then check to see whats larger
        # Still untested if ( !defined( $hash{$name} ) or $code < $hash{$name} ) { $hash{$name} = $code; }
Re: extract only smallest numbers
by GrandFather (Cardinal) on Jul 21, 2007 at 21:35 UTC

    Yes there is - there are many ways in fact. To get started you need to take a look at open, close, perldata (hashes in particular), perlsyn (while loops) and perlop (the diamond operator: '<FILEHANDLE>'), then take a stroll through the Tutorials section here.

    When you've done all that and written your first attempt at the code, come back here with any problems you might have.


    DWIM is Perl's answer to Gödel
Re: extract only smallest numbers
by Anonymous Monk on Jul 21, 2007 at 21:43 UTC
    Thank you all,
    I am trying to continue using your valuable comments, but still no luck...
    I tried the snippets of code you provided, but I continue to receive
    jim 5 nick 29 george 12
    as a result...
Re: extract only smallest numbers
by Anonymous Monk on Jul 21, 2007 at 21:48 UTC
    Sorry, it worked fine eventually...
    Thank you all for your time and trouble!
Re: extract only smallest numbers
by graff (Chancellor) on Jul 22, 2007 at 00:37 UTC
    Now that you've solved the problem with a perl script, you might still want to know that the unix/cygwin "sort" command does this, when you use the appropriate option flags -- e.g.:
    sort -n +1 a_file > a_sorted_file
    If you only want the first few lines, try the "head" command:
    sort -n +1 a_file | head -5 > first_five_sorted

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://628023]
Approved by FunkyMonk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (10)
As of 2014-12-19 12:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (82 votes), past polls