![]() |
Subs and lists
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 |
Re: Subs and lists
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 |
Re: Subs and lists
gao_bolin@voila.fr (Bolin) wrote in message news:<93c5215b.0311020733.1618bbe3@posting.google. com>...
> 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; |
Re: Subs and lists
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 |
Re: Subs and lists
Gunnar Hjalmarsson <noreply@gunnar.cc> wrote in message news:<glgpb.36154$dP1.120625@newsc.telia.net>...
> 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)). |
Re: Subs and lists
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 |
Re: Subs and lists
Gunnar Hjalmarsson <noreply@gunnar.cc> wrote in message news:<Moypb.36301$dP1.121175@newsc.telia.net>...
> 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 |
| All times are GMT. The time now is 05:04 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.