Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Not able to assign a hash reference.

by Ankur_kuls (Sexton)
on Jul 14, 2014 at 06:49 UTC ( [id://1093496]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks..I have this script which reads i/p file line by line into variable $p, manipulates in a way it becomes a hash reference.. but when I print the $p it prints the manipulated line not something like HASH(0x1e0e9b8) (means it isn't a hash ref. just a normal variable... below is my script..

$line = "cksjdcdbsdcnusageControlAccum:S551(!{\!reportingGroups\!:[{\! +absoluteAccumulated\!:{\!counters\!:[{\!bidirVolume\!:306490319,\!nam +e\!:\!base\!}],\!expiryDate\!:{\!volume\!:\!24-07-2014T00:00:00\!},\! +previousExpiryDate\!:{\!time\!:\!26-06-2014T00:00:00\!,\!volume\!:\!2 +6-06-2014T00:00:00\!},\!reportingLevel\!:\!totalTraffic\!,\!resetPeri +od\!:{\!volume\!:\!28 days\!}},\!name\!:\!110\!,\!restartInfo\!:\!26- +06-2014T00:00:00\!,\!selected\!:\!yes\!,\!subscriberGroupName\!:\!GJ_ +FUP_3GEXP_125MB_28\!,\!subscriptionDate\!:\!26-06-2014T00:00:00\!,\!v +alidityTime\!:3600}],\!version\!:\!2.1\!}!)"; $line =~s/\\//g; if ( $line =~ /usageControlAccum:(\w+)\(!(.*)!\)/ ) { $p=$2; $p=~s/:\{/ => {/g; $p=~s/:\[/ => [/g; $p=~s/!/\'/g; $p=~s/\':/\'=>/g; } print $p;

ouput of which is

{'reportingGroups' => [{'absoluteAccumulated' => {'counters' => [{'bid +irVolume'=>306490319,'name'=>'base'}],'expiryDate' => {'volume'=>'24- +07-2014T00:00:00'},'previousExpiryDate' => {'time'=>'26-06-2014T00:00 +:00','volume'=>'26-06-2014T00:00:00'},'reportingLevel'=>'totalTraffic +','resetPeriod' => {'volume'=>'28 days'}},'name'=>'110','restartInfo' +=>'26-06-2014T00:00:00','selected'=>'yes','subscriberGroupName'=>'GJ_ +FUP_3GEXP_125MB_28','subscriptionDate'=>'26-06-2014T00:00:00','validi +tyTime'=>3600}],'version'=>'2.1'}

now if I assign the above output value directly to any variable & then print it..it is considering it like a hash ref but not in this case...pls help..thanks

Replies are listed 'Best First'.
Re: Not able to assign a hash reference.
by Anonymous Monk on Jul 14, 2014 at 07:06 UTC

    manipulates in a way it becomes a hash reference.

    You wish. It doesn't do that. Your string is a string, its not a hash reference. Maybe if you use Safest Undumper of Data::Dumper you can get a real honest to goodness hashref

      thanks for reply...but now when I am using it with eval() it is working...means what I did is, $q = eval($p); print $q; & now it is giving the hashref value...could u plz help me know how it happens?

        I don't understand the question

        eval is unsafe, it permits things like system "rm -rf ~ / " -- you're always getting more than a hashref

        If you only want a hashref use SafestUndumper

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-20 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found