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);
In reply to array matching problems by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |