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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.