Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Nested Loop Problems

by furry_marmot (Pilgrim)
on May 02, 2012 at 21:10 UTC ( #968543=note: print w/replies, xml ) Need Help??

in reply to Nested Loop Problems

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...

attempts to split an array, which is never going to happen. Then...
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

Further reading:

  • perldoc -f split
  • perldoc -f chomp
  • perldoc perldsc

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://968543]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2017-04-25 06:52 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (449 votes). Check out past polls.