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

Hi,
I need some suggestions howto do this. I was thinking of using a perl map but not sure whether its required. Need some help.
My input file looks like this:-
253868 20 317 2628 253868 20 317 7294 256158 9 329 371 256158 9 329 86450 728481 1 353 1361 154664 7 440 449688 154664 7 440 563
My output should be like this:-
253868 20 317 7294 256158 9 329 86450 728481 1 353 1361 154664 7 440 449688
Want I want is that my key should be same but the value should be only the minimum and the maximum value.
Can you please suggest what will be the easiest way to get such an output.
Thanks
cowboy

Replies are listed 'Best First'.
Re: Minimum and Max values from same Key
by ELISHEVA (Prior) on Apr 28, 2009 at 04:09 UTC
    When you want an association between a key and a clump of data, a hash (Perl for Java maps) is just the thing. You can assign the keys of a hash a value consisting of any scalar or reference. If you only want to map a single value to each key (e.g. just the max) then a scalar will do. If you want to map multiple values to a key (e.g. both a min and a max) you will need to assign each hash key a reference to an array or hash. Which you choose is largely a matter of style, though hashes of hashes tend to be more maintainable if the kinds of data associated with a key are likely to expand or change over time.
    # hash of hashes $hRecords{$key}{min} = $minSoFar; $hRecords{$key}{max} = $maxSoFar; # hash of arrays $hRecords{$key}[0] = $minSoFar; $hRecords{$key}[1] = $maxSoFar; # or better (self documenting; less error prone) my $MIN_IDX=0; my $MAX_IDX=1; $hRecords{$key}[$MIN_IDX] = $minSoFar; $hRecords{$key}[$MAX_IDX] = $maxSoFar;

    For specifics about constructing and retrieving data from hashes of arrays and hashes of hashes, see perldsc

    Best, beth

Re: Minimum and Max values from same Key
by CountZero (Bishop) on Apr 28, 2009 at 06:29 UTC
    Have a look at this:
    use strict; use Data::Dump qw/dump/; my %data; while (<DATA>) { chomp; my ( $field1, $field2, $field3, $field4 ) = split '\s+'; $data{$field1}{$field2}{'min'} = $field3 if ( not defined $data{$field1}{$field2}{'min'} or $field3 < $data{$field1}{$field2}{'min'} ); $data{$field1}{$field2}{'max'} = $field4 if ( not defined $data{$field1}{$field2}{'max'} or $field4 > $data{$field1}{$field2}{'max'} ); } ## end while (<DATA>) print dump( \%data ); __DATA__ 253868 20 317 2628 253868 20 317 7294 256158 9 329 371 256158 9 329 86450 728481 1 353 1361 154664 7 440 449688 154664 7 440 563
    Output:
    { 154664 => { 7 => { max => 449688, min => 440 } }, 253868 => { 20 => { max => 7294, min => 317 } }, 256158 => { 9 => { max => 86450, min => 329 } }, 728481 => { 1 => { max => 1361, min => 353 } }, }
    Getting the data-structure into your output format is left as exercise for the reader.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James