Perl Monk, Perl Meditation PerlMonks

### Re^2: split function

by tcheungcm (Novice)
 on Jan 25, 2012 at 08:20 UTC ( #949844=note: print w/replies, xml ) Need Help??

in reply to Re: split function

Thanks. Here is my code

```sub split_the_element{
my (\$local_fh) = @_;
#my @records = split(/;/);
my (\$var1, \$var2, \$var3, \$var4, \$var5, \$var6, \$var7, \$var8, \$var9, \$
+var10, \$var11, \$var12,
\$var13, \$var14, \$var15, \$var16, \$var17, \$var18, \$var19, \$var20,
+\$var21, \$var22, \$var23, \$var24,
\$var25, \$var26, \$var27, \$var28, \$var29, \$var30, \$var31, \$var32,
+\$var33, \$var34, \$var35, \$var36) =split(/;/);
print "1 element \$var1\n";
print "2 element \$var2\n";
print "3 element \$var3\n";
print "4 element \$var4\n";
print "5 element \$var5\n";
print "6 element \$var6\n";
print "7 element \$var7\n";
print "8 element \$var8\n";
print "9 element \$var9\n";
print "10 element \$var10\n";
print "11 element \$var11\n";
print "12 element \$var12\n";
print "13 element \$var13\n";
print "14 element \$var14\n";
print "15 element \$var15\n";
print "16 element \$var16\n";
print "17 element \$var17\n";
#my \$i=0;
#foreach(\$i=1; \$i<=36; \$++) {
# print "\$i element \$var\$i\n"
#}
}
}

Here is my data file 1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30;31;32;33;34;35;36

And the output:

1 element 1

2 element 2

3 element 3

4 element 4

5 element 5

6 element 6

7 element 7

8 element 8

9 element 9

10 element 10

11 element 11

12 element 12

13 element 13

14 element 14

15 element 15

16 element 16

17 element 17

1 element 21

2 element 22

3 element 23

4 element 24

5 element 25

6 element 26

7 element 27

8 element 28

9 element 29

10 element 30

11 element 31

12 element 32

13 element 33

14 element 34

15 element 35

16 element 36

It seems that data file can split up to 17th elements and then the rest will start to split on the next line.

Replies are listed 'Best First'.
Re^3: split function
by Marshall (Abbot) on Jan 25, 2012 at 08:34 UTC
```#!/usr/bin/perl -w
use strict;

my \$data ='1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23
+;24;25;26;27;28;29;30;31;32;33;34;35;36';

my @elements = split (';',\$data);

my \$number =1;
foreach my \$element (@elements)
# or could be:
# foreach my \$element (split (';',\$data))
{
print "number=\$number\t element=\$element\n";
\$number++;
}
The above works.

I think that there is a "new line" in your data file file.

```sub split_the_element
{
my (\$fh_in) = @_; #or: my \$fh_in = shift;
my @result;
while (<\$fh_in>)
{
chomp;
foreach my \$element (split ';', \$_)
{
push (@result, \$element);
}
}
return @result;
}
Re^3: split function
by Anonymous Monk on Jan 25, 2012 at 08:37 UTC

It seems that data file can split up to 17th elements and then the rest will start to split on the next line.

Um, no, that would be jumping to conclusions.

```my @junk = split /;/, join ';', 'a'..'z', 'A'..'J';
print int(@junk),"\n";
print "@junk\n";
print "\$_=\$junk[\$_]  " for 0,1,10,20,33,34,-1;
__END__
36
a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I
+J
0=a  1=b  10=k  20=u  33=H  34=I  -1=J

The reason your foreach loop doesn't work is explained in this FAQ, read all 3 parts

Read perlintro and the array /loop chapters of http://learn.perl.org/books/beginning-perl/

Re^3: split function
by rovf (Priest) on Jan 25, 2012 at 11:22 UTC
It seems that data file can split up to 17th elements and then the rest will start to split on the next line.
I don't see from your output that split would behave in this way, but you can easily verify this, by doing a

```print "LINE JUST READ IS: [\$_]\n";
just before the split.

--
Ronald Fischer <ynnor@mm.st>

Thanks all. I think the problem is happened on the readline. After I add the line before the split

print "Line just read is \$_\n";

the output is

I also checked the lenght of each line is limited 51. Anyone would have clue?

Did you hexdump your data file?

--
Ronald Fischer <ynnor@mm.st>

Hopefully you're sufficiently convinced split function isn't broken, so your question is?

Create A New User
Node Status?
node history
Node Type: note [id://949844]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2018-03-24 01:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (297 votes). Check out past polls.

Notices?