Beefy Boxes and Bandwidth Generously Provided by pair Networks BBQ
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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:

Hi,

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

Diarmuid

Comment on more fun with references and hashes
Download Code
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 ./parse_timing_info.pl line 358.
    
(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:

    $$pin_hashref{$pin}

    instead of:

    $pin_hashref{$pin}

    Hope that works.

    Zenon Zabinski | zdog | zdog7@hotmail.com

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

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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://84341]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2014-04-21 06:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (492 votes), past polls