Subs and lists

Discussion in 'Perl' started by Bolin, Nov 2, 2003.

  1. Bolin

    Bolin Guest

    How to modify a list in a subfunction? E.g. I want to pass a list as
    an argument, and append it another list? Something like

    sub append {
    my $listRef = shift;
    my @list = @$listref;

    @list = (@list, @anotherlist);

    return \@list;
    }

    Right now I have to return the pointer to make it work, can I find a
    way to modify the imput argument without having to return the result,
    or is that against Perl's coding style?

    Another thing, I am also looking for an elegent way of initializing a
    hash table with two lists, or a list an a value, e.g. sth like
    @hash{@keyList} = @valueList or @hash{@keyList} = $value, which does
    not work.

    THanks

    Bolin
     
    Bolin, Nov 2, 2003
    #1
    1. Advertising

  2. Please don't start new threads in this group. It's defunct. Use
    comp.lang.perl.misc instead.

    Bolin wrote:
    > How to modify a list in a subfunction? E.g. I want to pass a list
    > as an argument, and append it another list? Something like
    >
    > sub append {
    > my $listRef = shift;
    > my @list = @$listref;
    >
    > @list = (@list, @anotherlist);
    >
    > return \@list;
    > }
    >
    > Right now I have to return the pointer to make it work, can I find
    > a way to modify the imput argument without having to return the
    > result,


    Sure. Just don't assign the dereferenced reference to a new variable:

    sub append {
    my $listRef = shift;
    push @$listRef, @anotherlist;
    }

    > or is that against Perl's coding style?


    Not as far as I know.

    > Another thing, I am also looking for an elegent way of initializing
    > a hash table with two lists, or a list an a value, e.g. sth like
    > @hash{@keyList} = @valueList or @hash{@keyList} = $value, which
    > does not work.


    Can't see why the first example would "not work". You'd better post
    some illustrative complete code.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Nov 2, 2003
    #2
    1. Advertising

  3. Bolin

    aplasia Guest

    (Bolin) wrote in message news:<>...
    > Another thing, I am also looking for an elegent way of initializing a
    > hash table with two lists, or a list an a value, e.g. sth like
    > @hash{@keyList} = @valueList or @hash{@keyList} = $value, which does
    > not work.


    (1) (@rlist = reverse @valueList) and (%hash = map {$_ , pop @rlist} @keyList);

    (2) %hash = map {$_ , $value} @keyList;
     
    aplasia, Nov 2, 2003
    #3
  4. aplasia wrote:
    >
    > (1) (@rlist = reverse @valueList) and (%hash = map {$_ , pop @rlist} @keyList);


    Out of curiosity: What's wrong with populating a hash slice with a
    list, just as OP suggested?

    @hash{@keyList} = @valueList;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Nov 2, 2003
    #4
  5. Bolin

    aplasia Guest

    Gunnar Hjalmarsson <> wrote in message news:<glgpb.36154$>...
    > aplasia wrote:
    > >
    > > (1) (@rlist = reverse @valueList) and (%hash = map {$_ , pop @rlist} @keyList);

    >
    > Out of curiosity: What's wrong with populating a hash slice with a
    > list, just as OP suggested?
    >
    > @hash{@keyList} = @valueList;


    (1) @list1=("lip","lip");
    @list2=("stick");
    @hash{@list1}=@list2;

    Then $hash{"lip"}'s value will be undefined.

    (2) @hash{@keyList}=($value) x @keyList;

    also populates %hash with $value (TMTOWTDI)

    (3) better use

    @rlist = reverse @valueList;
    %hash = map{$_ , pop @rlist} @keyList;

    in case @valueList could be empty and you wanna set values
    to be undefined (hence "exists $hash{$key}" yielding true for all
    $key in @keyList (even if the value of $key is undefined)).
     
    aplasia, Nov 3, 2003
    #5
  6. aplasia wrote:
    > Gunnar Hjalmarsson wrote:
    >>aplasia wrote:
    >>
    >>>(1) (@rlist = reverse @valueList) and (%hash = map {$_ , pop @rlist} @keyList);

    >>
    >>Out of curiosity: What's wrong with populating a hash slice
    >>with a list, just as OP suggested?
    >>
    >> @hash{@keyList} = @valueList;

    >
    > (1) @list1=("lip","lip");
    > @list2=("stick");
    > @hash{@list1}=@list2;
    >
    > Then $hash{"lip"}'s value will be undefined.


    Yes, but so it would with your construct as well.

    perldoc -f pop
    "If there are no elements in the array, returns the undefined value..."

    <snip>

    > in case @valueList could be empty and you wanna set values to
    > be undefined (hence "exists $hash{$key}" yielding true for all
    > $key in @keyList (even if the value of $key is undefined)).


    Sure, but that's true with

    @hash{@keyList} = @valueList;

    as well.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Nov 3, 2003
    #6
  7. Bolin

    Nick Guest

    Gunnar Hjalmarsson <> wrote in message news:<Moypb.36301$>...
    > aplasia wrote:
    > > Gunnar Hjalmarsson wrote:
    > >>aplasia wrote:
    > >>
    > >>>(1) (@rlist = reverse @valueList) and (%hash = map {$_ , pop @rlist} @keyList);
    > >>
    > >>Out of curiosity: What's wrong with populating a hash slice
    > >>with a list, just as OP suggested?
    > >>
    > >> @hash{@keyList} = @valueList;

    > >
    > > (1) @list1=("lip","lip");
    > > @list2=("stick");
    > > @hash{@list1}=@list2;
    > >
    > > Then $hash{"lip"}'s value will be undefined.

    >
    > Yes, but so it would with your construct as well.
    >
    > perldoc -f pop
    > "If there are no elements in the array, returns the undefined value..."
    >
    > <snip>
    >
    > > in case @valueList could be empty and you wanna set values to
    > > be undefined (hence "exists $hash{$key}" yielding true for all
    > > $key in @keyList (even if the value of $key is undefined)).

    >
    > Sure, but that's true with
    >
    > @hash{@keyList} = @valueList;
    >
    > as well.



    of course, the construct @hash{@keyList} = @valueList;
    works perfectly well
     
    Nick, Nov 3, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Showjumper
    Replies:
    0
    Views:
    4,406
    Showjumper
    Jun 26, 2003
  2. Kevin Spencer

    Re: Shared (Static) Function and Subs

    Kevin Spencer, Mar 3, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    364
    Carlos Campos
    Mar 3, 2004
  3. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    410
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  4. Colin Mc Mahon

    Functions vs. subs and role reversal

    Colin Mc Mahon, Sep 12, 2003, in forum: ASP General
    Replies:
    4
    Views:
    118
    Chris Barber
    Sep 13, 2003
  5. Gonzosez

    creating files for functions and subs

    Gonzosez, Jan 3, 2006, in forum: ASP General
    Replies:
    2
    Views:
    121
    Tim Slattery
    Jan 3, 2006
Loading...

Share This Page