From: ed on
I'm having a bit of trouble sending hash elements to sub routines and
back.

Any help appreciated!

sub entry {
my $l = shift;
my %h = %_;

while( ( my $k, my $v ) = each ( %h ) ) {
print( "K: $k V: $v\n" );
}
return(%h);
}

$h{'stuff'} = "hello";
%h = entry( "1", \%h );

When run %h becomes empty in entry.

--
Regards, Ed :: http://www.s5h.net
proud unix hacker
Mr. T cannot be pitied. Mr. T is most often envied, admired or
feared. Once, Mr. T was even ignored. That fool has since been
nothing but pitied.
From: John W. Krahn on
ed wrote:
> I'm having a bit of trouble sending hash elements to sub routines and
> back.
>
> Any help appreciated!
>
> sub entry {
> my $l = shift;
> my %h = %_;
>
> while( ( my $k, my $v ) = each ( %h ) ) {
> print( "K: $k V: $v\n" );
> }
> return(%h);
> }
>
> $h{'stuff'} = "hello";
> %h = entry( "1", \%h );
>
> When run %h becomes empty in entry.

You are calling entry() with a hash reference so you have to dereference it
inside the sub:

sub entry {
my $l = shift;
my $h = shift;

while( my ( $k, $v ) = each ( %$h ) ) {
print( "K: $k V: $v\n" );
}
return %h;
}




John
--
use Perl;
program
fulfillment
From: Abigail on
ed (ed(a)noreply.com) wrote on MMMMDCCLXVIII September MCMXCIII in
<URL:news:20060920232203.442362b3(a)localhost.localdomain>:
<> I'm having a bit of trouble sending hash elements to sub routines and
<> back.
<>
<> Any help appreciated!
<>
<> sub entry {
<> my $l = shift;
<> my %h = %_;
<>
<> while( ( my $k, my $v ) = each ( %h ) ) {
<> print( "K: $k V: $v\n" );
<> }
<> return(%h);
<> }
<>
<> $h{'stuff'} = "hello";
<> %h = entry( "1", \%h );
<>
<> When run %h becomes empty in entry.


Well, yes. %_ is a non-magical hash, which lives in the package main.
It's most likely to be empty, and inside, you initialize %h with it.

Perhaps you want something like:

my %h = %{+shift};

as the second line in your subroutine.

Alternatively, you can do:

my ($l, $h) = @_;

and use %$h where you now use %h.



Abigail
--
perl -we 'print q{print q{print q{print q{print q{print q{print q{print q{print
qq{Just Another Perl Hacker\n}}}}}}}}}' |\
perl -w | perl -w | perl -w | perl -w | perl -w | perl -w | perl -w | perl -w
From: David Squire on
ed wrote:
> I'm having a bit of trouble sending hash elements to sub routines and
> back.
>
> Any help appreciated!
>
> sub entry {
> my $l = shift;
> my %h = %_;

Hmmm. Is there such a variable s %_? It doesn't appear in perldoc perlvar.

>
> while( ( my $k, my $v ) = each ( %h ) ) {
> print( "K: $k V: $v\n" );
> }
> return(%h);
> }
>
> $h{'stuff'} = "hello";
> %h = entry( "1", \%h );

here you pass a *reference* to the hash %h. If you pass a reference
(which is a sensible thing to do for complex datastructures), you need
to treat it as a reference in the subroutine. Also, there is no need to
assign the subroutine return value to the hash, since if you pass a
reference, the subroutine will use the same hash (though yours does not
modify it, so you don't need to return anything.

For example:

----

#!/usr/bin/perl

use strict;
use warnings;

sub entry {
my $l = shift;
my $h_ref = shift;

while( ( my $k, my $v ) = each ( %$h_ref ) ) {
print( "K: $k V: $v\n" );
}
# Let's modify the hash while we're here
$$h_ref{'more stuff'} = 'nonsense';
}

my %h;
$h{'stuff'} = "hello";
entry( "1", \%h );

foreach my $key (keys %h) {
print "$key: $h{$key}\n";
}

----

Output:

K: stuff V: hello
stuff: hello
more stuff: nonsense

----


DS


From: Tad McClellan on
David Squire <David.Squire(a)no.spam.from.here.au> wrote:
> ed wrote:

>> my %h = %_;
>
> Hmmm. Is there such a variable s %_?


Yes there is. (but it doesn't do anything for the OP.)

There is a $_ variable, so then there is also @_ and %_ (and
a few more) variables.


eg: Since there is a $@ variable, this works fine, even with strictures:

-----------------
#!/usr/bin/perl
use warnings;
use strict;

@@ = qw/foo bar/;
print "$_\n" for @@;

%@ = qw/foo FOO bar BAR/;
print "$_ => $@{$_}\n" for keys %@;
-----------------


> It doesn't appear in perldoc perlvar.


Then it doesn't do anything special.

(but that does not mean that you cannot use it.)


--
Tad McClellan SGML consulting
tadmc(a)augustmail.com Perl programming
Fort Worth, Texas