Taking a subscript from a sub... 
Author Message
 Taking a subscript from a sub...

  I was wondering if there's a simpler way to pay attention to a
single element of an array returned by a sub, without having to
do something of this nature:

($junk, $junk, $junk, $junk, $junk, $year, $junk, $junk, $junk) = localtime(time);

  I tried doing $year=localtime(time)[5] and it choked...



Mon, 17 Jun 1996 01:49:16 GMT  
 Taking a subscript from a sub...
Jim>   I tried doing $year=localtime(time)[5] and it choked...

The syntax for the inline slice is not

        VALUE [ SUB ]

but rather

        ( VALUE ) [ SUB ]

hence you are missing a parenthesis pair.  Also, the result is an array,
not a scalar, so you'll want an array lvalue on the left:

        ($year) = (localtime(time))[5];

and that should do it.

print (($junk,$junk,$junk,"Just another Perl hacker,"))[27] # but why does this work? :-)
--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying

Phrase: "Welcome to Portland, Oregon ... home of the California Raisins!"



Mon, 17 Jun 1996 03:01:31 GMT  
 Taking a subscript from a sub...


Quote:
>Also, the result is an array,
>not a scalar, so you'll want an array lvalue on the left:

>    ($year) = (localtime(time))[5];

Both the above and

         $year  = (localtime(time))[5];

work in Perl 4.34 running on a CONVEX C2.  Both set $year to 93.

Quote:
>print (($junk,$junk,$junk,"Just another Perl hacker,"))[27]
># but why does this work? :-)

Hmm.  "print (" is taken as the start of a function call.  Its argument
is an array of four values.  $junk is undefined, so it prints as
nothing.  The print is executed before the subscript operation.
However, why it doesn't give a syntax error like locatime, sin, open, et
al is beyond me.

--
Tim McDaniel, Convex Computer Corporation, Richardson, TX (near Dallas)




Mon, 17 Jun 1996 03:28:44 GMT  
 Taking a subscript from a sub...

Quote:

>  I was wondering if there's a simpler way to pay attention to a
>single element of an array returned by a sub, without having to
>do something of this nature:

>($junk, $junk, $junk, $junk, $junk, $year, $junk, $junk, $junk) = localtime(time);

>  I tried doing $year=localtime(time)[5] and it choked...

Try another set of parenthesis:

        $year=(localtime(time))[5]

Dean



Sun, 16 Jun 1996 22:27:51 GMT  
 Taking a subscript from a sub...
: >print (($junk,$junk,$junk,"Just another Perl hacker,"))[27]
: ># but why does this work? :-)
:
: Hmm.  "print (" is taken as the start of a function call.  Its argument
: is an array of four values.  $junk is undefined, so it prints as
: nothing.  The print is executed before the subscript operation.
: However, why it doesn't give a syntax error like locatime, sin, open, et
: al is beyond me.

In perl 5 it says

    Can't use print as reference in array deref at - line 1, near "27]"

which can at least be construed to mean that SOMETHING went wrong.

Larry



Tue, 18 Jun 1996 08:00:12 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. last in a sub taking effect in caller

2. differance between &sub and &'sub

3. Outer scope of a sub inside a sub

4. Bug in Net::FTP sub _extract_path (or Net::Cmd sub parse_response)

5. Subscript of last element in an array

6. Is there a magic subscript number?

7. Differences between anonymous subs and named subs

8. get name of previous sub that called the actual sub

9. Evaluation order of variables in subscript to anonymous array

10. negative subscripts

11. Can I use a sub within a sub?

12. which sub calls my sub in perl module ?

 

 
Powered by phpBB® Forum Software