prpheart has asked for the wisdom of the Perl Monks concerning the following question:
I am desperately thankful for any help since I am new to Perl and Linux.
I am trying to write a program to read a file called milcar, which has the following columns:
<last name> <firsr name> <integer1> <word1> <integer2> <word2> <real number>I would like to set the key as “last_name first_name” and the value as integer2. In case there is a duplicate name I must update the value with the larger “integer2”. I must read another file that has 2 columns <last name> <first name>. I must check each of the name pairs in the 2nd file against, I guess, the hash table. Any matches must be written to user specified outfile. I must also pull all entries out of the hash and sort by first name and use the second name as tie breaker. The sorted list with 3 columns <first> <last> <integer2> will be written to user specified outfile.
I have the program that I have written so far with comments, so I appreciate any tips/suggestions. My main confusion is reading a file into a hash and if I am defining the keys properly (as I am doing so in terms of a multidimensional array, which is probably wrong).
Thank you for any help. -prpheart
#!/user/bin/perl if ($#ARGV != 3) { print "At command line, enter 2 files. First is full t +ext and second is the name text only. Third and fourth are names of o +utfiles.\n"; die "usage: entermname.pl <command line arg 1> <command line arg 2> <c +ommand line arg 3> <command line 4>\n"; } $command_line_parameter1 = $ARGV[0]; # This is file with all categories/columns described earlier. $command_line_parameter2 = $ARGV[1]; # This is file with names to check against. $command_line_parameter3 = $ARGV[2]; # This is output file for matches. $command_line_parameter4 = $ARGV[3]; # This is output file with three columns. $milcar = @milliony; open(inf,"<$milcar"); while(chomp($line = <inf>)){ ... # stuck on what to type here } my %assigned_million = ( "$milliony[$i][0] $milliony[$i][1]" => "$milliony[$i][4]"); # Am I missing "and" between names in key? # Trying to define keys in terms of two-dimensional array $hash_value = $assigned_million{$key}; if(!$hash_value or $value > $hash_value){ $assigned_million{$key} = $value; # changing integer2 } $outfile_uno = @newnamecheck; my %newnam = ( "$newnamecheck[$i][0] $newnamecheck[$i][1]" => undef); # Am I missing "and" between names in key? open(inf,"<$milcar"); while(chomp($line = <inf>)){ .......... # stuck here with loop } if($assigned_million{$key} eq $newnamecheck{$key}){ open(FH,">>$outfile_dos") or die "Cannot open outfile $outfile_dos\n"; print FH "$assigned_million{$key}\n"; close(FH); } @sorted = sort{$a->[1] cmp $b->[1] || $a->[0] cmp $b->[0]} @milliony; open(FH, ">$outfile_dos" or die "Can't write to $outfile_dos.\n"; { print FH "$sorted[$i][0], $sorted[$i][1], $sorted[$i][4]"; } close(FH);
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Writing Program With Hashes and Loops For Files With Data in Columns
by arnaud99 (Beadle) on Mar 05, 2013 at 08:55 UTC | |
Re: Writing Program With Hashes and Loops For Files With Data in Columns
by ww (Archbishop) on Mar 05, 2013 at 15:19 UTC | |
by Anonymous Monk on Mar 05, 2013 at 16:32 UTC | |
by ww (Archbishop) on Mar 05, 2013 at 18:07 UTC |