Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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);

In reply to Writing Program With Hashes and Loops For Files With Data in Columns by prpheart

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found