in reply to @array1 vs @array2

To be honest with you, I would be reaching for some hashes at this point. Given the relationship you have described, I would create a hash that looks kinda like this:
# Sorry about the MANY keyword - I would choose a better name # for the secondary data, but I have no clue what would make # sense. my %keyhash = ( $id => { NAME => $name, REF => $ref, MANY => [] } );
To populate it, I would do something like this:
for ( @array1 ) { my ($id,$name,$ref) = split /,/; # You may wish to add some error checking to make sure the # hash key $id does not already exist $keyhash{$id} = { NAME => $name, REF => $ref, MANY => [], } } for ( @array2 ) { my ($id,$name,$ref) = split /,/; # Warn and do nothing if a record is found for which the # $id is not already in %keyhash unless ( defined( $keyhash{$id} ) ) { warn "No such record $id!\n"; next; } push @{$keyhash{$id}{MANY}}, [ $name, $ref ]; }
Although you may want MANY to be a hash - it really depends on how you want to use your data later.

Finally, to extract the number of records for each ID,

# A little something to get the plurality correct for ( keys %keyhash ) { my $num = @{$keyhash{$_}{MANY}}; printf "%s appeared %d %s\n", $_, $num, $num > 1 ? "times" : "time"; }

I will say my choice of data structures ( hashes instead of arrays ) is really dependant on how you intend using the data later. Hashes, for me, seem to better reflect the relationship between tables better than arrays. YMMV, of course.

Updated 14:04 It was pointed out I had dropped an equals sign. Sigh.

mikfire

Replies are listed 'Best First'.
Re: Re: @array1 vs @array2
by Anonymous Monk on Mar 19, 2001 at 23:59 UTC
    Keep in mind that I'm new to hashes. I'm getting this error when running the above code.

    syntax error at ./file.pl line 15, near "%keyhash ("

    Any suggestions?

    Thanks for all the help, I really appreciate it.
Re: Re: @array1 vs @array2
by Anonymous Monk on Mar 20, 2001 at 00:26 UTC
    You rock!!!! Finally we've done it. Many many thanks to you and all who posted on this topic. I'm sure I'll be asking more questions. :)
Re: Re: @array1 vs @array2
by Anonymous Monk on Mar 20, 2001 at 00:53 UTC
    Using the has above, how would I print $name instead of $id. What exactly does the "MANY" keyword refer to? Many thanks for helping.
      I am storing the contents of the second file under the MANY key in the hash. Explore perldoc perldsc for further understanding of the structure I built. It isn't a proper keyword, but, well, it was a word I used as key.

      You can print the name out by changing the $_ in the printf to $keyhash{$_}{NAME}

      mikfire