On Jul 28, 10:12 am, [email protected] (Chandan Kumar) wrote:
> Hi ,
>
> using split ,I would like to split on space character. Dont want to use
> character classes.
> my $string="108 ambulance 100\nfireengine141jack";
>
> my @array = split /' '/,$string;
>
From the doc: perldoc -f split
As a special case, specifying a PATTERN of space (' ') will
split on white space just as "split" with no arguments does.
Thus, "split(' ')" can be used to emulate awk's default ...
So, /' '/ in your regex causes a pattern match on a
literal ' '. When the search fails, the entire $string is
returned:
> foreach my $value (@array){
> print "$value \n";}
>
>
> Result : its splitting on new line character.instead of space character.
>
> Can some one clarify ?
No, there's no split on a newline. You'd have to specify
/\n/ for that to happen. In your case, the whole string
is returned since the pattern match fails. This would've
been clear if your code had been:
foreach my $value {...@value) { print "=$value=\n"; }
which would've printed:
=108 ambulance 100
fireengine141jack=
>
> for the same if the string is enclosed in single quotes ,splitting is not
> happening on the given string.
>
No, single quotes don't impact the split but only
whether interpolation occurs inside the string.
--
Charles DeRykus
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/