Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Subs and lists

Reply
Thread Tools

Subs and lists

 
 
Bolin
Guest
Posts: n/a
 
      11-02-2003
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
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-02-2003
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

 
Reply With Quote
 
 
 
 
aplasia
Guest
Posts: n/a
 
      11-02-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Bolin) wrote in message news:<(E-Mail Removed). 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;
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-02-2003
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

 
Reply With Quote
 
aplasia
Guest
Posts: n/a
 
      11-03-2003
Gunnar Hjalmarsson <(E-Mail Removed)> wrote in message news:<glgpb.36154$(E-Mail Removed)>...
> 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)).
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-03-2003
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

 
Reply With Quote
 
Nick
Guest
Posts: n/a
 
      11-03-2003
Gunnar Hjalmarsson <(E-Mail Removed)> wrote in message news:<Moypb.36301$(E-Mail Removed)>...
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM
creating files for functions and subs Gonzosez ASP General 2 01-03-2006 09:02 PM
Re: Shared (Static) Function and Subs Kevin Spencer ASP .Net 1 03-03-2004 08:27 PM
Functions vs. subs and role reversal Colin Mc Mahon ASP General 4 09-13-2003 07:28 AM
Help w/ LoadViewState and SaveViewState subs Showjumper ASP .Net 0 06-26-2003 01:38 PM



Advertisments