When posting code, a complete description including sample input/output is helpful. See How do I post a question effectively?. Independent of that, I can identify a few problems with your posted code snippet.
- You don't have a closing curly on your foreach loop. I assume this is supposed to be trailing, which is important for the scoping issues I'm about to raise.
- You stash the results of your split in @listOfItems, but your pushes use @listofItems. Perl is case sensitive, so these are different variables. This would have been caught using strict -- see Use strict warnings and diagnostics or die.
- By declaring @array1, @array2 and @array3 with my within your foreach loop, you are creating variables which are scoped to that loop and thus wiped every iteration. As a result, you can't accumulate information in them. This would have been caught by strict when you tried to access the arrays outside the loop scope.
- You mean to access an array slice, but proper syntax there would be @listOfItems[0..1], not $listOfItems[0..1]. The leading $ sigil tells Perl you want a single scalar, and so .. is interpreted as the flip-flop operator -- see Range Operators for documentation. If you had warnings on, it would have complained. My guess is that this is the bug in your actual source code, since it manifests as looking like your join failed.
So taking all this into account, a working version of your code might look like:
use strict;
use warnings;
use Data::Dumper;
my @sorted = qw(1.2.3 a.b.c literal.b.c);
my @array1;
my @array2;
my @array3;
foreach my $results (@sorted) {
+
my @listOfItems = split( /\./, $results);
if ( $listOfItems[0] =~ /literal/ ) {
$listOfItems[0] = join(".", @listOfItems[0..1]);
+
}
push (@array1, $listOfItems[0]);
push (@array2, $listOfItems[1]);
push (@array3, $listOfItems[2]);
# @array1 should contain the newly conjoined $listOfItems[0]
}
print Dumper \@array1;
You may find a read through of Basic debugging checklist helpful -- it's quite useful for demonstrating how to track all of the above issues down.
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
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.