Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Joining Two Elements of an Array. Little Help Please.

by ishootperls (Novice)
on Sep 27, 2012 at 17:46 UTC ( [id://996056]=perlquestion: print w/replies, xml ) Need Help??

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

Monks, Here's the situation. I have an array split by periods (.). If the first element contains the word /literal/ join the 1st and 2nd elements together. Allowing the original array to contain the two conjoined elements in the first indice. Here's what I got, but no dice!

foreach my $results (@sorted) { + my @listOfItems = split( /\./, $results); if ( $listOfItems[0] =~ /literal/ ) { $listOfItems[0] = join(".", $listOfItems[0..1]); + } push ( my @array1, $listOfItems[0]); push ( my @array2, $listOfItems[1]); push ( my @array3, $listOfItems[2]); # @array1 should contain the newly conjoined $listOfItems[0] # My bad ! I had some typos in my sample code. It wasnt a # cut and paste from my terminal. So my mistake guys, sorry! # This is what I was actually working with. }
UPDATE: I took kennethk's sugestions and Im currently testing out some modifications. Thanks Monks!!

"An incendiary Perl will solve that problem" :-) - myself

Replies are listed 'Best First'.
Re: Joining Two Elements of an Array. Little Help Please.
by kennethk (Abbot) on Sep 27, 2012 at 18:20 UTC

    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.

    1. 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.
    2. 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.
    3. 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.
    4. 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.

      Got it ! Thank you so much for that valuable piece of information. Worked out just beautifully.

      "An incendiary Perl will solve that problem" :-) - myself
Re: Joining Two Elements of an Array. Little Help Please.
by toolic (Bishop) on Sep 27, 2012 at 18:07 UTC

    Use strict (Tip #1 from the Basic debugging checklist)

    Global symbol "@listofItems" requires explicit package name at

    @listOfItems is different from @listofItems (lower-case "o"). Try:

    push( my @array1, $listOfItems[0] ); push( my @array2, $listOfItems[1] ); push( my @array3, $listOfItems[2] );

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://996056]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2024-04-23 14:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found