Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
laziness, impatience, and hubris
 
PerlMonks  

Re^2: split function

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


in reply to Re: split function
in thread split function

Thanks. Here is my code

sub split_the_element{ my ($local_fh) = @_; while (readline $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.


Comment on Re^2: split function
Download Code
Re^3: split function
by Marshall (Prior) 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
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

      Line just read is 1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;

      Line just read is 21;22;23;24;25;26;27;28;29;30;31;32;33;34;35;36

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

        Have a clue about what?

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

        Did you hexdump your data file?

        -- 
        Ronald Fischer <ynnor@mm.st>

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2014-04-19 15:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (482 votes), past polls