Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Initializing Hash reference

by Sun751 (Beadle)
on Jul 17, 2009 at 01:04 UTC ( [id://780898]=perlquestion: print w/replies, xml ) Need Help??

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

When I try to initialize an hash reference this way it works,
$$err_info{'CMD'}= 'Command Execution Failed'; $$err_info{'STATUS'}= 'Status Check Failed';
but when I do this,
$$err_info =( CMD => 'Command Execution Failed', STATUS => 'Status Check Failed', SUCCESS => 'Command Succeful upto', TIME => 'scalar localtime()', );
Its not working can any one guide me? please. And any suggestion if I want to initialize the hash reference with heaps of record what the better way to do that? please, Cheers,

Replies are listed 'Best First'.
Re: Initializing Hash reference
by jwkrahn (Abbot) on Jul 17, 2009 at 01:39 UTC

    You need to use an anonymous hash for the assignment:

    my $err_info = { CMD => 'Command Execution Failed', STATUS => 'Status Check Failed', SUCCESS => 'Command Succeful upto', TIME => 'scalar localtime()', };
Re: Initializing Hash reference
by roubi (Hermit) on Jul 17, 2009 at 02:52 UTC
    Your example should be corrected like such:
    %$err_info =( CMD => 'Command Execution Failed', STATUS => 'Status Check Failed', SUCCESS => 'Command Succeful upto', TIME => 'scalar localtime()', );
    but this is indeed simpler:
    $err_info ={ CMD => 'Command Execution Failed', STATUS => 'Status Check Failed', SUCCESS => 'Command Succeful upto', TIME => 'scalar localtime()', };
Re: Initializing Hash reference
by zer (Deacon) on Jul 17, 2009 at 02:34 UTC
    In the first example you are setting a referance to a scalar which points to a hash. Similar to: $err->{'CMD'} = ... The latter example can be handled similar to $arrayref = \@ARGV; so $arrayref = ... http://perldoc.perl.org/perlref.html
Re: Initializing Hash reference
by afoken (Chancellor) on Jul 18, 2009 at 10:48 UTC

    Come on, DON'T create a new thread for every tiny aspect of your problem! You won't get better responses if you hide the context of your problem over and over again. You opened at least five other threads just for THIS part of your problem: executing hash element as command, reading commands from configuration file, How to read hash of .cfg file, Useless use of private variable, Reading .cfg file. And you did the same for any other part of your problem: Environment variables, testing for undef, getopt, reading directories, capturing program output, XML; often clearly showing that you did not even spend one second searching the documentation. You were told several times to read the documentation and to stay in one thread. You are wasting our resources.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-24 12:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found