Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

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


Comment on Re: Nested Loop Problems
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://968543]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (16)
As of 2014-08-27 16:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (244 votes), past polls