How would I do the following?

D

DaLoverhino

I can do this in perl:

$#array

Which prints the index value of the last element in array.

Can I do this? :

$#(expression)

To print the index value of the last element of an expression that
evaluates to an array? Or something like it? I can't get it to work.


thanks.
 
P

Peter Makholm

DaLoverhino said:
Can I do this? :

$#(expression)

To print the index value of the last element of an expression that
evaluates to an array? Or something like it? I can't get it to work.

I have no idea why you would want to do that. But a common trick is to
make an anonymous array reference and dereference it
immediately. Works in you case too:

print $#{[ stat '.' ]}

//Makholm
 
T

Tad J McClellan

DaLoverhino said:
Subject: How would I do the following?


Please put the subject of your article in the Subject of your article.

I can do this in perl:

$#array

Which prints the index value of the last element in array.

Can I do this? :

$#(expression)

To print the index value of the last element of an expression that
evaluates to an array?


No.

But you can get it to print the index value of the last element of
an expression that evaluates to a array reference.

See "Use Rule 1" in:

perldoc perlreftut

i.e. $#{ returns_array_ref() }
 
J

jl_post

I can do this in perl:

$#array

Which prints the index value of the last element in array.

Can I do this? :

$#(expression)

To print the index value of the last element of an expression that
evaluates to an array? Or something like it? I can't get it to work.

If "expression" returns an array, you can do this to return the
number of elements in the array:

scalar(expression)

then all you'd have to do is subtract one to get the index of the last
element.

However, this only works if "expression" returns an array. If,
instead, it returns a list, "scalar(expression)" would return the last
element. (To find out why, you can type:

perldoc -q "difference between a list and an array"

at any DOS/Unix prompt.) Be careful about this distinction, because
lists and arrays are often treated interchangeably, but they do make a
difference here.

To get around this, you can use this to get the number of elements
of a list (it also works with arrays):

() = expression

So to get the last index of the array, you subtract 1 from it, like
this:

my $lastIndex = ( () = expression ) - 1;

Assigning an array/list to a list (even an empty one) always
returns the number of elements of the list/array on the RIGHT side of
the '=' operator. (This is slightly counter-intuitive; I would think
it would return the number of elements of the list on the LEFT side,
but it that were so then the above wouldn't work.)

I hope this helps.

-- Jean-Luc
 
T

Tad J McClellan

However, this only works if "expression" returns an array. If,
instead, it returns a list, "scalar(expression)" would return the last
element.


No, if it returns a list, then you must know which list
function "expression" represents before you can know what
will be returned in scalar context.

(and also note that it does NOT "return a list". It is not possible
to return a list into scalar context...
)


Here are 3 "expressions" that do not return the last element, for example:

--------------------
#!/usr/bin/perl
use warnings;
use strict;

my $return = scalar grep /o/, qw/one two three/;
print "$return\n";

$return = scalar reverse qw/one two three/;
print "$return\n";

$return = scalar map $_, qw/one two three/;
print "$return\n";
--------------------


Note also that the "scalar" keyword has no effect in the above code.

The assignment operator is already providing scalar context, so
an explicit scalar() is not needed.
 
J

jl_post

No, if it returns a list, then you must know which list
function "expression" represents before you can know what
will be returned in scalar context.

Oh, that's true. Some functions will return the number of elements
when used in scalar context even if they normally return a list in
list context. In that case, nothing special needs to be done other
than assigning "expression" to a scalar (or evaluating it in a scalar
context).

In the past, though, I have been burned by shoddy documentation
that said an array would be returned, when in fact it returned a
list. The code:

my $numElements = function();

should have worked, but instead it put the last element into
$numElements, which isn't what I wanted. As a result, I often use
instead:

my $numElements = () = function();

especially when the documentation makes no mention of context. That
way, if in some future revision of the code, a maintainer changed code
like:

my @array = (1, -1);
return @array;

to:

return (1, -1);

and not updated the documentation to reflect that change, my own code
would be safe.

(and also note that it does NOT "return a list". It is not possible
to return a list into scalar context...)

Sure, agreed. I didn't mean to imply that it could. What I meant
to imply is that if you call a function that normally returns a list,
you will get a scalar if you call it in scalar context. And unlike
with arrays, this scalar will not necessarily be the number of
elements. (It can be, but unless the documentation explicitly says
so, I wouldn't depend on it.)

What I DID neglect to mention were functions that behave
differently based on context (such as those that use the wantarray()
function).

-- Jean-Luc
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top