Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

How to slice a split directly?
This works and does what I want it to:

perl -e '@x = split("\\.", "a.b.c"); print $x[0];'

Why does not this work?
perl -e 'print  @{split("\\.", "a.b.c")}[0];'

Is there a compact way to take a slice of a split (or other function that
returns an array) without creating a temporary variable?

Thanks,
Siegfried


Report this thread to moderator Post Follow-up to this message
Old Post
Siegfried Heintze
09-24-04 01:57 AM


Re: How to slice a split directly?
On Thu, 23 Sep 2004 13:43:08 -0600, Siegfried Heintze
<siegfried@heintze.com> wrote:
> This works and does what I want it to:
>
> perl -e '@x = split("\\.", "a.b.c"); print $x[0];'
>
> Why does not this work?
> perl -e 'print  @{split("\\.", "a.b.c")}[0];'

Because split doesn't return an array reference, it returns a list.

print(  (split(/\./, "a.b.c"))[0]   );

>
> Is there a compact way to take a slice of a split (or other function that
> returns an array) without creating a temporary variable?
>
>   Thanks,
>      Siegfried
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>



--
-will
http://www.wgunther.tk
(the above message is double rot13 encoded for security reasons)

Most Useful Perl Modules
-strict
-warnings
-Devel::DProf
-Benchmark
-B::Deparse
-Data::Dumper
-Clone
-Perl::Tidy
-Beautifier
-DBD::SQLite

Report this thread to moderator Post Follow-up to this message
Old Post
William Gunther
09-24-04 01:57 AM


Re: How to slice a split directly?
From: "Siegfried Heintze" <siegfried@heintze.com>
> This works and does what I want it to:
>
> perl -e '@x = split("\\.", "a.b.c"); print $x[0];'
>
> Why does not this work?
> perl -e 'print  @{split("\\.", "a.b.c")}[0];'
>
> Is there a compact way to take a slice of a split (or other function
> that returns an array) without creating a temporary variable?

perl -e 'print ((split("\\.", "a.b.c"))[0]);'

It's a bit tricky. The outermost braces belong to print(), the next
ones enclose the call to split() so that it can be sliced and the
innermost enclose the parameters for split(). Only the innermost may
be left out.

This makes the slicing of function result a bit clearer I think:

($hour, $minute, $sec) = (localtime())[2,1,0];

Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


Report this thread to moderator Post Follow-up to this message
Old Post
Jenda Krynicky
09-24-04 01:57 AM


Re: How to slice a split directly?
> This works and does what I want it to:
>
> perl -e '@x = split("\\.", "a.b.c"); print $x[0];'
>
> Why does not this work?
> perl -e 'print  @{split("\\.", "a.b.c")}[0];'
>
> Is there a compact way to take a slice of a split (or other function that
> returns an array) without creating a temporary variable?
>
>    Thanks,
>       Siegfried
>

Sometimes when working out this kind of detail it is helpful to make a
full script and activate strict/warnings. In the above case you get the
following,

> perl -Mstrict -w -e 'print  @{split("\\.", "a.b.c")}[0];'
Use of implicit split to @_ is deprecated at -e line 1.
Can't use string ("3") as an ARRAY ref while "strict refs" in use at -e
line 1.

Essentially C<split> returns a list, the construct C<@{ }> is a way to
slice into a hash, which you don't have. So you need to slice into a
list, which in this case is done like,

perl -Mstrict -w -e 'print  ((split("\\.", "a.b.c"))[0]);'

Notice the extra set of parens, otherwise you get a syntax error because
C<print> would otherwise use the first set as an argument list.

HTH,

http://danconia.org

Report this thread to moderator Post Follow-up to this message
Old Post
Wiggins d Anconia
09-24-04 01:57 AM


Re: How to slice a split directly?
From: "Wiggins d Anconia" <wiggins@danconia.org> 
>
> Sometimes when working out this kind of detail it is helpful to make a
> full script and activate strict/warnings. In the above case you get
> the following,
> 
> Use of implicit split to @_ is deprecated at -e line 1.
> Can't use string ("3") as an ARRAY ref while "strict refs" in use at
> -e line 1.
>
> Essentially C<split> returns a list, the construct C<@{ }> is a way to
> slice into a hash, which you don't have.

There'd have to be a name of a variable between the @ and the opening
curly brace for that to be a hash slice. This way its an array
dereference:

@arr = (1,2,3);
$rArr = \@arr;
@other = @{$rArr};

Of course in this case you do not need the braces.

You'd use the @{} if the thing you need to dereference is more
complex, for example if you need to dereference a function result.

So
print join(', ', @{function('returning', 'arrayref')});
would be correct, just like
print join(', ', @{function('returning', 'arrayref')}[1,2,4,7]);

If you'd want just one item from the referenced array you would of
course use ${}[] instead:

print ${function('returning', 'arrayref')}[2];

In this case the function doesn't return a reference, but a list so
there is no point in trying to dereference anything :-)

This is where the "Can't use string ("3") as an ARRAY ref while
"strict refs" in use at -e line 1." message comes from. The split()
is evaluated in scalar context and thus returns the number of
elements found in the string. And then the code tries to do this:

perl -e 'print  @{3}[0];'

Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


Report this thread to moderator Post Follow-up to this message
Old Post
Jenda Krynicky
09-24-04 01:57 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:29 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.