garbage777 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I have the following code:
my $l = "WINGFRAME MACH1;VECTORTHRUST 40 00 40 40;VECTORTHRUST 60 00 +40 90;VECTORTHRUST 10 40 30 10;VECTORTHRUST 80 10 10 10;VECTORTHRUST +60 10 95 00;VECTORTHRUST 70 00 60 00;VECTORTHRUST 80 00 70 90;VECTORT +HRUST 60 50 80 90;VECTORTHRUST 60 40 80 10;WINGFRAME MACH2;VECTORTHR +UST 40 00 40 40;VECTORTHRUST 60 00 40 90;VECTORTHRUST 10 40 30 10;VEC +TORTHRUST 80 10 10 10;VECTORTHRUST 60 10 95 00;VECTORTHRUST 70 00 60 +00;VECTORTHRUST 80 00 70 90;VECTORTHRUST 60 50 80 90;VECTORTHRUST 60 +40 80 10;"; my @a = split(/WINGFRAME /,$l); foreach (@a) { print $_,"\n"; } print scalar (@a),"\n";
When I run the above code,we see there is a "blank value" for $a[0].
Why does split function put blank value into the array?

Replies are listed 'Best First'.
Re: split function gives emply line as output of the array element
by Anonymous Monk on Nov 09, 2009 at 06:47 UTC
    perldoc -f split
    split /PATTERN/ split Splits the string EXPR into a list of strings and returns that list. By default, empty leading fields are preserved, and empt +y trailing ones are deleted. (If all fields are empty, they are considered to be trailing.)
    Why don't you split on ";" like has already been suggested?
      If there is no leading empty space (field), in his case shouldn't split return just two elements in the array?
        split CUTS your string into fields. Between the beginning of string and "WINGFRAME ", there is the empty string. Example
        $ perl -e " print qq!($_)\n! for split /,/, q!,1,2,3!" () (1) (2) (3) $ perl -e " print qq!($_)\n! for split /,/, q!,,,,1,2,3!" () () () () (1) (2) (3) $ perl -e " print qq!($_)\n! for split /(,)/, q!,,,,1,2,3!" () (,) () (,) () (,) () (,) (1) (,) (2) (,) (3)
        There is an empty string around every corner
        $ perl -e " print qq!($_)\n! for split /,/, q!5,,,!" (5) $ perl -e " print qq!($_)\n! for split /(,)/, q!5,,,!" (5) (,) () (,) () (,)
Re: split function gives emply line as output of the array element
by johngg (Canon) on Nov 09, 2009 at 11:39 UTC

    If you don't mind keeping "WINGFRAME " in your elements use zero-width look-behind and -ahead assertions to split at points in the string that are not preceded by beginning of string and are followed by "WINGFRAME ".

    $ perl -Mstrict -MData::Dumper -wle ' > my $line = q{WINGFRAME MACH1;VECTORTHRUST 40 00 40 40;VECTORTHRUST +60 00 40 90;VECTORTHRUST 10 40 30 10;VECTORTHRUST 80 10 10 10;VECTORT +HRUST 60 10 95 00;VECTORTHRUST 70 00 60 00;VECTORTHRUST 80 00 70 90;V +ECTORTHRUST 60 50 80 90;VECTORTHRUST 60 40 80 10;WINGFRAME MACH2;VEC +TORTHRUST 40 00 40 40;VECTORTHRUST 60 00 40 90;VECTORTHRUST 10 40 30 +10;VECTORTHRUST 80 10 10 10;VECTORTHRUST 60 10 95 00;VECTORTHRUST 70 +00 60 00;VECTORTHRUST 80 00 70 90;VECTORTHRUST 60 50 80 90;VECTORTHRU +ST 60 40 80 10;}; > my @arr = split m{(?x) (?<!\A) (?=WINGFRAME )}, $line; > print Data::Dumper->Dumpxs( [ \ @arr ], [ qw{ *arr } ] );' @arr = ( 'WINGFRAME MACH1;VECTORTHRUST 40 00 40 40;VECTORTHRUST 60 00 + 40 90;VECTORTHRUST 10 40 30 10;VECTORTHRUST 80 10 10 10;VECTORTHRUST + 60 10 95 00;VECTORTHRUST 70 00 60 00;VECTORTHRUST 80 00 70 90;VECTOR +THRUST 60 50 80 90;VECTORTHRUST 60 40 80 10;', 'WINGFRAME MACH2;VECTORTHRUST 40 00 40 40;VECTORTHRUST 60 00 + 40 90;VECTORTHRUST 10 40 30 10;VECTORTHRUST 80 10 10 10;VECTORTHRUST + 60 10 95 00;VECTORTHRUST 70 00 60 00;VECTORTHRUST 80 00 70 90;VECTOR +THRUST 60 50 80 90;VECTORTHRUST 60 40 80 10;' ); $

    If you really don't want the "WINGFRAME " text then remove it by substitution in a map.

    $ perl -Mstrict -MData::Dumper -wle ' > my $line = q{WINGFRAME MACH1;VECTORTHRUST 40 00 40 40;VECTORTHRUST +60 00 40 90;VECTORTHRUST 10 40 30 10;VECTORTHRUST 80 10 10 10;VECTORT +HRUST 60 10 95 00;VECTORTHRUST 70 00 60 00;VECTORTHRUST 80 00 70 90;V +ECTORTHRUST 60 50 80 90;VECTORTHRUST 60 40 80 10;WINGFRAME MACH2;VEC +TORTHRUST 40 00 40 40;VECTORTHRUST 60 00 40 90;VECTORTHRUST 10 40 30 +10;VECTORTHRUST 80 10 10 10;VECTORTHRUST 60 10 95 00;VECTORTHRUST 70 +00 60 00;VECTORTHRUST 80 00 70 90;VECTORTHRUST 60 50 80 90;VECTORTHRU +ST 60 40 80 10;}; > my @arr = > map { s{WINGFRAME }{}; $_ } > split m{(?x) (?<!\A) (?=WINGFRAME )}, $line; > print Data::Dumper->Dumpxs( [ \ @arr ], [ qw{ *arr } ] );' @arr = ( ' MACH1;VECTORTHRUST 40 00 40 40;VECTORTHRUST 60 00 40 90;VEC +TORTHRUST 10 40 30 10;VECTORTHRUST 80 10 10 10;VECTORTHRUST 60 10 95 +00;VECTORTHRUST 70 00 60 00;VECTORTHRUST 80 00 70 90;VECTORTHRUST 60 +50 80 90;VECTORTHRUST 60 40 80 10;', ' MACH2;VECTORTHRUST 40 00 40 40;VECTORTHRUST 60 00 40 90;VEC +TORTHRUST 10 40 30 10;VECTORTHRUST 80 10 10 10;VECTORTHRUST 60 10 95 +00;VECTORTHRUST 70 00 60 00;VECTORTHRUST 80 00 70 90;VECTORTHRUST 60 +50 80 90;VECTORTHRUST 60 40 80 10;' ); $

    I hope this is helpful.

    Cheers,

    JohnGG