Hi Joe,

Probably the easiest is to store the second and third values as an array
or hash ref. Here's a demo script that shows what I mean (remove the line
numbers on the left before running it; they are there only to facilitate

#!/usr/local/bin/perl -w

1 my %records = ();
2 open IN, "some_file" or die "Oops! $!\n";
3 while (<IN>) {
4 my @fields = split /\t/;
5 $records{ shift @fields } = [ @fields ];
6 }
7 close IN;

8 while (my($key, $ra_value) = each %records) {
9 print "key, values: $key => $ra_value->[0], $ra_value->[1]\n";
10 }

Here's what the interesting lines do:

3: Read a line from the file, magically assign it to $_.

4: Split defaults to $_ if you don't specify a variable.

5: Shift removes the first argument from the specified array (or @_ if
none specified) and returns it. That means that, once you evaluate the
left hand side of this line, @fields contains only two values. On the
right hand side, we are using square brackets to construct a reference to
an anonymous array (cf 'perldoc perlref') and initializing it with the
values in @fields.

8: 'each' iterates through a hash and returns one key/value pair after
another. The idiom I use here is common and very convenient.

9: Notice that, since $ra_value is a reference to an array (hence the
prefix), you need to dereference its fields with a '->'.


I read in a file and process three fields per record. For speed I wish to store them in hash arrays.
Field 1 is the key field, the one I search for. When I get the reply, I want the related data
from the other hash arrays, i.e. the three fields are related. What is best way to store
that data?



Search Discussions

Discussion Posts


Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 2 | next ›
Discussion Overview
groupbeginners @
postedOct 10, '01 at 6:18p
activeOct 13, '01 at 2:22a



site design / logo © 2021 Grokbase