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

Re: split function

by Marshall (Prior)
on Jan 25, 2012 at 07:40 UTC ( #949843=note: print w/ replies, xml ) Need Help??


in reply to split function

A more normal formulation of this would be:

while (<$fh_in>) { chomp; #deletes the trailing "new line" line ending #this works for files from any OS my @array = split(';',$_); }
$a,$b,$1,$2 etc are special variables that Perl uses.
Never assign values to any of these.
These should be "read only" variables as far as your code is concerned.

while (<$fh_in>) { print; #prints $_ (the line from $fh_in) my ($first_thing) = split(';',$_); print "$first_thing\n"; }
Is there a limit of length that split function can handle, otherwise, it will truncate the line into next line. is there a way how to resolve it?

There is no "hard limit" on the length of the expression that split can handle nor is there a limit on the size of the resulting array. ...well ok.. there is a limit somewhere, but this limit is certainly not 36 ... it is in the range of many thousands or even more... In a practical sense: NO. There is no limit, meaning that the limit is so huge that you won't encounter it in normal programming.


Comment on Re: split function
Select or Download Code
Re^2: split function
by tcheungcm (Initiate) on Jan 25, 2012 at 08:20 UTC

    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.

      #!/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; }
      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?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2014-09-21 21:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (176 votes), past polls