Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > hashes question

Reply
Thread Tools

hashes question

 
 
ed
Guest
Posts: n/a
 
      09-20-2006
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.
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      09-20-2006
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
 
Reply With Quote
 
 
 
 
David Squire
Guest
Posts: n/a
 
      09-20-2006
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


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      09-21-2006
David Squire <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Ch Lamprecht
Guest
Posts: n/a
 
      09-21-2006
Tad McClellan wrote:
> David Squire <(E-Mail Removed)> 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:

^^^^^^^^^^^^^^^^^^^^^^^^^^^^


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

--

perl -e "print scalar reverse q/(E-Mail Removed)/"
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      09-21-2006
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.

--
Affijn, Ruud

"Gewoon is een tijger."


 
Reply With Quote
 
Ch Lamprecht
Guest
Posts: n/a
 
      09-21-2006
Dr.Ruud wrote:
> 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.
>


Of course you are right.

s/control/punctuation/


--

perl -e "print scalar reverse q/(E-Mail Removed)/"
 
Reply With Quote
 
ed
Guest
Posts: n/a
 
      09-21-2006
On Wed, 20 Sep 2006 22:31:53 GMT
"John W. Krahn" <(E-Mail Removed)> wrote:

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



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.


--
Regards, Ed :: http://www.linuxwarez.co.uk
proud bash person
When Chuck Norris roundhouse kicks people, they do not die of blunt
trauma or tissue damage. They simple lose their will to live.
 
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
How to make an array of hashes to a single array with all thevalues of these hashes ? kazaam Ruby 12 09-13-2007 01:30 PM
using hashes as keys in hashes Steven Arnold Ruby 3 11-23-2005 03:25 PM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM
Question about hashes of hashes djacober Perl Misc 10 07-25-2005 10:01 AM
Hashes of Hashes via subs Ben Holness Perl 8 10-08-2003 06:57 AM



Advertisments