Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Passing hash to a subroutine

Reply
Thread Tools

Passing hash to a subroutine

 
 
Ray
Guest
Posts: n/a
 
      04-07-2007
Hi all,

I've been trying to figure out how to pass a hash (and a hash of
hashes) into a function. After some fiddling around and reading
perlreftut, I think I got it. However, I turned on "use strict" and
it is warning me that what I am doing is "deprecated" and may not be
supported in the future. So, even though it works (I mean...I get the
right answer), can someone tell me what the supported way?

I made a small example to demonstrate my problem:

-----
#!/usr/bin/perl -w
use strict;
use diagnostics;

my %cities;
my %states;

sub printStates {
my $href = shift @_;

## Next line is deprecated
printf (STDOUT "* %s\n", %{$href} -> {"New York"});
}

sub printCities {
my $href = shift @_;

## Next line is also deprecated
printf (STDOUT "* %s\n", %{$href} -> {"San Francisco"}{"LA"});
}

$cities{"San Francisco"}{"LA"} = "near";
$cities{"San Francisco"}{"New York"} = "far";
$states{"San Francisco"} = "California";
$states{"New York"} = "New York";

printf (STDOUT "%s\n", $cities{"San Francisco"}{"LA"});
printf (STDOUT "%s\n", $cities{"San Francisco"}{"New York"});

printf (STDOUT "%s\n", $states{"San Francisco"});
printf (STDOUT "%s\n", $states{"New York"});

printf (STDOUT "%s\n", \%states);

printStates (\%states);
printCities (\%cities);
-----

The warnings are:

Using a hash as a reference is deprecated at ./foo.pl line 11 (#1)
(D deprecated) You tried to use a hash as a reference, as in
%foo->{"bar"} or %$ref->{"hello"}. Versions of perl <= 5.6.1
used to allow this syntax, but shouldn't have. It is now
deprecated, and will
be removed in a future version.

Using a hash as a reference is deprecated at ./foo.pl line 17 (#1)

which are good in telling me what I did wrong, but I have no clue on
how to correct it. Any ideas?

Thanks in advance!

Ray

 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      04-07-2007
Ray <(E-Mail Removed)> wrote:
> I've been trying to figure out how to pass a hash (and a hash of
> hashes) into a function. After some fiddling around and reading
> perlreftut, I think I got it. However, I turned on "use strict" and
> it is warning me that what I am doing is "deprecated" and may not be
> supported in the future. So, even though it works (I mean...I get the
> right answer), can someone tell me what the supported way?


> I made a small example to demonstrate my problem:


> -----
> #!/usr/bin/perl -w
> use strict;
> use diagnostics;


> my %cities;
> my %states;


> sub printStates {
> my $href = shift @_;


A simple

my $href = shift;

will also do.

> ## Next line is deprecated
> printf (STDOUT "* %s\n", %{$href} -> {"New York"});


That looks rather strange (and, yes, it's deprecated). Just use

printf (STDOUT "* %s\n", $href->{"New York"});

It looks simpler and makes the warning go away

When you have a hash you access the values the reference points to
using the '->' after the reference (the same holds if you have an
array reference). E.g.

my %h = (a => 1, b => 2);
my $hr = \%h;
print "$h{a} == $hr->{a}\n";

my @a = (1, 2);
my $ar = \@a;
print "$a[0] == $ar->[0]\n";

Regards, Jens
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      04-07-2007
Ray <(E-Mail Removed)> wrote:


> I've been trying to figure out how to pass a hash (and a hash of
> hashes) into a function.



You *have* figured out how to pass a hash into a function.

What you haven't figured out is how to dereference it within
the function body.


> After some fiddling around and reading
> perlreftut, I think I got it.



perlreftut describes two different ways of dereferencing.

You are applying *both* ways.


> can someone tell me what the supported way?



Deferencing once, using either way, is the supported way.


> sub printStates {
> my $href = shift @_;
>
> ## Next line is deprecated
> printf (STDOUT "* %s\n", %{$href} -> {"New York"});
> }



printf (STDOUT "* %s\n", ${$href}{"New York"}); # Use Rule 1
or
printf (STDOUT "* %s\n", $href->{"New York"}); # Use Rule 2


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Ray
Guest
Posts: n/a
 
      04-09-2007

Hi Michele,

On Apr 7, 10:35 pm, Michele Dondi <(E-Mail Removed)> wrote:
> On 7 Apr 2007 04:46:25 -0700, "Ray" <(E-Mail Removed)> wrote:
>
> >I made a small example to demonstrate my problem:

>
> In my first post I addressed your immediate problem. There are more
> issues with your code, though. I will briefly go through them now.


Yes, thank you that! It was very helpful! And for cleaning up my
Perl code. I've been doing a few things in Perl which compile but
aren't very Perl-ish and am changing at a very slow pace. So, thanks!

> > ## Next line is also deprecated
> > printf (STDOUT "* %s\n", %{$href} -> {"San Francisco"}{"LA"});


And you also caught me. I am originally a C-programmer and changing to
a Perl programming means my Perl code sometimes looks like C. Thanks
again -- I'll use what you suggested from now on.

Ray


 
Reply With Quote
 
Ray
Guest
Posts: n/a
 
      04-09-2007

Hi Tad,

On Apr 7, 11:13 pm, Tad McClellan <(E-Mail Removed)> wrote:
> Ray <(E-Mail Removed)> wrote:
> > I've been trying to figure out how to pass a hash (and a hash of
> > hashes) into a function.

>
> You *have* figured out how to pass a hash into a function.
>
> What you haven't figured out is how to dereference it within
> the function body.


Yes, that's what I was thinking.

> perlreftut describes two different ways of dereferencing.
>
> You are applying *both* ways.


Ah! Thank you. I was just looking back at perlreftut to see where I
went wrong. I guess read @a and @{$aref} were equivalent and
mistakenly kept modifying this until it worked instead of looking at
it more carefully. My biggest mistake was thinking that references
were somehow different if I passed it in a function, but it doesn't
seem to be.

Thanks again!

Ray


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      04-09-2007
Ray <(E-Mail Removed)> wrote:
> On Apr 7, 11:13 pm, Tad McClellan <(E-Mail Removed)> wrote:


>> What you haven't figured out is how to dereference it within
>> the function body.


> I was just looking back at perlreftut to see where I
> went wrong. I guess read @a and @{$aref} were equivalent and



And they *are* equivalent!

But you didn't want the whole aggregate (array above but hash earlier).

You wanted to index into the aggregate, so you needed the
equivalent of $a[], which is ${$aref}[].


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Ray
Guest
Posts: n/a
 
      04-19-2007

Hi Tad,

On Apr 9, 8:55 pm, Tad McClellan <(E-Mail Removed)> wrote:
> Ray <(E-Mail Removed)> wrote:
> > On Apr 7, 11:13 pm, Tad McClellan <(E-Mail Removed)> wrote:
> >> What you haven't figured out is how to dereference it within
> >> the function body.

> > I was just looking back at perlreftut to see where I
> > went wrong. I guess read @a and @{$aref} were equivalent and

>
> And they *are* equivalent!
>
> But you didn't want the whole aggregate (array above buthashearlier).
>
> You wanted to index into the aggregate, so you needed the
> equivalent of $a[], which is ${$aref}[].


I see -- I guess the equivalence between the two wasn't immediately
obvious to me. They look so different -- but thanks for this!

Ray


 
Reply With Quote
 
Ray
Guest
Posts: n/a
 
      04-19-2007

Hi Michele,

On Apr 9, 6:02 pm, Michele Dondi <(E-Mail Removed)> wrote:
> On 8 Apr 2007 21:23:43 -0700, "Ray" <(E-Mail Removed)> wrote:
>
> >> > printf (STDOUT "* %s\n", %{$href} -> {"San Francisco"}{"LA"});

>
> >And you also caught me. I am originally a C-programmer and changing to
> >a Perl programming means my Perl code sometimes looks like C. Thanks

>
> Well... that is, in $Larry's words, "officially ok". But using a
> simple print() where it suffices has obvious advantages. It's much
> like when you learn a foreign language and it's fine if you initially
> use its constructs that most resemble your mother tongue, even if
> they're obsolete or unusual; but as you learn more and more you switch
> to more idiomatic ones.


Interesting analogy. Alas, it's hard to break old habits, especially
if your first language (C or English ) has been with you for so
long. But not impossible; hopefully not yet old enough to not being
able to learn new tricks! Thanks!

Ray


 
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
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
use one subroutine's variable value in another subroutine inside a module. king Perl Misc 5 04-29-2007 06:39 AM
"too many arguments" passing hash reference to subroutine Dean Perl Misc 3 10-06-2006 11:55 AM
Options for passing Hash to a subroutine. chung.ley@amd.com Perl 6 04-04-2005 04:47 AM
Trouble Passing Array Containing Hash to Subroutine Scott Perl Misc 10 12-07-2004 12:51 PM



Advertisments