Function Split

G

George Kinley

Hi,
Just wondering, when we use function "split" it returns a list ,
now is this possible to, directly access any index of a that list with out
initializing to a list variable
for example ,
$S="win.kit"
@L=split (/\./,$S)
we get @L=("wins","kit")
return @L[0]
what I want is some thing like
@{splits(/\./,$S)}[0] # is it possible :-o)
 
G

Gunnar Hjalmarsson

George said:
Just wondering, when we use function "split" it returns a list ,
now is this possible to, directly access any index of a that list
with out initializing to a list variable
for example ,
$S="win.kit"
@L=split (/\./,$S)
we get @L=("wins","kit")
return @L[0]
what I want is some thing like
@{splits(/\./,$S)}[0] # is it possible :-o)

You can do:

return ( split /\./, $S )[0];
 
A

Ala Qumsieh

George Kinley said:
Hi,
Just wondering, when we use function "split" it returns a list ,
now is this possible to, directly access any index of a that list with out
initializing to a list variable
for example ,
$S="win.kit"
@L=split (/\./,$S)
we get @L=("wins","kit")

Not really :)
return @L[0]

You most probably should return $L[0]. Check out the faqs for the difference
between the two:

% perldoc -q '\$array'
Found in /home/gnu/perl-5.6.1/lib/5.6.1/pod/perlfaq4.pod
What is the difference between $array[1] and @array[1]?

what I want is some thing like
@{splits(/\./,$S)}[0] # is it possible :-o)

return (split /\./, $S)[0];

--Ala
 
B

Brian McCauley

George Kinley said:
Subject: Function Split

You are not paritioning your problem properly.
Just wondering, when we use function "split" it returns a list ,

Indeed split is just an example of a function that returns a list.
Technically all Perl functions (evaluated in list contest) return lists,
although some will always return single element list.

Your question is not about split, it's about lists.
now is this possible to, directly access any index of a that list with out
initializing to a list variable
for example ,
$S="win.kit"
@L=split (/\./,$S)
we get @L=("wins","kit")
return @L[0]
what I want is some thing like
@{splits(/\./,$S)}[0] # is it possible :-o)

Close, but @{ whatever() }[0] is an array reference slice so
whatever() must evaluate to an array reference.

You can make a list into an array reference using [].

@{[split (/\./,$S)]}[0]

But this still initializes an array variable, albeit an anonymous
one, and you said you did't want that.

What you want is a list slice.

(split (/\./, $S, 2))[0]

Note the 2 in the call to split() to prevent it building a huge list
only to throw away all but the first element.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top