Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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:@numbers = ('1','1','1','2','2','3','3','4','4','5','6'); @strings = ('hello','you','tree','people','fun','sun','grass','love',' +food','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 !helloyoutree peoplefun sungrass lovefood car rabbit
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 | |
by Anonymous Monk on Jul 29, 2004 at 10:18 UTC | |
by borisz (Canon) on Jul 29, 2004 at 11:00 UTC | |
|
Re: array matching problems
by deibyz (Hermit) on Jul 29, 2004 at 10:47 UTC | |
|
Re: array matching problems
by davido (Cardinal) on Jul 29, 2004 at 16:08 UTC | |
|
Re: array matching problems
by bgreenlee (Friar) on Jul 29, 2004 at 10:36 UTC | |
|
Re: array matching problems
by murugu (Curate) on Jul 30, 2004 at 05:37 UTC |