usage of Split

--0-1061781526-1280337135=:60378
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

Hi ,
=A0
using split ,I would like to split on space character. Dont want to use cha=
racter classes.
my $string=3D"108 ambulance 100\nfireengine141jack";
=A0
my [at] array =3D split /' '/,$string;=A0
=A0
foreach my $value ( [at] array){
print "$value \n";
}
=A0
Result : its splitting on new line character.instead of space character.
=A0
Can some one clarify ?
=A0
for the same if the string is enclosed in single quotes ,splitting is not h=
appening on the given string.
=A0
=A0
Regards,
chandan.=0A=0A
--0-1061781526-1280337135=:60378--
Chandan Kumar [ Mi, 28 Juli 2010 19:12 ] [ ID #2045262 ]

Re: usage of Split

On Wednesday 28 Jul 2010 20:12:15 Chandan Kumar wrote:
> Hi ,
>
> using split ,I would like to split on space character. Dont want to use
> character classes. my $string=3D"108 ambulance 100\nfireengine141jack";
>
> my [at] array =3D split /' '/,$string;
>

You need =ABmy [at] array =3D split / /, $string;=BB instead. =ABsplit ' ', $st=
ring;=BB is
more magical.

Regards,

Shlomi Fish

=2D-
=2D--------------------------------------------------------- -------
Shlomi Fish http://www.shlomifish.org/
Interview with Ben Collins-Sussman - http://shlom.in/sussman

God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Shlomi Fish [ Mi, 28 Juli 2010 19:23 ] [ ID #2045263 ]

Re: usage of Split

On 07/28/10 19:12, Chandan Kumar wrote:
> using split ,I would like to split on space character. Dont want to use character classes.
> my $string="108 ambulance 100\nfireengine141jack";
>
> my [at] array = split /' '/,$string;
>
> foreach my $value ( [at] array){
> print "$value \n";
> }
>
> Result : its splitting on new line character.instead of space character.
>
> Can some one clarify ?

It's clarified in http://perldoc.perl.org/functions/split.html

my [at] array = split / /, $string;

> for the same if the string is enclosed in single quotes ,splitting is not happening on the given string.

Can you clarify what you mean by that?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Gunnar Hjalmarsson [ Mi, 28 Juli 2010 19:31 ] [ ID #2045264 ]

Re: usage of Split

On Jul 28, 10:12=A0am, chandan_28... [at] yahoo.com (Chandan Kumar) wrote:
> Hi ,
> =A0
> using split ,I would like to split on space character. Dont want to use c=
haracter classes.
> my $string=3D"108 ambulance 100\nfireengine141jack";
> =A0
> my [at] array =3D split /' '/,$string;=A0
>

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:
=A0
> foreach my $value ( [at] array){
> print "$value \n";}
>
> =A0
> Result : its splitting on new line character.instead of space character.
> =A0
> 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 { [at] value) { print "=3D$value=3D\n"; }

which would've printed:
=3D108 ambulance 100
fireengine141jack=3D

> =A0
> for the same if the string is enclosed in single quotes ,splitting is not=
happening on the given string.
> =A0

No, single quotes don't impact the split but only
whether interpolation occurs inside the string.

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
derykus [ Mi, 28 Juli 2010 23:17 ] [ ID #2045266 ]
Perl » gmane.comp.lang.perl.beginners » usage of Split

Vorheriges Thema: ftps module
Nächstes Thema: doubts in using Getopt in perl