On 11 April 2010 21:10, Harry Putnam <[email protected]> wrote:
> Why is it that the first two splits do not produce any elements?
>
> #!/usr/local/bin/perl
>
> use strict;
> use warnings;
>
> my $var = 100421;
> my @elems1 = split(/\d/,$var);
> my @elems2 = split(/./,$var);
> my @elems3 = split(//,$var);
>
> if (@elems1){
>  print "elems1 has these elements:\n";
>  for(@elems1){
>    print "$_\n";
>  }
>  print"-------       -------       ---=---       -------      -------\n";
> }
> if (@elems2){
>  print "elems2 has these elements:\n";
>  for(@elems2){
>    print "$_\n";
>  }
>  print"-------       -------       ---=---       -------      -------\n";
> }
> if (@elems3){
>  print "elems3 has these elements\n";
>  for(@elems3){
>    print "$_\n";
>  }
>  print"-------       -------       ---=---       -------      -------\n";
> }
>
>
>
>
>
> --
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> http://learn.perl.org/
>
>
>

Hi Harry,


split considers the pattern given to it, as a delimiter within the
expression. Let us consider the three cases:

> my @elems1 = split(/\d/,$var);
Digits are the delimiters. Delimiters are stripped out of the string
by split. Hence, when digits are stripped out of a numeric expression,
the result will be undef.


> my @elems2 = split(/./,$var);
Again, anything is considered to be a delimiter due to the '.' (match
anything). Thus, again, the result is undef.


> my @elems3 = split(//,$var);
When the pattern is omitted, split splits on whitespace. Since the
string does not contain whitespaces, the string cannot be split and
split returns the string as it is.

Hope it is clear now.


Regards,
Alan Haggai Alavi.
-- 
The difference makes the difference

--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to