Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

References with hashes

by brayshakes (Acolyte)
on Feb 15, 2007 at 04:34 UTC ( [id://600130]=perlquestion: print w/replies, xml ) Need Help??

brayshakes has asked for the wisdom of the Perl Monks concerning the following question:

I am having trouble with printing references.
use strict; use warnings; my $address{Bray} = { Name => 'Braden Mailloux', Str => 'kapellenstr. 1', EMail => 'brayshakes@gmail.com' }; my $email = $address{Bray}->{EMail}; print_address($address{Bray});

# perl data_structure_refs.pl syntax error at data_structure_refs.pl line 4, near "$address{Bray" Execution of data_structure_refs.pl aborted due to compilation errors.

I only changed the values from a tutorial, so this is
straight out of the pages. I am wondering if the tutorial has some bugs?

Edit: g0n - code tags

Replies are listed 'Best First'.
Re: References with hashes
by chargrill (Parson) on Feb 15, 2007 at 04:50 UTC

    This simple change gets your program closer to working:

    #!/usr/bin/perl use strict; use warnings; my %address; $address{Bray} = { Name => 'Braden Mailloux', Str => 'kapellenstr. 1', EMail => 'brayshakes@gmail.com' }; my $email = $address{Bray}->{EMail}; print_address($address{Bray});

    After this change, running it only gives the error "Undefined subroutine &main::print_address called at bray.pl line 17."

    You could also do this:

    #!/usr/bin/perl use strict; use warnings; my %address = ( Bray => { Name => 'Braden Mailloux', Str => 'kapellenstr. 1', EMail => 'brayshakes@gmail.com' }); my $email = $address{Bray}->{EMail}; print_address($address{Bray});

    I believe it has something to do with the hash key not existing as you're trying to declare it lexically, but I couldn't find anything concrete (to me) in the docs. See my and perlsub for more details.



    --chargrill
    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
Re: References with hashes
by syphilis (Archbishop) on Feb 15, 2007 at 04:47 UTC
    my $address{Bray} = {

    One way to avoid the syntax error:
    my %address; $address{Bray} = {
    Cheers,
    Rob
Re: References with hashes
by fenLisesi (Priest) on Feb 15, 2007 at 08:47 UTC
    brayshakes,

    The following may give you ideas to start from:

    use strict; use warnings; use Data::Dumper; my %personal_info_of = ( Larry => { Name => 'Larry Wall', Str => 'Camelot 1', EMail => 'larry@dromedary.com', }, Bray => { Name => 'Braden Mailloux', Str => 'kapellenstr. 1', EMail => 'brayshakes@gmail.com', }, ); for my $hacker (sort keys %personal_info_of) { print dump_personal_info_of( $hacker ); } exit( 0 ); sub dump_personal_info_of { my ($goodfella) = @_; return q() . "Personal information of $goodfella:\n" . Dumper( $personal_info_of{ $goodfella } ) ; }
    It prints:
    Personal information of Bray: $VAR1 = { 'Str' => 'kapellenstr. 1', 'EMail' => 'brayshakes@gmail.com', 'Name' => 'Braden Mailloux' }; Personal information of Larry: $VAR1 = { 'Str' => 'Camelot 1', 'EMail' => 'larry@dromedary.com', 'Name' => 'Larry Wall' };
    Cheers.
Re: References with hashes
by DrHyde (Prior) on Feb 15, 2007 at 10:11 UTC
    If you really did just "change the values from a tutorial" then yes, it has bugs. I'd be interested to see precisely what tutorial it is that is this buggy.

    Anyway, as presented here it has at least two serious bugs. First, %address isn't declared. Second, someone forgot to write the print_address subroutine.

Re: References with hashes
by brayshakes (Acolyte) on Feb 15, 2007 at 05:04 UTC
    So, why is the program still printing a subroutine error? What is the interpreter assuming print_address is a subroutine?
      Because print_address() is how one would call a subroutine called print_address. Apparently there is no such subroutine, thus the error. You need to create the subroutine or remove that line.

      print_address is not a built in function so you would have to supply it yourself. If you provided a link to the tutorial you are using it would be easier to help you. Try just changing it to print instead.

      P.S. if you want to include code in your post wrap it in <code></code> tags.


      ___________
      Eric Hodges
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-25 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found