Subs and lists

B

Bolin

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
 
G

Gunnar Hjalmarsson

Please don't start new threads in this group. It's defunct. Use
comp.lang.perl.misc instead.
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.
 
A

aplasia

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;
 
G

Gunnar Hjalmarsson

aplasia said:
(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;
 
A

aplasia

Gunnar Hjalmarsson said:
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)).
 
G

Gunnar Hjalmarsson

aplasia said:
(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..."

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

Nick

Gunnar Hjalmarsson said:
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..."



Sure, but that's true with

@hash{@keyList} = @valueList;

as well.


of course, the construct @hash{@keyList} = @valueList;
works perfectly well
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top