Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Read a hash

Reply
Thread Tools

Read a hash

 
 
DanielC
Guest
Posts: n/a
 
      04-27-2011
INPUT (tt):

INFO - {
'Result' => 'failure',
'Id' => '1564',
'dataRecv' => 'adfasdfi',
'dataSent' => 'rqerqerer',
'elapsedTime' => '7.023359',
'externalId' => 'AI2JL4PBLG',
'responseCode' => '0',
'responseMsg' => 'n/a',
'sentAt' => '2011-04-27 00:08:02',
'serviceId' => '13',
'serviceName' => 'abc',
'timeInQueue' => 1
}

INFO - {
'Result' => 'failure',
'Id' => '1565',
'dataRecv' => 'asdfghjkl',
'dataSent' => 'qwerrtyu',
'elapsedTime' => '1.106637',
'externalId' => 'AI2JL4X1EN',
'responseCode' => '410',
'responseMsg' => 'Customer account not active',
'sentAt' => '2011-04-27 00:14:02',
'serviceId' => '13',
'serviceName' => 'def',
'timeInQueue' => 1
}


SCRIPT (test.pl):
#!/usr/bin/perl -w
#

my %hash;
local $/ = "\n\n";
while (my $line = <>)
{
$line =~ s/INFO - {/(/;
$line =~ s/}/)/;
%hash = $line;
print "$hash{'sentAt'}\n";
}

I know how to read the input line by line, then put them into a hash.
Here I just want to try read one section of data into a hash. However
it failed with below errors. Can someone shed some light on this
script and make it work?

$ cat tt|perl test.pl
Odd number of elements in hash assignment at test.pl line 10, <> chunk
1.
Use of uninitialized value in print at test.pl line 11, <> chunk 1.
Odd number of elements in hash assignment at test.pl line 10, <> chunk
2.
Use of uninitialized value in print at test.pl line 11, <> chunk 2.
 
Reply With Quote
 
 
 
 
Jim Gibson
Guest
Posts: n/a
 
      04-28-2011
In article
<(E-Mail Removed)>,
DanielC <(E-Mail Removed)> wrote:

> INPUT (tt):
>
> INFO - {
> 'Result' => 'failure',
> 'Id' => '1564',
> 'dataRecv' => 'adfasdfi',
> 'dataSent' => 'rqerqerer',
> 'elapsedTime' => '7.023359',
> 'externalId' => 'AI2JL4PBLG',
> 'responseCode' => '0',
> 'responseMsg' => 'n/a',
> 'sentAt' => '2011-04-27 00:08:02',
> 'serviceId' => '13',
> 'serviceName' => 'abc',
> 'timeInQueue' => 1
> }
>
> INFO - {
> 'Result' => 'failure',
> 'Id' => '1565',
> 'dataRecv' => 'asdfghjkl',
> 'dataSent' => 'qwerrtyu',
> 'elapsedTime' => '1.106637',
> 'externalId' => 'AI2JL4X1EN',
> 'responseCode' => '410',
> 'responseMsg' => 'Customer account not active',
> 'sentAt' => '2011-04-27 00:14:02',
> 'serviceId' => '13',
> 'serviceName' => 'def',
> 'timeInQueue' => 1
> }
>
>
> SCRIPT (test.pl):
> #!/usr/bin/perl -w
> #
>
> my %hash;
> local $/ = "\n\n";
> while (my $line = <>)
> {
> $line =~ s/INFO - {/(/;
> $line =~ s/}/)/;
> %hash = $line;
> print "$hash{'sentAt'}\n";
> }
>
> I know how to read the input line by line, then put them into a hash.
> Here I just want to try read one section of data into a hash. However
> it failed with below errors. Can someone shed some light on this
> script and make it work?


The variable $line is _data_. You are hoping that Perl will treat it as
_code_, but it does not. So your %hash is assigned one key ($line) and
no values. Hence the error about an odd number of elements. When the
value for 'sentAt' is fetched, it is null because it does not exist in
the hash, hence the 'uninitialized value in print' error.

You could try running eval on your data to treat it as code, but that
is not recommended.

Your only hope is to come up with a regular expression that can extract
keys and values from your string, capture them into an array or list,
and assign the array/list to your hash. Something like:

my %hash = ( $line =~ /'[^']*'/g );

That probably doesn't work, and I don't have time to test and fix it,
but somebody smarter than me will no doubt produce a working version
forthwith.

>
> $ cat tt|perl test.pl
> Odd number of elements in hash assignment at test.pl line 10, <> chunk
> 1.
> Use of uninitialized value in print at test.pl line 11, <> chunk 1.
> Odd number of elements in hash assignment at test.pl line 10, <> chunk
> 2.
> Use of uninitialized value in print at test.pl line 11, <> chunk 2.


--
Jim Gibson
 
Reply With Quote
 
 
 
 
DanielC
Guest
Posts: n/a
 
      04-28-2011
On Apr 27, 5:01*pm, Tad McClellan <(E-Mail Removed)> wrote:
> DanielC <(E-Mail Removed)> wrote:
> > INPUT (tt):

>
> > INFO - {
> > * * * * * 'Result' => 'failure',
> > * * * * * 'Id' => '1564',
> > * * * * * 'dataRecv' => 'adfasdfi',
> > * * * * * 'dataSent' => 'rqerqerer',
> > * * * * * 'elapsedTime' => '7.023359',
> > * * * * * 'externalId' => 'AI2JL4PBLG',
> > * * * * * 'responseCode' => '0',
> > * * * * * 'responseMsg' => 'n/a',
> > * * * * * 'sentAt' => '2011-04-27 00:08:02',
> > * * * * * 'serviceId' => '13',
> > * * * * * 'serviceName' => 'abc',
> > * * * * * 'timeInQueue' => 1
> > * * * * }

>
> > INFO - {
> > * * * * * 'Result' => 'failure',
> > * * * * * 'Id' => '1565',
> > * * * * * 'dataRecv' => 'asdfghjkl',
> > * * * * * 'dataSent' => 'qwerrtyu',
> > * * * * * 'elapsedTime' => '1.106637',
> > * * * * * 'externalId' => 'AI2JL4X1EN',
> > * * * * * 'responseCode' => '410',
> > * * * * * 'responseMsg' => 'Customer account not active',
> > * * * * * 'sentAt' => '2011-04-27 00:14:02',
> > * * * * * 'serviceId' => '13',
> > * * * * * 'serviceName' => 'def',
> > * * * * * 'timeInQueue' => 1
> > * * * * }

>
> > SCRIPT (test.pl):
> > #!/usr/bin/perl -w
> > #

>
> > my %hash;
> > local $/ = "\n\n";
> > while (my $line = <>)
> > {
> > * * $line =~ s/INFO - {/(/;
> > * * $line =~ s/}/)/;
> > * * %hash = $line;

>
> * * %hash = eval $line; *# Danger Will Robinson!
>
> > * * print "$hash{'sentAt'}\n";
> > }

>
> > I know how to read the input line by line, then put them into a hash.
> > Here I just want to try read one section of data into a hash. However
> > it failed with below errors. Can someone shed some light on this
> > script and make it work?

>
> > $ cat tt|perl test.pl
> > Odd number of elements in hash assignment at test.pl line 10, <> chunk
> > 1.
> > Use of uninitialized value in print at test.pl line 11, <> chunk 1.
> > Odd number of elements in hash assignment at test.pl line 10, <> chunk
> > 2.
> > Use of uninitialized value in print at test.pl line 11, <> chunk 2.

>
> --
> Tad McClellan
> email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
> The above message is a Usenet post.
> I don't recall having given anyone permission to use it on a Web site.


Yes, it works. I tried this before but it didn't get the right output.
Why?

%hash = eval { $line };
 
Reply With Quote
 
DanielC
Guest
Posts: n/a
 
      04-28-2011
On Apr 27, 5:07*pm, Jim Gibson <(E-Mail Removed)> wrote:
> In article
> <(E-Mail Removed)>,
>
>
>
>
>
>
>
>
>
> DanielC <(E-Mail Removed)> wrote:
> > INPUT (tt):

>
> > INFO - {
> > * * * * * 'Result' => 'failure',
> > * * * * * 'Id' => '1564',
> > * * * * * 'dataRecv' => 'adfasdfi',
> > * * * * * 'dataSent' => 'rqerqerer',
> > * * * * * 'elapsedTime' => '7.023359',
> > * * * * * 'externalId' => 'AI2JL4PBLG',
> > * * * * * 'responseCode' => '0',
> > * * * * * 'responseMsg' => 'n/a',
> > * * * * * 'sentAt' => '2011-04-27 00:08:02',
> > * * * * * 'serviceId' => '13',
> > * * * * * 'serviceName' => 'abc',
> > * * * * * 'timeInQueue' => 1
> > * * * * }

>
> > INFO - {
> > * * * * * 'Result' => 'failure',
> > * * * * * 'Id' => '1565',
> > * * * * * 'dataRecv' => 'asdfghjkl',
> > * * * * * 'dataSent' => 'qwerrtyu',
> > * * * * * 'elapsedTime' => '1.106637',
> > * * * * * 'externalId' => 'AI2JL4X1EN',
> > * * * * * 'responseCode' => '410',
> > * * * * * 'responseMsg' => 'Customer account not active',
> > * * * * * 'sentAt' => '2011-04-27 00:14:02',
> > * * * * * 'serviceId' => '13',
> > * * * * * 'serviceName' => 'def',
> > * * * * * 'timeInQueue' => 1
> > * * * * }

>
> > SCRIPT (test.pl):
> > #!/usr/bin/perl -w
> > #

>
> > my %hash;
> > local $/ = "\n\n";
> > while (my $line = <>)
> > {
> > * * $line =~ s/INFO - {/(/;
> > * * $line =~ s/}/)/;
> > * * %hash = $line;
> > * * print "$hash{'sentAt'}\n";
> > }

>
> > I know how to read the input line by line, then put them into a hash.
> > Here I just want to try read one section of data into a hash. However
> > it failed with below errors. Can someone shed some light on this
> > script and make it work?

>
> The variable $line is _data_. You are hoping that Perl will treat it as
> _code_, but it does not. So your %hash is assigned one key ($line) and
> no values. Hence the error about an odd number of elements. When the
> value for 'sentAt' is fetched, it is null because it does not exist in
> the hash, hence the 'uninitialized value in print' error.
>
> You could try running eval on your data to treat it as code, but that
> is not recommended.
>
> Your only hope is to come up with a regular expression that can extract
> keys and values from your string, capture them into an array or list,
> and assign the array/list to your hash. Something like:
>
> my %hash = ( $line =~ /'[^']*'/g );
>
> That probably doesn't work, and I don't have time to test and fix it,
> but somebody smarter than me will no doubt produce a working version
> forthwith.
>
>
>
> > $ cat tt|perl test.pl
> > Odd number of elements in hash assignment at test.pl line 10, <> chunk
> > 1.
> > Use of uninitialized value in print at test.pl line 11, <> chunk 1.
> > Odd number of elements in hash assignment at test.pl line 10, <> chunk
> > 2.
> > Use of uninitialized value in print at test.pl line 11, <> chunk 2.

>
> --
> Jim Gibson


Why I did "%hash = $line;" is because I had tested a script. BTW, I
don't understand - my %hash = ( $line =~ /'[^']*'/g );


#!/usr/bin/perl -w
#

my %hash = (
'Result' => 'failure',
'Id' => '1565',
'dataRecv' => 'asdfghjkl',
'dataSent' => 'qwerrtyu',
'elapsedTime' => '1.106637',
'externalId' => 'AI2JL4X1EN',
'responseCode' => '410',
'responseMsg' => 'Customer account not active',
'sentAt' => '2011-04-27 00:14:02',
'serviceId' => '13',
'serviceName' => 'def',
'timeInQueue' => 1
);

print "$hash{sentAt}\n";
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      04-28-2011
DanielC <(E-Mail Removed)> writes:

> INPUT (tt):
>
> INFO - {
> 'Result' => 'failure',
> 'Id' => '1564',
> 'dataRecv' => 'adfasdfi',
> 'dataSent' => 'rqerqerer',
> 'elapsedTime' => '7.023359',
> 'externalId' => 'AI2JL4PBLG',
> 'responseCode' => '0',
> 'responseMsg' => 'n/a',
> 'sentAt' => '2011-04-27 00:08:02',
> 'serviceId' => '13',
> 'serviceName' => 'abc',
> 'timeInQueue' => 1
> }


If you have any control over the input format you might want to change
to YAML, or use Data:umper. I recommend/prefer the former.

--
John Bokma j3b

Blog: http://johnbokma.com/ Facebook: http://www.facebook.com/j.j.j.bokma
Freelance Perl & Python Development: http://castleamber.com/
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      04-28-2011
>>>>> "TM" == Tad McClellan <(E-Mail Removed)> writes:

TM> Choose a more meaningful name, remove the unneeded parens and add the
TM> needed parens:

TM> my %hash = $chunk =~ /'([^']*)'/g;

and that fails because of this data line:

'timeInQueue' => 1

so it will also likely spit out an odd number of hash elements warning.

one way to fix this is to make the regex grab the left part (always in
'') skip => and whitespace and then the right part which is either in ''
or a digit (word?) string. i leave it as an exercise.

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Is there a hash algorithm with direct access to hash elements andreference count? Bo Peng C++ 4 03-12-2006 05:57 AM
In 'HashMap.put', "if (e.hash == hash && eq(k, e.key))" ? Red Orchid Java 3 01-30-2006 07:04 PM
standard library for hash table storage and hash algorithm Pieter Claassen C Programming 1 08-04-2004 03:11 AM



Advertisments