Get length of returned array without storing? - Perl

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

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14

Get length of returned array without storing?

  1. Default Get length of returned array without storing?

    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.


  2. Default Re: Get length of returned array without storing?

    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.

  3. Default Re: Get length of returned array without storing?

    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

  4. Default Re: Get length of returned array without storing?

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

  5. Default Re: Get length of returned array without storing?

    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




  6. Default Re: Get length of returned array without storing?

    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


  7. Default Re: Get length of returned array without storing?

    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


  8. Default Re: Get length of returned array without storing?

    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.

  9. Default Re: Get length of returned array without storing?

    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/"

  10. Default Re: Get length of returned array without storing?

    > ...
    > You could use an anonymous array:
    >
    > my $length = @{ $win->lbMacros1->GetSelItems() };
    > ^ ^

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


    --
    Charles DeRykus


+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. number of checkboxes (array length) returned as undefined
    By Application Development in forum Javascript
    Replies: 3
    Last Post: 09-23-2007, 02:16 PM
  2. Replies: 23
    Last Post: 08-28-2007, 06:54 PM
  3. storing string array into HDF
    By Application Development in forum Idl-pvwave
    Replies: 0
    Last Post: 07-11-2007, 12:48 AM
  4. HELP: GetFieldValue CDBVariant varbinary - wrong length returned
    By Application Development in forum ADO DAO RDO RDS
    Replies: 0
    Last Post: 01-21-2006, 09:44 AM
  5. hashed array in array need the keys... and length
    By Application Development in forum Perl
    Replies: 1
    Last Post: 08-14-2003, 01:50 PM