Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

just mucking about with splits

2 views
Skip to first unread message

Igor Aptekar

unread,
Dec 19, 2000, 8:09:37 AM12/19/00
to
anyone know how i can do something like print split(",",$a)[1]

thats is get whatever item of the split array i want without using an
intermediate variable

just curious


Rafael Garcia-Suarez

unread,
Dec 19, 2000, 8:37:50 AM12/19/00
to
Igor Aptekar wrote in comp.lang.perl.misc:

> anyone know how i can do something like print split(",",$a)[1]

Insert the appropriate parenthesis according to the precedence of
operators (that may not always be what you expect) :

print ( (split(/,/,$ARGV[0])) [1] )

--
# Rafael Garcia-Suarez / http://rgarciasuarez.free.fr/

Dick Latshaw

unread,
Dec 19, 2000, 8:58:08 AM12/19/00
to
In article <Z5J%5.73304$eT4.5...@nnrp3.clara.net>,

Try print (split /,/,$a)[1];

--
Regards,
Dick


Sent via Deja.com
http://www.deja.com/

Igor Aptekar

unread,
Dec 19, 2000, 9:26:25 AM12/19/00
to
this brings up a lot of errors - no compile


"Dick Latshaw" <lats...@my-deja.com> wrote in message
news:91nphd$pmh$1...@nnrp1.deja.com...

Tad McClellan

unread,
Dec 19, 2000, 9:19:35 AM12/19/00
to


Use a "list slice" (see the "Slices" section in perldata.pod):

$second_element = ( split(/,/, $a) )[1]
^ ^
^ ^

Patterns should _look like_ patterns, not like strings.


--
Tad McClellan SGML consulting
ta...@metronet.com Perl programming
Fort Worth, Texas

Richard Zilavec

unread,
Dec 19, 2000, 11:40:27 AM12/19/00
to
On Tue, 19 Dec 2000 14:26:25 -0000, "Igor Aptekar"
<igor_a...@programmer.net> wrote:

>this brings up a lot of errors - no compile

For me too..


>>
>> Try print (split /,/,$a)[1];

my $tmp = (split/,/, $string)[1];
print $tmp. "\n";

Works fine..

--
Richard Zilavec
rzil...@tcn.net

Rafael Garcia-Suarez

unread,
Dec 19, 2000, 11:53:28 AM12/19/00
to
Dick Latshaw wrote in comp.lang.perl.misc:

> In article <Z5J%5.73304$eT4.5...@nnrp3.clara.net>,
> "Igor Aptekar" <igor_a...@programmer.net> wrote:
> > anyone know how i can do something like print split(",",$a)[1]
> >
> > thats is get whatever item of the split array i want without using an
> > intermediate variable
>
> Try print (split /,/,$a)[1];

which is parsed as
(print (split /,/,$a))[1];
and then doesn't work.

Bart Lateur

unread,
Dec 20, 2000, 4:34:50 AM12/20/00
to
Igor Aptekar wrote:

>> Try print (split /,/,$a)[1];

>this brings up a lot of errors - no compile

That is because the parentheses make what's between them the arguments
for print(). Putting a '+' in front of the opening paren will change
that. '+' has no other effect, so +"foo" is still "foo". Or, as Richard
Zilavec wrote, stuff the result of the expression into a variable first,
and use that.

--
Bart.

0 new messages