Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have an array of numbers and an array of strings:
@numbers = ('1','1','1','2','2','3','3','4','4','5','6'); @strings = ('hello','you','tree','people','fun','sun','grass','love',' +food','car','rabbit');
Both arrays have the same number of elements in them. I basically want to concatenate the strings together if the numbers in @numbers are the same. e.g. if $number$i is 1, concatenate all of the correponding elements from @strings together: So would end up with the following new strings:
helloyoutree peoplefun sungrass lovefood car rabbit
My code fails for the last consecutive same number in a row! Can anyone see what i'm doing wrong? Many many thanks !
for (my $i=0; $i< @numbers; $i++) { if (($numbers[$i] == $numbers[$i+1]) || ($numbers[$i] == $numbers[$ +i-1])) { + + push @new_array, "$strings[$i]"; } + + else { push @new_array, ">"; push @new_array, $strings[$i]; push @new_array, ">"; } } my $j = join ('', @new_array); $j =~ s/\s+//; @new_array = split (/>+/, $j);

Replies are listed 'Best First'.
Re: array matching problems
by borisz (Canon) on Jul 29, 2004 at 10:08 UTC
    for ( 0 .. $#numbers ) { push @{$h{$numbers[$_]}}, $strings[$_]; } for ( sort { $a <=> $b } keys %h ) { print join('', @{$h{$_}}), "\n"; }
    Boris
      Hi borisz, mant thanks for you response. The code works well but I don't understand it! How can i get it to print each new string to a new array (where each string can be accessed by element number)? AM
        the first part build a hash of arrays with the numbers as the key.
        %h = ( '1' => [ 'hello', 'you', 'tree' ], '3' => [ 'sun', 'grass' ], '2' => [ 'people', 'fun' ], ... );
        The second for loop grap the array and join the words. To grap a entry use
        $num = 1; $string = join '', @{$h{$num}}; # string is now helloyoutree $num = 4; # select the string with number 4 ( lovefood ) $string = join '', @{$h{$num}};
        Boris
Re: array matching problems
by deibyz (Hermit) on Jul 29, 2004 at 10:47 UTC
    Why don't you use a hash to concatenate, with numbers as keys? Something like:

    use strict; use warnings; use Data::Dumper; my @numbers = ('1','1','1','2','2','3','3','4','4','5','6'); my @strings = ('hello','you','tree','people','fun', 'sun','grass','love','food','car','rabbit'); my %hash; for (0..$#numbers){ $hash{$numbers[$_]} .= $strings[$_]; } print Dumper(\%hash); __OUTPUT__ $VAR1 = { '6' => 'rabbit', '4' => 'lovefood', '1' => 'helloyoutree', '3' => 'sungrass', '2' => 'peoplefun', '5' => 'car' };

    You can also use an array instead of a hash.
    Regards,

    deibyz

Re: array matching problems
by davido (Cardinal) on Jul 29, 2004 at 16:08 UTC
    There is more than one way to do it...

    use strict; use warnings; my @numbers = ('1','1','1','2','2','3','3','4','4','5','6'); my @strings = ('hello','you','tree','people','fun','sun', 'grass','love','food','car','rabbit'); my %cats; # This solution will gobble @strings. foreach ( @numbers ) { $cats{$_} .= shift( @strings ); } print "$cats{$_}\n" foreach sort keys %cats;

    Dave

Re: array matching problems
by bgreenlee (Friar) on Jul 29, 2004 at 10:36 UTC

    The problem with your code is this:

    if (($numbers[$i] == $numbers[$i+1]) ...

    When you reach the last element in your list that will be false, because there is no next element. And since the previous element doesn't match, it fails.

    Without solving it for you (since I'm guessing this is homework), one way to fix it would be to add another 'or' clause that checks to see if it is the last element in the array.

    Brad

Re: array matching problems
by murugu (Curate) on Jul 30, 2004 at 05:37 UTC

    Here is my code,

    @numbers = ('1','1','1','2','2','3','3','4','4','5','6'); @strings = ('hello','you','tree','people','fun','sun','grass','love',' +food','car','rabbit'); for (0..$#numbers){ $hash{$numbers[$_]}.=$strings[$_] }; print "$hash{$_}\n" for sort {$a<=>$b} keys (%hash) ;