hashes question

E

ed

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

John W. Krahn

ed said:
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
 
D

David Squire

ed said:
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
 
T

Tad McClellan

David Squire said:
ed wrote:

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.)
 
C

Ch Lamprecht

Tad said:
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:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^


perlvar:

Perl identifiers that begin with digits, control characters, or punctuation
characters are exempt from the effects of the package declaration and are always
forced to be in package main; they are also exempt from strict 'vars' errors.

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

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

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

so this will work with any variable name starting with a control character:

use warnings;
use strict;

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

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



Christoph
 
D

Dr.Ruud

Ch Lamprecht schreef:
so this will work with any variable name starting with a
control character:

use warnings;
use strict;

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

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

I see no "variable name starting with a
control character" in your example.
 
C

Ch Lamprecht

Dr.Ruud said:
Ch Lamprecht schreef:




I see no "variable name starting with a
control character" in your example.

Of course you are right.

s/control/punctuation/
 
E

ed

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


Thank you all very much, Abigail, David and Tad. Problem solved. This
was a very difficult problem for me, I would never have solved it alone.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top