Alan Haggai Alavi wrote:
On 11 April 2010 21:10, Harry Putnam <rea...@newsguy.com> 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);

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.

No, the result will be a list in list context or a scalar in scalar context.

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

Again, wrong.  The only time that split will return undef is:

perldoc -f split
[ SNIP ]
            As with regular pattern matching, any capturing parentheses
            that are not matched in a "split()" will be set to "undef"
            when returned:

                @fields = split /(A)|B/, "1A2B3";
                # @fields is (1, 'A', 2, undef, 3)

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.

Incorrect. The pattern // will split the string into individual characters. Try it and see.

Hope it is clear now.

No, nothing is clear from those answers.



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to