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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.