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

I am simply concatenating strings together based on numbers in an array (see below). However, i'm struggling to adapt the code to take into consideration a third array (@check) which holds consecutive numbers with increments of 1.
e.g. my @numbers = ('1', '2', '2', '3', '3'); my @strings = ('hello', 'green', 'grass', 'rainbow', 'pretty'); my @new_strings;
So the output is:
hello greengrass rainbowpretty
Heres the code to do this:
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]"; } }
## 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! #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
@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

    my @numbers = (1, 2, 2, 3, 3); my @strings = qw(hello green grass rainbow pretty); my @check = (1, 2, 3, 4); my @new_strings; foreach (@numbers) { die "string $_ does not exists" unless defined $strings[$_-1]; $new_strings[$_-1] .= $strings[$_-1]; } foreach (@check) { $new_strings[$_-1] = 'xxxxxx' unless defined $new_strings[$_-1]; } print $_, "\n" for grep defined, @new_strings;

    Update: filtered output, indexes corrected

      hi ccn! thanks for your help, but i can't get this to work right. my outputm from it is:
      green grassgrass rainbowrainbow xxxxxx green grassgrass rainbowrainbow xxxxxx green grassgrass rainbowrainbow xxxxxx green grassgrass rainbowrainbow xxxxxx
      I need:
      hello greengrass rainbowpretty xxxxxxxx # where @numbers basically refers to which line the strings should be +printed to
Re: arrays and strings
by Gunth (Scribe) on Aug 31, 2004 at 15:25 UTC
    Probably broken, but something to work off of I suppose
    #!/usr/bin/perl use strict; use warnings; my @numbers = (1, 2, 2, 3, 3); my @strings = qw(hello green grass rainbow pretty); my @check = (1, 2, 3, 4); my @con; for (0 .. $#numbers) { push @{$con[$numbers[$_]]}, $strings[$_]; } for (@check) { if ($con[$_]) { print @{$con[$_]}; } else { print 'xxxxxxxxx'; } print "\n"; } __DATA__ hello greengrass rainbowpretty xxxxxxxxx
    -Will
Re: arrays and strings
by revdiablo (Prior) on Aug 31, 2004 at 17:12 UTC

    Here's how I'd approach it:

    my @numbers = qw(1 2 2 3 3); my @strings = qw(hello green grass rainbow pretty); my @check = qw(1 2 3 4); my %concat; for (0 .. $#numbers) { $concat{$numbers[$_]} .= $strings[$_]; } for (@check) { print $concat{$_} || "xxxxxxxx", "\n"; }