Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

split function

by tcheungcm (Initiate)
on Jan 25, 2012 at 02:55 UTC ( #949808=perlquestion: print w/ replies, xml ) Need Help??
tcheungcm has asked for the wisdom of the Perl Monks concerning the following question:

Is there a length limit to split a line into mutliple variables?

I tried to split a string into a multiple variables where they are using commas delimiter to separate. It has 36 variables per line.

e.g. 123;abc;456;def;123;;123...

My code is

while (readline $fhin) { my ($1, $2, $3,....$36) = split(/;/); printf $fhout, $s,\n", $1; }

When I looked at the output files, it gave werid output where it mixed up with the different variable and print it out

123,

abc,

123,

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?

The perl version is 5.8.8

Comment on split function
Download Code
Re: split function
by davido (Archbishop) on Jan 25, 2012 at 03:19 UTC

    Does Perl 5.8.8 allow you to explicitly assign values to numbered variables such as $1? Because with Perl 5.14.2, with this snippet:

    perl -E '$1 = 100; say $1;'

    I get this message:

    Modification of a read-only value attempted at...

    Perl imposes no artificial limits on how many elements a string may be split into with the split function, nor does Perl care how many variables you assign to. So I think for us to determine what is going wrong you would need to show us a small snippet of the actual code that is failing.

    Though Perl imposes no limits on how many variables you can assign to, common sense and good style may dictate that assigning to thirty six variables that are numbered might be a construct better handled with an array.


    Dave

Re: split function
by Anonymous Monk on Jan 25, 2012 at 03:23 UTC
    $ perl -le " my($1,$2) ; " Can't use global $1 in "my" at -e line 1, near "($1" Execution of -e aborted due to compilation errors.

    $1,$2... are reserved for match operator (regex)

    Post real code, that compiles under strict/warnings

Re: split function
by kielstirling (Scribe) on Jan 25, 2012 at 03:28 UTC
    Hi,

    Kind of hard to help you with the code provided.

    split returns an array so I would do something like

    my @values = split ';', "123;abc;456;def;123;;123";

    Then loop the array and print each element to the file

    print FILE "$_,\n" foreach @values;

    As for a limit I would assume that the limit would be the system memory

    - Kiel Stirling
Re: split function
by Marshall (Prior) on Jan 25, 2012 at 07:40 UTC
    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.

      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>

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-08-29 06:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (275 votes), past polls