For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > September 2007 > White space split









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author White space split
manojkumarg@dataone.in

2007-09-20, 7:59 am

Hello,

I am trying to do this

#!/bin/perl
$cmd="maheshverama #XXXXXXXXXXXXXXXXXXXXXX#";
$cmd1=split /\s+/,$cmd;
print $cmd1;

Wanted the first part to be taken its giving me some numbers as output.

Thanks
Manoj
useperl-jeff@yahoo.com.cn

2007-09-20, 7:59 am


--- manojkumarg@dataone.in wrote:

> Hello,
>
> I am trying to do this
>
> #!/bin/perl
> $cmd="maheshverama #XXXXXXXXXXXXXXXXXXXXXX#";
> $cmd1=split /\s+/,$cmd;
> print $cmd1;
>
> Wanted the first part to be taken its giving me some numbers as output.
>


Hello,

because split return a list,which is expressed as a number when in scalar
context.This number is the list's length.
you may do,
my ($cmd1) = split/\s+/,$cmd;

($cmd1) force it to be in list context,and get the correct value.




________________________________________
________________________________________
____
Sick of deleting your inbox? Yahoo!7 Mail has free unlimited storage.
http://au.docs.yahoo.com/mail/unlimitedstorage.html

Andrew Curry

2007-09-20, 7:59 am

Appologies typo, I know the difference thanks.

-----Original Message-----
From: useperl-jeff@yahoo.com.cn [mailto:useperl-jeff@yahoo.com.cn]
Sent: 20 September 2007 12:57
To: beginners@perl.org
Subject: RE: White space split


--- Andrew Curry <andrew.curry@pa-sport.com> wrote:

> Split returns an array


returns a list not array.
see `perldoc -q "What is the difference between a list and an array?"`




________________________________________
____________________________________
________
Sick of deleting your inbox? Yahoo!7 Mail has free unlimited storage.
http://au.docs.yahoo.com/mail/unlimitedstorage.html


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



This e-mail is from the PA Group. For more information, see
www.thepagroup.com.

This e-mail may contain confidential information. Only the addressee is
permitted to read, copy, distribute or otherwise use this email or any
attachments. If you have received it in error, please contact the sender
immediately. Any opinion expressed in this e-mail is personal to the sender
and may not reflect the opinion of the PA Group.

Any e-mail reply to this address may be subject to interception or
monitoring for operational reasons or for lawful business practices.




useperl-jeff@yahoo.com.cn

2007-09-20, 7:59 am


--- Andrew Curry <andrew.curry@pa-sport.com> wrote:

> Split returns an array


returns a list not array.
see `perldoc -q "What is the difference between a list and an array?"`



________________________________________
________________________________________
____
Sick of deleting your inbox? Yahoo!7 Mail has free unlimited storage.
http://au.docs.yahoo.com/mail/unlimitedstorage.html

Andrew Curry

2007-09-20, 7:59 am

Split returns an array
So you need to do something like

#!/bin/perl
$cmd="maheshverama #XXXXXXXXXXXXXXXXXXXXXX#";
($key,$value)=split(/\s+/,$cmd);
print "$key, $value\n";

-----Original Message-----
From: manojkumarg@dataone.in [mailto:manojkumarg@dataone.in]
Sent: 20 September 2007 12:56
To: beginners@perl.org
Subject: White space split

Hello,

I am trying to do this

#!/bin/perl
$cmd="maheshverama #XXXXXXXXXXXXXXXXXXXXXX#";
$cmd1=split /\s+/,$cmd;
print $cmd1;

Wanted the first part to be taken its giving me some numbers as output.

Thanks
Manoj

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



This e-mail is from the PA Group. For more information, see
www.thepagroup.com.

This e-mail may contain confidential information. Only the addressee is
permitted to read, copy, distribute or otherwise use this email or any
attachments. If you have received it in error, please contact the sender
immediately. Any opinion expressed in this e-mail is personal to the sender
and may not reflect the opinion of the PA Group.

Any e-mail reply to this address may be subject to interception or
monitoring for operational reasons or for lawful business practices.




useperl-jeff@yahoo.com.cn

2007-09-20, 7:59 am


--- manojkumarg@dataone.in wrote:

> Thanks Andrew and Jeff.
> ($cmd1, $rest)=split/\s+/,cmd$; worked for me.
> my ($cmd1) = split/\s+/,$cmd; is still giving me the length...
> I wanted to use something like above ...


you may type something wrong?see the test,

$ cat t3.pl
my $cmd = "maheshverama #XXXXXXXXXXXXXXXXXXXXXX#";
my ($cmd1) = split /\s+/,$cmd;
print $cmd1;

$ perl t3.pl
maheshverama


________________________________________
________________________________________
____
Sick of deleting your inbox? Yahoo!7 Mail has free unlimited storage.
http://au.docs.yahoo.com/mail/unlimitedstorage.html

Andrew Curry

2007-09-20, 7:59 am

Try

my $cmd1 = (split/\s+/,$cmd)[0];

-----Original Message-----
From: useperl-jeff@yahoo.com.cn [mailto:useperl-jeff@yahoo.com.cn]
Sent: 20 September 2007 13:04
To: manojkumarg@dataone.in; Andrew Curry
Cc: beginners@perl.org
Subject: Re: RE: White space split


--- manojkumarg@dataone.in wrote:

> Thanks Andrew and Jeff.
> ($cmd1, $rest)=split/\s+/,cmd$; worked for me.
> my ($cmd1) = split/\s+/,$cmd; is still giving me the length...
> I wanted to use something like above ...


you may type something wrong?see the test,

$ cat t3.pl
my $cmd = "maheshverama #XXXXXXXXXXXXXXXXXXXXXX#";
my ($cmd1) = split /\s+/,$cmd;
print $cmd1;

$ perl t3.pl
maheshverama



________________________________________
____________________________________
________
Sick of deleting your inbox? Yahoo!7 Mail has free unlimited storage.
http://au.docs.yahoo.com/mail/unlimitedstorage.html


This e-mail is from the PA Group. For more information, see
www.thepagroup.com.

This e-mail may contain confidential information. Only the addressee is
permitted to read, copy, distribute or otherwise use this email or any
attachments. If you have received it in error, please contact the sender
immediately. Any opinion expressed in this e-mail is personal to the sender
and may not reflect the opinion of the PA Group.

Any e-mail reply to this address may be subject to interception or
monitoring for operational reasons or for lawful business practices.




manojkumarg@dataone.in

2007-09-20, 7:59 am

Yep...I got it ....!! ..


----- Original Message -----
From: useperl-jeff@yahoo.com.cn
Date: Thursday, September 20, 2007 5:44 pm
Subject: Re: RE: White space split
To: manojkumarg@dataone.in, Andrew Curry <andrew.curry@pa-sport.com>
Cc: beginners@perl.org


> --- manojkumarg@dataone.in wrote:
>
>
> you may type something wrong?see the test,
>
> $ cat t3.pl
> my $cmd = "maheshverama #XXXXXXXXXXXXXXXXXXXXXX#";
> my ($cmd1) = split /\s+/,$cmd;
> print $cmd1;
>
> $ perl t3.pl
> maheshverama
>
>
> ________________________________________
________________________________________
____
> Sick of deleting your inbox? Yahoo!7 Mail has free unlimited storage.
> http://au.docs.yahoo.com/mail/unlimitedstorage.html
>

Yitzle

2007-09-20, 7:00 pm

Note: split splits on whitespace by default, so these two are the same:
split /\s+/, @array;
split @array;
useperl-jeff@yahoo.com.cn

2007-09-20, 7:00 pm


--- yitzle <yitzle@users.sourceforge.net> wrote:

> Note: split splits on whitespace by default, so these two are the same:
> split /\s+/, @array;
> split @array;
>


Not right.You split a string,not an array or a list.
all below are wrong:
split @array;
split (11,22,33);
split $string;

see `perldoc -f split`.


________________________________________
________________________________________
____
Sick of deleting your inbox? Yahoo!7 Mail has free unlimited storage.
http://au.docs.yahoo.com/mail/unlimitedstorage.html

Uri Guttman

2007-09-20, 7:00 pm

>>>>> "u" == useperl-jeff <useperl-jeff@yahoo.com.cn> writes:
[color=darkred]

u> because split return a list,which is expressed as a number when in scalar
u> context.This number is the list's length.
u> you may do,

wrong answer. there is no such thing as a list in scalar context. split
is returning the number of elements because it is given the scalar
context. it isn't a function of 'list converting to integer' as that
never happens in perl because it can't.

u> my ($cmd1) = split/\s+/,$cmd;
u> ($cmd1) force it to be in list context,and get the correct value.

that part is correct.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Uri Guttman

2007-09-20, 7:00 pm

>>>>> "u" == useperl-jeff <useperl-jeff@yahoo.com.cn> writes:

u> --- Andrew Curry <andrew.curry@pa-sport.com> wrote:
[color=darkred]

u> returns a list not array.
u> see `perldoc -q "What is the difference between a list and an array?"`

and you see my other followup to you about contexts.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
John W. Krahn

2007-09-20, 7:00 pm

yitzle wrote:
> Note: split splits on whitespace by default, so these two are the same:
> split /\s+/, @array;
> split @array;


$ perl -le'
@array = "a" .. "z";
$_ = "@{[ 10 .. 40 ]}";
print for split /\s+/, @array;
'
26


split /\s+/, @array;

Is the same as:

split /\s+/, '26', 0;



$ perl -le'
@array = "a" .. "z";
$_ = "@{[ 10 .. 40 ]}";
print for split @array;
'
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
27 28 29 30 31 32 33 34 35 36 37 38 39 40


split @array;

Is the same as:

split /26/, $_, 0;




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Dr.Ruud

2007-09-20, 7:00 pm

Andrew Curry schreef:

> (split/\s+/,$cmd)


Very often, (split ' ', $cmd) is the better alternative.

--
Affijn, Ruud

"Gewoon is een tijger."
Chas Owens

2007-09-20, 7:00 pm

On 9/20/07, Dr.Ruud <rvtol+news@isolution.nl> wrote:
> Andrew Curry schreef:
>
>
> Very often, (split ' ', $cmd) is the better alternative.

snip

For the uninitiated: split ' ', $string does what split /\s+/, $string
does, but also removes leading whitespace.

The following code prints
||foo||bar||baz
foo||bar||baz

#!/usr/bin/perl

use strict;
use warnings;

print join('||', split /\s+/, " foo bar baz"), "\n",
join('||', split ' ', " foo bar baz"), "\n";
Rob Dixon

2007-09-20, 7:00 pm

yitzle wrote:
>
> Note: split splits on whitespace by default, so these two are the same:
> split /\s+/, @array;
> split @array;


Not true:

use strict;
use warnings;

my @list;

for (' A B C ') {

@list = split /\s+/, $_;
print scalar @list, "\n";

@list = split;
print scalar @list, "\n";
}

**OUTPUT**

4
3
John W. Krahn

2007-09-20, 7:00 pm

useperl-jeff@yahoo.com.cn wrote:
> --- yitzle <yitzle@users.sourceforge.net> wrote:
>
>
> Not right.You split a string,not an array or a list.
> all below are wrong:


It depends what you mean by "wrong".

> split @array;


Assuming that @array contains 6 elements, is parsed by perl as:

split /6/, $_, 0;


> split (11,22,33);


split( /11/, '22', 33 );


> split $string;


split /$string/, $_, 0;


> see `perldoc -f split`.




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Yitzle

2007-09-20, 10:01 pm

--- yitzle <yitzle@users.sourceforge.net> wrote:
> Note: split splits on whitespace by default, so these two are the same:
> split /\s+/, @array;
> split @array;


Technically, the two /are/ the same, and my point is valid. The fact
that I used an array instead of a string was a mistake. I blame it on
the lack of sleep caused by university.
Rob Dixon

2007-09-21, 4:00 am

yitzle wrote:
>
> --- yitzle <yitzle@users.sourceforge.net> wrote:
>
> Technically, the two /are/ the same, and my point is valid. The fact
> that I used an array instead of a string was a mistake. I blame it on
> the lack of sleep caused by university.


But they're not the same Yitzle as I pointed out in my earlier reply. The
default behaviour of split is achieved by the special case

split ' ', $string;

which does something slightly different from

split /\s+/, $string;

Good luck at university!

Rob
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com