in reply to Which data structure should I use?

What I need to do is be able to read the data based on the SECOND_NUMBER.

Sounds like you want an array of records for each value SECOND_NUMBER can take.

my $header = <>; chomp($header); my @field_names = split /\t/, $header; my %grouped_by_num2; while (<>) { chomp; my %rec; @rec{ @field_names } = split /\t/; push @{ $grouped_by_num2{$rec{SECOND_NUMBER}} }, \%rec; } use Data::Dumper; print(Dumper(\%grouped_by_num2));

Replies are listed 'Best First'.
Re^2: Which data structure should I use?
by Anonymous Monk on Sep 06, 2009 at 23:43 UTC
    So, in the code you suggest, ID won't be used at all? I don't need to use ID after all, I just want to store the pairs SECOND_NUMBER<->FIRST_NUMBER, so It suits me better...
    However, how would you use the code you provide in order to get the pair 9<->6 in line 12? If I gave you $wanted_second_number=9, how would you print $first_number=6?

      So, in the code you suggest, ID won't be used at all

      It's there, just not used as the index.

      However, how would you use the code you provide

      The output of Data::Dumper illustrates that quite well.

      If I gave you $wanted_second_number=9, how would you print $first_number=6?

      No really, look at the structure yourself before continuing.

      my @matching_recs = $grouped_by_num2{$wanted_second_number}; for my $rec (@matching_recs) { my $first_number = $rec->{FIRST_NUMBER}; print("$first_numner\n"); # 5, 6 }