in reply to Help needed please: Data Manipulation in 2D Array

If you want to locate a string quickly from a list of strings, use a hash.
# Assumes each cat1 appears only once in file2. my %cat2_by_cat1; while (<$fh2>) { chomp; my ($cat1, $cat2) = split /=/; $cat2_by_cat1{$cat1} = $cat2; } while (<$fh1>) { chomp; my ($dog, $bird, $cat1) = split /=/; exists( $cat2_by_cat1{$cat1} ) or die; my $cat2 = $cat2_by_cat1{$cat1}; print $fh_out (join('=', $dog, $bird, $cat1, $cat2), "\n"); }

Replies are listed 'Best First'.
Re^2: Help needed please: Data Manipulation in 2D Array
by Syntenty (Initiate) on May 05, 2008 at 06:00 UTC

    Hi, I'd like to thank you for your fast repsonse.

    In regards to your solution, I've refrained from using hashes because I have elements/keys that appear more than once and I need to include them all in my final output.

    For example, in file 2, the first three lines, would have the same key and different values. example:


    cat1=cat2
    cat1=cat9
    cat1=cat10
    FILE 1: dog1=bird2=cat1

    So what I would have to do, is find a way to create three lines from FILE 1, push these into a new 2D array, and then join the values to the keys, so my new array will be.
    index 0 - dog1=bird2=cat1=cat2
    index 1 - dog1=bird2=cat1=cat9
    index 2 - dog1=bird2=cat1=cat10

    The reason I cannot use a hash is because in this case it would only create the first line:
    index 0 - dog1=bird2=cat1=cat2
    since hashes cannot have identical keys for different values.

      No problem, just store more than one value per hash key. This is known as a HoA.
      my %cat2s_by_cat1; while (<$fh2>) { chomp; my ($cat1, $cat2) = split /=/; push @{ $cat2s_by_cat1{$cat1} }, $cat2; } while (<$fh1>) { chomp; my ($dog, $bird, $cat1) = split /=/; exists( $cat2s_by_cat1{$cat1} ) or die; for my $cat2 ( @{ $cat2s_by_cat1{$cat1} } ) { print $fh_out (join('=', $dog, $bird, $cat1, $cat2), "\n"); } }

        Thanks for your help. I was not aware of the multiple value per hash key. I forgot to mention in my original post that while file 2's data format will always have only 2 elements, FILE one will change.

        It will not always have the same number of elements, or three in this case. How can I change the second part of your code, concerning file handle 1 to account for this? And also I'm not sure how to use your method to output my final data to a file