sanket vaidya wrote:
Hi,

Hello,

Consider the code below:

use warnings;
use strict;
my $string = '100955 BLow-Gomez,Joseph MMEX.AMER. QHUTC012';

my ($id) = split(/\s/,$string);

print "id = $id";

Output:

100955

Now remove brackets surrounding $id like as under:

use warnings;
use strict;
my $string = '100955 BLow-Gomez,Joseph MMEX.AMER. QHUTC012';

my $id = split(/\s/,$string);

print "id = $id";

Output:
4

This is the no. of elements in the array generated by split.

Why in first case (when brackets are used) it prints correct id?

This is the way that split() works. When used in list context, the scalar variable in parentheses, it fills the list items or array from the string that was split up. When used in scalar context (without parentheses) it fills the @_ array and puts the number of elements split in the scalar variable.

Because you have warnings enabled you should have received a warning something like this for your second example:

Use of implicit split to @_ is deprecated at your_program line 6.




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