Get length of returned array without storing?

Get length of returned array without storing?

am 01.11.2007 01:36:28 von George Cooke

This function supposedly returns an array:

[quote http://perl-win32-gui.sourceforge.net/cgi-bin/docs.cgi?doc=l istbox#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.

Re: Get length of returned array without storing?

am 01.11.2007 01:57:15 von Big and Blue

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

my $len = (func_call());



--
Just because I've written it doesn't mean that
either you or I have to believe it.

Re: Get length of returned array without storing?

am 01.11.2007 02:45:16 von krahnj

boole wrote:
>
> This function supposedly returns an array:
>
> [quote http://perl-win32-gui.sourceforge.net/cgi-bin/docs.cgi?doc=l istbox#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
--
use Perl;
program
fulfillment

Re: Get length of returned array without storing?

am 01.11.2007 04:31:49 von Uri Guttman

>>>>> "BaB" == Big and Blue writes:

BaB> boole wrote:
>>
>> I want to get the length of that array without storing the result in
>> an array variable.

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

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Re: Get length of returned array without storing?

am 01.11.2007 12:22:40 von George Cooke

On Nov 1, 1:45 am, "John W. Krahn" wrote:
> boole wrote:
>
> > 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.
> > [/quote]
>
> In Perl functions/subroutines return a list not an array.

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.

>
> > 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.

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.

>
> John
> --
> use Perl;
> program
> fulfillment

Re: Get length of returned array without storing?

am 01.11.2007 13:51:17 von it_says_BALLS_on_your forehead

On Oct 31, 11:31 pm, Uri Guttman wrote:
.. parens do not make
> lists or arrays in perl. they are just used to manage precedence.
>

would it be accurate to say that parens provide list context to lvalues

Re: Get length of returned array without storing?

am 01.11.2007 16:02:02 von jl_post

On Oct 31, 6:36 pm, boole wrote:
>
> 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.
> [/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


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

Re: Get length of returned array without storing?

am 01.11.2007 18:56:50 von xhoster

Big and Blue wrote:
> boole wrote:
> >
> > I want to get the length of that array without storing the result in
> > an array variable.
>
> 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.

Re: Get length of returned array without storing?

am 01.11.2007 23:49:33 von Tad McClellan

jl_post@hotmail.com wrote:


> 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.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: Get length of returned array without storing?

am 01.11.2007 23:53:31 von Charles DeRykus

> ...
> You could use an anonymous array:
>
> my $length = @{ $win->lbMacros1->GetSelItems() };
> ^ ^
I'm sure you meant: @{[$win->lbMacros1->GetSelItems()]};


--
Charles DeRykus

Re: Get length of returned array without storing?

am 02.11.2007 00:15:41 von Big and Blue

xhoster@gmail.com wrote:
> Big and Blue wrote:
>> boole wrote:
>>> I want to get the length of that array without storing the result in
>>> an array variable.
>> my $len = (func_call());
>
> No, that does the same thing as not using the extra parens. Maybe you
> mean this:
>
> my $len = () = func_call();

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

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



--
Just because I've written it doesn't mean that
either you or I have to believe it.

Re: Get length of returned array without storing?

am 02.11.2007 00:40:51 von jl_post

jl_post@hotmail.com wrote:
> > you can easily get the
> > length of a list in scalar context

On Nov 1, 4:49 pm, Tad McClellan wrote:
> No you can't.
>
> A list cannot even exist in a scalar context.


This might be just a matter of semantics, but when I wrote:

> 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

Re: Get length of returned array without storing?

am 02.11.2007 02:39:00 von Uri Guttman

>>>>> "BaB" == Big and Blue writes:

BaB> xhoster@gmail.com wrote:
>> Big and Blue wrote:
>>> boole wrote:
>>>> I want to get the length of that array without storing the result in
>>>> an array variable.
>>> my $len = (func_call());
>> No, that does the same thing as not using the extra parens. Maybe you
>> mean this:
>> my $len = () = func_call();

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

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Re: Get length of returned array without storing?

am 06.11.2007 18:49:58 von nobull67

On Nov 2, 1:39 am, Uri Guttman wrote:
> >>>>> "BaB" == Big and Blue writes:

> 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.