473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
Jul 19 '05 #1
6 2320
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

Jul 19 '05 #2
ga*******@voila.fr (Bolin) wrote in message news:<93**************************@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;
Jul 19 '05 #3
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

Jul 19 '05 #4
Gunnar Hjalmarsson <no*****@gunnar.cc> wrote in message news:<gl********************@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)).
Jul 19 '05 #5
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

Jul 19 '05 #6
Gunnar Hjalmarsson <no*****@gunnar.cc> wrote in message news:<Mo********************@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
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Colin Mc Mahon | last post by:
Hi all, First off I know the difference between functions and subs, basically a function returns a value on execution of a code block wheras a sub just executes a code block (more or less). I...
8
by: | last post by:
In global.asa I have some code outside all the Subs. I have some confirmation that it is being executed OnStart. Yet I can's see if it is executed OnEnd. The literature I have says that OnEnd...
13
by: Steven Scaife | last post by:
I have decided to re-write the intranet site i created over a year ago. The coding is pretty awful and hard to read cos I made the mistake of not putting comments in or putting crappy comments in...
2
by: nakhi | last post by:
Hi, I declared a dataset outside any subs, then I fill the dataset with a tables in a Private Sub1, then I want to take the data out from the dataset in Sub2. like . sub2()...
7
by: djc | last post by:
I have several subroutines (all inline code) that wind up using the same database connection object variable. I have been declaring a new variable in every sub. I just now came to a point where I...
11
by: Nicky Smith | last post by:
Hello, I'm studying a book on VB.net Win apps, and I'm reading a section on events and delegates and raising events. Is it just me, or is this not just subs dressed up as something else? I...
5
by: Brett | last post by:
In a class, I have several Private subs. I declare an instance of the class such as: Dim MySelf as new Class1 within a private sub. The motive is to provide access to other subs within the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.