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.
This is a discussion on Get length of returned array without storing? - Perl ; This function supposedly returns an array: [quote http://perl-win32-gui.sourceforge.ne...x#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 ...
This function supposedly returns an array:
[quote http://perl-win32-gui.sourceforge.ne...x#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.
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.
boole wrote:
>
> This function supposedly returns an array:
>
> [quote http://perl-win32-gui.sourceforge.ne...x#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
>>>>> "BaB" == Big and Blue <No_4@dsl.pipex.com> 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
On Nov 1, 1:45 am, "John W. Krahn" <kra...@telus.net> 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
On Oct 31, 11:31 pm, Uri Guttman <u...@stemsystems.com> 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
On Oct 31, 6:36 pm, boole <geo...@gmail.com> 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
Big and Blue <No_4@dsl.pipex.com> 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.
jl_post@hotmail.com <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/"
> ...
> You could use an anonymous array:
>
> my $length = @{ $win->lbMacros1->GetSelItems() };
> ^ ^
I'm sure you meant: @{[$win->lbMacros1->GetSelItems()]};
--
Charles DeRykus