Ask a Question related to PERL Beginners, Design and Development.
-
Jan Eden #1
Passing several arguments
Hi all,
I have a subroutine which fills into several hashes and arrays (%author_indexlines, %poem_lines, @alphabet_index etc). How can I return those variables and pass them to another subroutine? This does not work, of course
sub read_index {
blablabla
return (%author_indexlines, %poem_lines, @alphabet_index);
}
sub write_index {
my (%author_indexlines, %poem_lines, @alphabet_index) = shift;
blablabla
}
write_index(&read_index);
I thought that this might be achieved returning references to the respective arrays and hashes and dereference them after passing, but since the elements have local scope in the read_index sub, I am not sure if this might work:
sub read_index {
blablabla
return (\%author_indexlines, \%poem_lines, \@alphabet_index);
}
sub write_index {
my ($author_ref, $poem_ref, $alphabet_ref) = shift;
my @author_index = @$author_ref;
blablabla
}
Any hints appreciated,
Jan
--
These are my principles and if you don't like them... well, I have others. - Groucho Marx
Jan Eden Guest
-
#39930 [NEW]: Error passing Word macro arguments
From: poon dot fung at gmail dot com Operating system: Windows XP PHP version: 5.2.0 PHP Bug Type: COM related Bug... -
passing arguments to functions
Hi all Problem: I want to send 2 arguments to a subroutine in the form of arrays and want to use their result which is also in the form of an... -
trouble passing arguments
I have a RedHat Linux 8.0 system. I have started the httpd (Apache 2.0.40) process and can now fetch pages from the server. I have a test htlm... -
Launching shell scripts using PERL and passing arguments
Hello All, I have a very basic question. I need to launch a Bourne shell script using PERL and automate the responses required by the script. I... -
Passing arguments to 'env ruby' on OS X
When using env on the shebang line, how to I pass arguments to Ruby under OS X? Doing the following gives me an error on OS X, but works on... -
Wiggins D'Anconia #2
Re: Passing several arguments
Jan Eden wrote:
"Not sure if it might work"... did you try? Trial and error is the best> Hi all,
>
> I have a subroutine which fills into several hashes and arrays (%author_indexlines, %poem_lines, @alphabet_index etc). How can I return those variables and pass them to another subroutine? This does not work, of course
>
> sub read_index {
> blablabla
> return (%author_indexlines, %poem_lines, @alphabet_index);
> }
>
> sub write_index {
> my (%author_indexlines, %poem_lines, @alphabet_index) = shift;
> blablabla
> }
>
> write_index(&read_index);
>
> I thought that this might be achieved returning references to the respective arrays and hashes and dereference them after passing, but since the elements have local scope in the read_index sub, I am not sure if this might work:
>
> sub read_index {
> blablabla
> return (\%author_indexlines, \%poem_lines, \@alphabet_index);
> }
>
> sub write_index {
> my ($author_ref, $poem_ref, $alphabet_ref) = shift;
> my @author_index = @$author_ref;
> blablabla
> }
>
> Any hints appreciated,
>
way to learn....
perldoc perlreftut
perldoc perlref
perldoc perldsc
perldoc perllol
Until you have a strong grasp of references make heavy use of
Data::Dumper it is your friend and will help you wrangle more complex
structures.
May I also suggest to improve your understanding of scoping issues:
[url]http://perl.plover.com/FAQs/Namespaces.html[/url]
[url]http://danconia.org[/url]
Wiggins D'Anconia Guest
-
Drieux #3
Re: Passing several arguments
On Jan 27, 2004, at 3:26 AM, Jan Eden wrote:
[..][..]> I have a subroutine which fills into several hashes and arrays
> (%author_indexlines, %poem_lines, @alphabet_index etc). How can I
> return those variables and pass them to another subroutine? This does
> not work, of course
perldoc -q "How can I pass/return a {Function, FileHandle, Array,"
remember that your core signature is
my @return_list = function(@arglist);
so the only way that you can return more than one
array or hash will be by passing their refs
my ($auther, $poem, $index) = function(@arglist);
....
sub function {
....
(\%author_indexlines, \%poem_lines, \@alphabet_index, $etc)
}
then you re-ref them as
while( my ($k,$v) = each %$author )
{
....
}
ciao
drieux
---
Drieux Guest
-
Jeff 'Japhy' Pinyan #4
Re: Passing several arguments
On Jan 27, Jan Eden said:
You most CERTAINLY want to return references to those.>I have a subroutine which fills into several hashes and arrays
>(%author_indexlines, %poem_lines, @alphabet_index etc). How can I return
>those variables and pass them to another subroutine? This does not work,
>of course
>
>sub read_index {
> blablabla
> return (%author_indexlines, %poem_lines, @alphabet_index);
> }
sub read_index {
...
return \(%author_indexlines, %poem_lines, @alphabet_index);
}
That last line is equivalent to
return (\%author_indexlines, \%poem_lines, \@alphabet_index);
That is just pure nonsense. First of all, shift() only returns ONE> sub write_index {
> my (%author_indexlines, %poem_lines, @alphabet_index) = shift;
>}
element from an array. Second, you can't assign to multiple aggregates at
once (aggregates meaning arrays and hashes). You'll need to use
references here too:
sub write_index {
my ($authref, $poemref, $alpharef) = @_;
my %author_indexlines = %$authref;
my %poem_lines = %$poemref;
my @alphabet_index = @$alpharef;
...
}
You guessed almost correctly. You still have 'shift', though. shift()>I thought that this might be achieved returning references to the
>respective arrays and hashes and dereference them after passing, but
>since the elements have local scope in the read_index sub, I am not sure
>if this might work:
will only return ONE element. Getting all of them from @_ is the way to
do it.
--
Jeff "japhy" Pinyan [email]japhy@pobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
[ I'm looking for programming work. If you like my work, let me know. ]
Jeff 'Japhy' Pinyan Guest
-
Jan Eden #5
Re: Passing several arguments
Jeff 'japhy' Pinyan wrote:
[snip]>On Jan 27, Jan Eden said:
>>>I have a subroutine which fills into several hashes and arrays
>>(%author_indexlines, %poem_lines, @alphabet_index etc). How can I
>>return those variables and pass them to another subroutine?
Ok. I noticed this problem trying to get it to work and did not rethink theway shift works. Thanks!>>>I thought that this might be achieved returning references to the
>>respective arrays and hashes and dereference them after passing,
>>but since the elements have local scope in the read_index sub, I am
>>not sure if this might work:
>You guessed almost correctly. You still have 'shift', though.
>shift() will only return ONE element. Getting all of them from @_
>is the way to do it.
>
- Jan
--
There's no place like ~/
Jan Eden Guest



Reply With Quote

