Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

more fun with references and hashes

by diarmuid (Beadle)
on May 30, 2001 at 22:46 UTC ( #84341=perlquestion: print w/replies, xml ) Need Help??
diarmuid has asked for the wisdom of the Perl Monks concerning the following question:


Again I am mystified by references and hashes. I am trying to pass a hash to a subroutine where upon I generate a new hash containing a copy of the original hash and some new dublicate information. (phew ... check out the code it's easier :-)

my (%clocks); # snip # clock is built up like this : $clocks{$pin_name}{'period'} = 12; $clocks{$pin_name}{'size'} = 63 # etc..... # # What I want to do is create a new hash with same info # and with a copy of the information eg # $new_clock{'test'}... copy into $new_clock{'test_probe'} # # Here's my attempt at that subroutine sub duplicate_probe_signals { my ($pin_hashref) = shift; my %new_hash; foreach my $pin ( keys %{$pin_hashref}){ # exclude some pins next if grep(/$pin/,@no_probe_pins); $new_hash{$pin} = $pin_hashref{$pin}; my $pin_probe = $pin."_probe"; $new_hash{$pin_probe} = $pin_hashref->$pin; } return %new_hash; }
This is wrong but can anyone point me in the right direction


Replies are listed 'Best First'.
(zdog) Re: more fun with references and hashes
by zdog (Priest) on May 30, 2001 at 22:59 UTC
    To access the hash content thorught the hash reference, you have to do:


    instead of:


    Hope that works.

    Zenon Zabinski | zdog |

      The problem seems to be this line ...
      foreach my $pin ( keys %{$pin_hashref}){
      Any ideas why (see above for error)

        The error you're getting makes me think that you're calling your subroutine like this: duplicate_probe_signals(%clocks). That will pass %clocks by value.

        Since your sub wants a reference, you should call it like this: duplicate_probe_signals(\%clocks).

Re: more fun with references and hashes
by rchiav (Deacon) on May 30, 2001 at 23:04 UTC
    Couple things I see here..

    First, if you have a refrence to a hash, doing $pin_hasref{$pin} isn't going to work. What that says is that You have a hash named %pin_hasref, and you don't. You have a scalar named $pin_hashref that is a refrence to a hash. So you need to do the following..

    $new_hash{$pin} = $pin_hashref->{$pin};
    Second, I think what you want to return is a refrence to your new hash, not just the contents.

    Hope this helps..

Re: more fun with references and hashes
by diarmuid (Beadle) on May 30, 2001 at 22:56 UTC
    Sorry ,typo:
    $new_hash{$pin} = $pin_hashref{$pin};
    should be
    $new_hash{$pin} = $pin_hashref->$pin;
    The error I am getting is
    Can't use string ("v_scanen_clk") as a HASH ref while "strict refs" 
    in use at ./ line 358.
Re: more fun with references and hashes
by malloc (Pilgrim) on May 30, 2001 at 23:34 UTC
    To my knowledge, you cannot return a hash, you have to return a reference from your sub, so return \%new_hash, and in your main code:
    my $ref = duplicate_probe_signals(\%clocks); my %new_hash = %$ref;
    Hope this helps.. -malloc
      It is possible to return a hash...
      #!perl -w use strict; use Data::Dumper; sub test { my %hash = ( a => 1, b => 2, c => 3, ); return %hash; } my %got_this = &test; print Dumper( \%got_this );
      The hash returned is simply a list that happens to have an even number of values (and unique values in the key positions 0,2,4,6,8etc.).

      "Argument is futile - you will be ignorralated!"

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://84341]
Approved by root
[Corion]: We-ird - I hadn't known this, but Ingress and Pokemon Go (both AR games in the real world where you run around) switched from using Google Maps to OpenStreetMaps, which is great. But I wonder why, as Ingress is (was?) owned by Google/Alphabet. But ...
[Corion]: ... maybe not wholly owned, or the load on Google Maps was too high?!

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2018-03-19 09:06 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (236 votes). Check out past polls.