Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Parsing a data file to find the highest value.

by misconfiguration (Sexton)
on Nov 30, 2007 at 18:32 UTC ( [id://654159]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

Long time reader first time poster.

I'm currently writing some code to better automate adding a print queue on HP-UX and AIX systems. These printers are addressed by a simple numeric identifier, each has to be unique. I'm wanting to have Perl read the text file and figure out which is the highest number, then store it in a Scalar for later use in the script.

Any input that points me in the right direction is greatly appreciated.
  • Comment on Parsing a data file to find the highest value.

Replies are listed 'Best First'.
Re: Parsing a data file to find the highest value.
by FunkyMonk (Chancellor) on Nov 30, 2007 at 18:55 UTC
    As moritz has said, without seeing the format of the input file it's impossible to give a definitive answer, but the following may help you get started

    update: Changed the regex to match the data provided by the OP.

    my $high_id = 0; open my $ID_FILE, "<", "path/to/file" or die $!; while ( <$ID_FILE> ) { my ( $id ) = m/:(\d+):/; # extract first number from line $high_id = $id if $id > $high_id; } print "highest id found = $high_id\n";

      Sorry for the lack of information guys, I'll show you the basic syntax of our printer config file.

      M08_amvpss09_MEDIP :279:lp -damvpss09 >amvpss09.trace 2>&1

      The number :279 is the identifier we need to locate and add by one.

      There are over 700 unique numbers in no particular order, Are you guessing that this will need to be sorted by columns?
Re: Parsing a data file to find the highest value.
by tcf03 (Deacon) on Nov 30, 2007 at 19:08 UTC
    Depending on the file format, perhaps something like this?
    #!/usr/bin/perl use strict; use warnings; my %prns; for (<DATA>) { my ( $num, $prn ) = split /\s/; $prns{$num} = $prn; } my @ord = sort { $b <=> $a } keys %prns; print "Highest numbered printer is $prns{shift @ord}\n"; __DATA__ 1 prt1 2 prt2 99 prt99 10 prt10 100 prt100 7 prt7
    update
    use strict; use warnings; my %prns; for (<DATA>) { my ( $printer, $num, $cmd ) = split /:/; $prns{$num} = $printer; } my @ord = sort { $b <=> $a } keys %prns; print "Highest numbered printer is $prns{shift @ord}\n"; __DATA__ M08_amvpss09_MEDIP :275:lp -damvpss09 M07_amvpss09_MEDIP :279:lp -damvpss09 M09_amvpss09_MEDIP :278:lp -damvpss09 M01_amvpss09_MEDIP :2:lp -damvpss09 M04_amvpss09_MEDIP :1000:lp -damvpss09
    Ted
    --
    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
      --Ralph Waldo Emerson
      Why would you do a full sort just to find the maximum value?
        Its just what I though of first. You could also do:
        use strict; use warnings; use List::Util qw(max); my %prns; while (<DATA>) { my ( $printer, $num, $cmd ) = split /:/; $prns{$num} = $printer; } print "Highest numbered printer is $prns{max (keys %prns) }\n"; __DATA__ M08_amvpss09_MEDIP :275:lp -damvpss09 M07_amvpss09_MEDIP :279:lp -damvpss09 M09_amvpss09_MEDIP :278:lp -damvpss09 M01_amvpss09_MEDIP :2:lp -damvpss09 M04_amvpss09_MEDIP :1000:lp -damvpss09
        Something line FunkyMonk's suggestion would work as well.
        Ted
        --
        "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
          --Ralph Waldo Emerson
Re: Parsing a data file to find the highest value.
by moritz (Cardinal) on Nov 30, 2007 at 18:37 UTC
    The first step is to parse these files, and we can't help you with that unless we know what they look like.

    So the first step is to provide a sample config file here ;)

Re: Parsing a data file to find the highest value.
by sh1tn (Priest) on Dec 01, 2007 at 00:39 UTC
    perl -ne '$"<($_=(split(/:/, $_))[1]) and $"=$_;eof&&print$"' file.con +f


      Its a great command line usage. But it does NOT work if input data is negative numbers. Can you throw some light? -Thanks

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-20 00:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found