Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Nested Loop Problems

by cuautemoc (Initiate)
on May 02, 2012 at 19:30 UTC ( [id://968521]=perlquestion: print w/replies, xml ) Need Help??

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

I have a question about a nested loop I'm having trouble with. I'm reading in a list in the following format:


name1,a1,b1,c1,\n,name2,a2,b2,c2\n...etc.

What I'm trying to do is use a nested foreach loop to peel off the first four elements, then parse those four elements into four new arrays. Here is my code:

foreach(@array1) { $list=split(/,/,@array1,4); foreach($list) { (@w,@x,@y,@z)=split(/ /,$list,1);} }
I get the error "Use of implicit split to @_ is deprecated at filename.pl." I thought I was assigning the first split to $list, not $_. Can someone point out where I'm going wrong? What I'm hoping to get is pull each name(x) from the array, and store to it's own array, each a(x) to it's own array, and so forth.

Replies are listed 'Best First'.
Re: Nested Loop Problems
by brx (Pilgrim) on May 02, 2012 at 20:11 UTC

    Take care to context (list or scalar) when you use split.

    #!perl my @lines=<DATA>; for my $line (@lines) { chomp $line; my @arr = split/,/,$line; print "$arr[0]: $arr[1], $arr[2] and $arr[3]\n"; } __DATA__ name1,a1,b1,c1 name2,a2,b2,c2

      Awesome, that worked. I'm still confused as to context; was I treating it as a scalar? I looked through help file for split, but I guess I don't understand the difference.

      Anyway, I've got it working. Thanks for the advice!

        About variables and context, see pelrdata

        $list=split ...: 'split' in scalar context because '$list' is a scalar value (split: In scalar context, returns the number of fields found.)

        @list=split ...: 'split' in list context and returns a list because '@list' expects a list value.

Re: Nested Loop Problems
by ww (Archbishop) on May 02, 2012 at 20:46 UTC

    See split. Your message (a warning, not an error, IIRC) reflects split's dis-inclination to let you split to a scalar ($list). You'll also want to read chomp for how to get those newlines out of your way.

    Suggest you start with something like the following, which (sorta') follows your scheme:

    my @list; for my $item(@array1) { # assumes you are reading the list into an arr +ay and also # assumes that the command between c1 and the +newline is spurious push @list, $item; }

    Then write code to process @list, pushing its elements onto @w,@x,@y,@z.

    Use the docs. As a guru once remarked, "You can't just make stuff up and expect the computer to understand."

    Update: typos fixed (in just 3 tries); clarification about the "error" in the 1st para.

Re: Nested Loop Problems
by furry_marmot (Pilgrim) on May 02, 2012 at 21:10 UTC

    I tried to write some code to help you out, but I'm not at all sure what you're doing. First, split works on strings, not arrays. It also returns a list. So...

    split(/,/,@array1,4);
    attempts to split an array, which is never going to happen. Then...
    $list=split(/,/,@array1,4);
    attempts to assign the list that you wanted to appear to $list, which is a scalar. In scalar context, split returns the number of elements that resulted from the split, not the elements themselves. And foreach also works on lists, not scalars.

    Regarding your data, is it lines of data with embedded newlines? As newlines, wouldn't they be separate lines already? Anyway, maybe this will help:

    foreach my $line (<DATA>) { #assuming you have a data source chomp $line; @lines = split /\\n/, $line, 4; foreach $l (@lines) { # <--that's an L, not a one $l =~ s/^,//; @elems = split /,/, $l; print "$_ " for @elems; print "\n"; } } __DATA__ name1,a1,b1,c1,\n,name2,a2,b2,c2\nname3,a3,b3,c3,\n,name4,a4,b4,c4 name5,a5,b5,c5,\n,name6,a6,b6,c6\nname7,a7,b7,c7,\n,name8,a8,b8,c8
    This produces:
    name1 a1 b1 c1 name2 a2 b2 c2 name3 a3 b3 c3 name4 a4 b4 c4 name5 a5 b5 c5 name6 a6 b6 c6 name7 a7 b7 c7 name8 a8 b8 c8
    --marmot

    Further reading:

    • perldoc -f split
    • perldoc -f chomp
    • perldoc perldsc
Re: Nested Loop Problems
by johngg (Canon) on May 02, 2012 at 21:33 UTC

    It is possible to populate the four arrays in one go by pushing onto the correct array as decided by nested ternaries. However, it doesn't look particularly clean and simple and you are probably better off to start with following ww's suggestion to break the problem into two stages. Here's one way to do it in one fell swoop.

    knoppix@Microknoppix:~$ perl -Mstrict -wE ' > open my $fh, q{<}, \ <<EOD or die $!; > name1,a1,b1,c1 > name2,a2,b2,c2 > name3,a3,b3,c3 > EOD > > my( @c1, @c2, @c3, @c4 ); > while ( <$fh> ) > { > chomp; > my $idx = 0; > push @{ > $_->[ 0 ] == 1 ? \ @c1 > : $_->[ 0 ] == 2 ? \ @c2 > : $_->[ 0 ] == 3 ? \ @c3 > : \ @c4 }, $_->[ 1 ] for > map { [ ++ $idx, $_ ] } > split m{,}; > } > > say q{}; > say qq{@c1}; > say qq{@c2}; > say qq{@c3}; > say qq{@c4};' name1 name2 name3 a1 a2 a3 b1 b2 b3 c1 c2 c3 knoppix@Microknoppix:~$

    I hope this is of interest.

    Update: corrected broken link.

    Cheers,

    JohnGG

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-23 18:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found