Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
So the output is:e.g. my @numbers = ('1', '2', '2', '3', '3'); my @strings = ('hello', 'green', 'grass', 'rainbow', 'pretty'); my @new_strings;
Heres the code to do this:hello greengrass rainbowpretty
## PROBLEM ! ==================== If @check contains a number not present in @numbers, I want 'xxxxxxxx' to be inserted into the output in its place. I hope someone can help! #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\for ( 0 .. $#numbers ) { push @{$h{$numbers[$_]}}, $strings[$_]; } for ( sort { $a <=> $b } keys %h ) { push @new_strings, join('', @{$h{$_}}), ">>\n"; } + my $v = join ('', @new_strings); @new_strings = split (/>>/, $v); + + my @final_strings; + + for (my $k=0; $k<@new_strings; $k++) { $new_strings[$k] =~ s/\n+//g; $new_strings[$k] =~ s/\s+//g; if ($new_strings[$k] =~ /\w+/) { push @final_strings, "$new_strings[$k]"; } }
@check = ('1','2','3','4'); # desired output: #----------------- hello greengrass rainbowpretty xxxxxxxx # THE 4TH ELEMENT NOW CONTAINS 'xxxxxxxx' as there isn' +t a '4' in @numbers
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: arrays and strings
by ccn (Vicar) on Aug 31, 2004 at 14:06 UTC | |
by Anonymous Monk on Aug 31, 2004 at 14:20 UTC | |
|
Re: arrays and strings
by Gunth (Scribe) on Aug 31, 2004 at 15:25 UTC | |
|
Re: arrays and strings
by revdiablo (Prior) on Aug 31, 2004 at 17:12 UTC |