Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

asking for suggestions about hash files

by Anonymous Monk
on Jun 29, 2009 at 19:39 UTC ( #775781=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks I have 6 files in which there is two columns per line. I want to compare the first column and each file filed by field, and get the maximum one, retrieve the second column value corresponding to the max value. for example if I have in my 6 files like this:
file1: 2 3 file2: 4 2 file3: -1 2 file4: -4 4 file5: 0 -1 file6: 0 -1
I want to retrive 2, since 2 is the value corresponding to 4 which is the max value of the first column and each file. Any idea what is the best way to do it?

Replies are listed 'Best First'.
Re: asking for suggestions about hash files
by jrsimmon (Hermit) on Jun 29, 2009 at 20:00 UTC
    This will get you started. If you want more help, you'll need to put more effort into your question:
    declare or otherwise obtain list of files declare scalar for max (a, b) equal to undef for every file{ open the file for reach line{ split the line into scalars (current a, current b) if current a > max a then set max b = current b } close the file } you now have b from the maximum seen value of a

    I'm not sure what the post has to do with hashes or hash files (I'm not even sure what is meant by a hash file), so my response didn't address that.
Re: asking for suggestions about hash files
by duff (Parson) on Jun 29, 2009 at 20:43 UTC
    In the spirit of timtowtdi ... sort -rn file* | head -1 | awk '{ print $2 }' :-)
      Involving Perl,
      sort -rn file* | head -1 | perl -pale'$_=$F[1]'
Re: asking for suggestions about hash files
by psini (Deacon) on Jun 29, 2009 at 20:02 UTC

    Read all files, storing the values in a single hash, then sort-reverse the hash keys and take the first.

    Your question does not consider the case in which more than one file has the highest value in the first column. If you want in the result all the values corresponding to the highest key, make that a HoA.

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://775781]
Approved by ELISHEVA
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2022-09-25 14:58 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (116 votes). Check out past polls.