Get length of returned array without storing?

B

boole

This function supposedly returns an array:

[quote http://perl-win32-gui.sourceforge.net/cgi-bin/docs.cgi?doc=listbox#getselitems]
GetSelItems()

Returns an array containing the zero-based indexes of the selected
items in a multiple selection Listbox.
[/quote]

I want to get the length of that array without storing the result in
an array variable.

This is what I found:

scalar($win->lbMacros1->GetSelItems()); # returns the last VALUE (yes,
VALUE) of the array, not the length like with normal arrays.

@array = $win->lbMacros1->GetSelItems();
scalar(@array); # returns
the length of the array

I want the value of the second method, without storing the array
first.

I've tried alot including {}, [], () and eval(), but I can't figure
this one out.

Please help when you have time.
 
J

John W. Krahn

boole said:
This function supposedly returns an array:

[quote http://perl-win32-gui.sourceforge.net/cgi-bin/docs.cgi?doc=listbox#getselitems]
GetSelItems()

Returns an array containing the zero-based indexes of the selected
items in a multiple selection Listbox.
[/QUOTE]

In Perl functions/subroutines return a list not an array.

perldoc -q "What is the difference between a list and an array"

perldoc perlsub

I want to get the length of that array without storing the result in
an array variable.

This is what I found:

scalar($win->lbMacros1->GetSelItems()); # returns the last VALUE (yes,
VALUE) of the array, not the length like with normal arrays.

That is what a LIST does. Because it is NOT AN ARRAY.

@array = $win->lbMacros1->GetSelItems();
scalar(@array); # returns
the length of the array

I want the value of the second method, without storing the array
first.

I've tried alot including {}, [], () and eval(), but I can't figure
this one out.

You could use an anonymous array:

my $length = @{ $win->lbMacros1->GetSelItems() };


Or count the elements:

my $length;
$length++ for $win->lbMacros1->GetSelItems();



John
 
U

Uri Guttman

BaB> my $len = (func_call());

did you try that or is it a guess?

perl -le 'sub r {return 0,1,2} ; $s = (r()) ; print $s'
2

looks to me like the () around the call did no good. parens do not make
lists or arrays in perl. they are just used to manage precedence.

what a caller sees is dependent on what the sub returns and the calling
context.

figure out this one!

perl -le 'sub r {return 0 .. 4} ; $s = (r()) ; print $s'
1

where did that 1 come from? and it is not a bug. and yes, i know why it
prints 1. and the extra parens around the sub call are useless.

perl -le 'sub r {return 0 .. 4} ; $s = r() ; print $s'
1

uri
 
B

boole

boole said:
This function supposedly returns an array:
[quotehttp://perl-win32-gui.sourceforge.net/cgi-bin/docs.cgi?doc=listbox#ge...]
GetSelItems()

Returns an array containing the zero-based indexes of the selected
items in a multiple selection Listbox.

In Perl functions/subroutines return a list not an array.[/QUOTE]

Ah, there we go thanks for the excellent knowledge there.
perldoc -q "What is the difference between a list and an array"

perldoc perlsub

Thanks, that's the info I needed. I must remember to use perldoc more
often, I'm on Win32 so it's not as apparent.
That is what a LIST does. Because it is NOT AN ARRAY.

Right, I assumed because the documentation said it was an array, it
was an array. Thanks again.
@array = $win->lbMacros1->GetSelItems();
scalar(@array); # returns
the length of the array
I want the value of the second method, without storing the array
first.
I've tried alot including {}, [], () and eval(), but I can't figure
this one out.

You could use an anonymous array:

my $length = @{ $win->lbMacros1->GetSelItems() };

Or count the elements:

my $length;
$length++ for $win->lbMacros1->GetSelItems();

Great, that's the ticket, thanks John, you've answered my question and
explained the reason, perfect.
 
J

jl_post

This function supposedly returns an array:

[quotehttp://perl-win32-gui.sourceforge.net/cgi-bin/docs.cgi?doc=listbox#ge...]
GetSelItems()

Returns an array containing the zero-based indexes of the selected
items in a multiple selection Listbox.

I want to get the length of that array without storing the result in
an array variable.

This is what I found:

scalar($win->lbMacros1->GetSelItems()); # returns the last VALUE[/QUOTE]


Dear Boole,

I am under the impression that ::GetSelItems() returns a list
instead of an array, despite what the documentation might say. The
reason I think this is because the following code:

sub returnList { return ('a', 'b', 'c'); }
print scalar(returnList()); # prints 'c'

would print 'c', while the following code:

sub returnArray { my @a = ('a', 'b', 'c'); return @a; }
print scalar(returnArray()); # prints 3

would print '3'. The fact that calling scalar() on ::GetSelItems()
returns the last value of a list tells me that it's returning a list
(and not an array). This is either an error in the documentation or
an error in the code. (I would guess that it's an error in the code,
since it would make more sense to me if the function returns the
length in scalar context, as I would think that's more useful than
returning the last element. But I digress.)

I can't remember where I read this, but you can easily get the
length of a list in scalar context by assigning the list to an empty
list. So instead of writing:

my $length = ('a', 'b', 'c'); # $length is 'c'

you'd write:

my $length = () = ('a', 'b', 'c'); # $length is 3

So instead of your line:

# Returns the last VALUE of the return list:
scalar($win->lbMacros1->GetSelItems());

tweak it to by assigning the call to an empty list:

# Returns the LENGTH of the return list:
scalar( () = $win->lbMacros1->GetSelItems() );

That should do exactly what you want in that you don't need to
store the return list into a temporary array to extract the length.

Note that this technique will also work on arrays, too, like this:

my @a = ('a' .. 'z');
my $length = () = @a; # $length is 26

although since arrays already return their length in scalar context, I
don't see why you wouldn't just use the simpler line:

my $length = @a; # $length is 26

and avoid the "() = " usage altogether.

But if want to get the length of a list returned from a function, I
found that using "() = f()" in scalar context is a simple way of doing
it.

I hope this helps, boole.

-- Jean-Luc Romano
 
X

xhoster

Big and Blue said:
my $len = (func_call());

No, that does the same thing as not using the extra parens. Maybe you
mean this:

my $len = () = func_call();

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
T

Tad McClellan

you can easily get the
length of a list in scalar context


No you can't.

A list cannot even exist in a scalar context.

you'd write:

my $length = () = ('a', 'b', 'c'); # $length is 3


But now the list is _not_ in a scalar context. You have put the
list into a list context.
 
C

comp.llang.perl.moderated

...
You could use an anonymous array:

my $length = @{ $win->lbMacros1->GetSelItems() };
^ ^
I'm sure you meant: @{[$win->lbMacros1->GetSelItems()]};
 
J

jl_post

No you can't.

A list cannot even exist in a scalar context.

you can easily get the length of a list in scalar context

I didn't mean:
you can easily get the length of [a list in scalar context]

but rather:
you can easily get the [length of a list] in scalar context

or even:
[when in scalar context (such as assigning to a scalar
or using scalar()),] you can easily get the length
of a list

I didn't mean to imply that the list itself was in scalar context,
but just the final operation was (which in the case of:

my $length = () = ('a', 'b', 'c'); # $length is 3

would be the left-most '=' operator).

I can see how the way I worded that might be a bit ambiguous, but I
do think my examples clarified what I meant (and more importantly,
answered the original poster's question).

-- Jean-Luc
 
U

Uri Guttman

BaB> Thanks - that's the effect I was looking for (so Uri - a pointer in
BaB> the right direction, rather than a guess - although it was untested).

i don't like the () = trick anyhow. also i never seem to want the length
of lists. i always want the data. maybe i think differently but temp
lists just to generate counts seems like a waste.

BaB> Of course, if the function itself checked its context and just
BaB> return the length in a scalar context then this all becomes
BaB> irrelevant. I was assuming it doesn't and won't.

that is also a poor assumption. you can check the context and return
many things besides the count in scalar context. i have modules that
would return an array ref of the data instead of a list/array
itself. some code may return some other info like a handle or object in
scalar context that may be used in chaining calls (a style i don't like
either). it is up to the module designer and not the users of the module
that determines what is returned.

uri
 
B

Brian McCauley

BaB> I was assuming it doesn't and won't.
that is also a poor assumption.

Actually, I think it's more likely to be affected use of English.

I suspect BaB wrote "I was assuming it doesn't" when he meant "I
wasn't assuming it does".

This is rather an unfortunately common affectation in the general
population.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top