Click to See Complete Forum and Search --> : Error on a Reporting tool
eXceed69
09-18-2007, 03:50 AM
Hi Guys, I'm having error "Bad index while coercing array into hash" with this line which the only aim is just to get the xml values and save it to a variable.
$errTag = $file->{INFORMATION}->{NOTICE}->{LSR_NOTICE}->{LR}->{ERROR_INFO}->{ERROR_CODE};
OUTPUT
Telephone = 702-842-9010
line = HASH(0xd08a98)
date =
It's a array of hash in a array.
Guyz, any help..
dragle
09-18-2007, 01:58 PM
Hi!
One or more of those references is an array reference, instead of a hash; in which case you would need to access the appropriate array entry (at the appropriate spot); i.e., something like this:
$errTag = $file->[0]->{INFORMATION}->{NOTICE} ...
Try dumping the value of $file, that might help you see the internal hierarchical structure of the variable and thus how you can get to the data you're looking for. For example:
use Data::Dumper;
print Dumper($file);
Good luck!
eXceed69
09-19-2007, 03:28 AM
I found this one(http://perl.active-venture.com/pod/perl5005delta.html)
Bad index while coercing array into hash
(F) The index looked up in the hash found as the 0'th element of a pseudo-hash is not legal. Index values must be at 1 or greater
I can't see the dump value for it
dragle
09-19-2007, 07:57 AM
You'll probably need to show us more details before we can help you further. Are you really using pseudo-hashes (i.e., intentionally)? I still suspect that one of those entries in your chain is an array reference instead of a hash, and that you just need to dereference it as such. If dump isn't helping you find it, you could always check them one at a time; i.e.,
print '$file is a: ', ref $file, "\n";
print '$file->{INFORMATION} is a ', ref $file->{'INFORMATION'}, "\n";
etc. If you're using pseudo-hashes intentionally, then perldoc perlref might help, in the section labeled "Pseudo-hashes: Using an array as a hash." See: perlref (http://perldoc.perl.org/perlref.html#Pseudo-hashes%3a-Using-an-array-as-a-hash-pseudo-hash-pseudo-hash-pseudohash).
eXceed69
09-20-2007, 03:04 AM
.............
NOTICE' => {
'NOTICE_ID' => {},
'LSR_NOTICE' => {
'LSR_NOTICE_ADMIN_HDR' => {
'LSR_NO' => '28263',
'VER' => '01'
},
'LR' => {
'REMARKS' => should log as REJECT',
TECSON',
'ERROR_INFO' => [
{
'ERROR_CODE' => 965,
'ERROR_DESC' => 'multiple account'
},
{
'ERROR_CODE' => '907',
'ERROR_DESC' => 'This should be mapped as a REJECT Thank you'
...............
Here's the output of the dumper, how could i recognize if its array or href?
dragle
09-20-2007, 07:59 AM
ERROR_INFO is your array. You can tell by the square brackets; i.e., when you define an anonymous hash you do so with curly braces {}, and when you define an anonymous array you do so with square brackets []. Dumper uses this same visual syntax when it dumps out the structure so you can see which is which. So to refer to the first ERROR_INFO entry it would be:
{NOTICE}->{LSR_NOTICE}->{LR}->{ERROR_INFO}->[0]->{ERROR_CODE}
and the second would be [1], etc.
Cheers!
eXceed69
09-20-2007, 11:53 AM
But I already did adding indexes on it but it gives me error "NOT an Array". I did the second option using ref function and telling me that it is a Hash.
dragle
09-20-2007, 12:49 PM
You'll need to give us more to go on here, preferably a full reduction of the problem. For example, when I execute this script:
#!/usr/bin/perl
use strict;
use warnings;
my $file = {
INFORMATION => {
NOTICE => {
NOTICE_ID => {},
LSR_NOTICE => {
LSR_NOTICE_ADMIN_HDR => {
LSR_NO => '28263',
VER => 'r1'
},
LR => {
REMARKS => 'should log as REJECT',
ERROR_INFO => [
{
ERROR_CODE => 965,
ERROR_DESC => 'multiple account'
},
{
ERROR_CODE => '907',
ERROR_DESC => 'This should be mapped as a REJECT Thank you'
}
]
}
}
}
}
};
my $errTag1 = $file->{INFORMATION}->{NOTICE}->{LSR_NOTICE}->{LR}->{ERROR_INFO}->
[0]->{ERROR_CODE};
my $errTag2 = $file->{INFORMATION}->{NOTICE}->{LSR_NOTICE}->{LR}->{ERROR_INFO}->
[1]->{ERROR_CODE};
print 'Error code 1: ', $errTag1, "\n";
print 'Error code 2: ', $errTag2, "\n";
I get:
Error code 1: 965
Error code 2: 907
as I expect.
Cheers!
eXceed69
09-20-2007, 10:23 PM
The whole problem is retriving all value of ERROR_CODE and ERROR_DESC to its respective variables. Get the values of the fields gives error "Bad index while coercing array into hash". Below are the line of codes that error out while running the tool.
$errorATag = $file->{INFORMATION}->{NOTICE}->{LSR_NOTICE}->{LR}->{ERROR_INFO}->{ERROR_CODE};
$errorBTag = $file->{INFORMATION}->{NOTICE}->{LSR_NOTICE}->{LR}->{ERROR_INFO}->{ERROR_DESC};
And here's the whole actual output of the dumper:
$VAR1 = {
'hdr' => {
'file_name' => 'FILE456’
},
'INFORMATION' => {
'NOTICE' => {
'NOTICE_ID' => {},
'CC' => 'RON',
'LSR_NOTICE' => {
'LSR_NOTICE_ADMIN_HDR' => {
'PURCHASE' => 'W0000123,
'LSR_NO' => '200719',
'VER' => '01'
},
'LR' => {
'ORDER_INFO' => {},
'REP' => 'MAX P HANE',
'RT' => 'E',
'INIT' => 'RITCH TECSON',
'ERROR _INFO' => [
{
'ERROR _CODE' => 965,
'ERROR _DESC' => 'IT involves multiple Account'
},
{
'ERROR_CODE' => '907',
'ERROR _DESC' => 'This should be REJECT Thank you'
}
],
'EC_VER' => '01'
}
},
'NOTICE_AVAIL_DT_TM' => '07/07/84 15:15:15.000',
'CCNA' => 'R0N'
}
},
'TAG_ACK' => {},
'WOOT_ACK' => {
'MESSAGE_STATUS' => {}
}
};
Script aims to pull information to my archive files which is a xml in have a actual report file of the retrive information.
I'm not good with perl..
dragle
09-21-2007, 08:33 AM
Hi!
If you want to get all the possible entries in the ERROR_INFO array, you can loop through it with something like this:
foreach my $error_info (@{$file->
{INFORMATION}->
{NOTICE}->
{LSR_NOTICE}->
{LR}->
{ERROR_INFO}
}) {
print 'Error: ',
$error_info->{'ERROR_CODE'},
' ',
$error_info->{'ERROR_DESC'},
"\n";
}
With each pass through the loop, the private variable $error_info is set to be the next hash reference in the ERROR_INFO array; you can then access the $error_info hash directly within the loop.
For more details on references, you may want to look at these:
perldoc perlreftut (http://perldoc.perl.org/perlreftut.html)
perldoc perlref (http://perldoc.perl.org/perlref.html)
Good luck!
eXceed69
09-26-2007, 12:59 AM
Thanks Dragle
Just used this one:
if (ref($error_info) eq 'ARRAY') {
Work with array }
else {
Work with nonArray
}
I didn't encounter any error with this one :D
the only problem was its printing it.. And storing it to a array of hash the tool store it to a AoH variables. How could I do that?