Hi, I'm having trouble to get the value of a certain tag which seems have a complicated data structure.

Object: To pull data from a data and print it out to a report file.
Problem: Getting the data from array of hash and pass it to a hash of hash or print it directly(any easy way).


This code is where i pull data from a certain file (Array of hashes)

$rej= $error_info->[0]->{'ERROR_CODE'};

pass $rej variable to be a hash (Hashes of hash).

$outHash{reject}= $rej;

output should populate the report leaving those blank fields

[API] tmstmp = 2007-08-15_192437
[API] rspns_typ = Line Loss
[API] rej_code =
[API] rej_desc =
[API] Response Type Included in Report.

Recommended Answers

All 4 Replies

one quick question is, how could I create a loop to this code:

$rej= $error_info->[0]->{'ERROR_CODE'};

I try this and had no output:

@size=$error_info; #where I get the initial value(Array of Hash)
for ($i=0; $i<$size; $i++) {
$rej= $error_info->[$i]->{'ERROR_CODE'};
}

where i did wrong ang how could I print it separately.

one quick question is, how could I create a loop to this code:

$rej= $error_info->[0]->{'ERROR_CODE'};

I try this and had no output:

@size=$error_info; #where I get the initial value(Array of Hash)
for ($i=0; $i<$size; $i++) {
$rej= $error_info->[$i]->{'ERROR_CODE'};
}

where i did wrong ang how could I print it separately.

First, as I asked in my previous reply, you need to give a more complete example of your data structure.

What is a small example of $error_info for example?

It is a good idea to post a small example of your code so others can help you. Posting individual lines or little snippets of code is not going to help me help you.

In your code above, there is no output because you haven't asked Perl to do any output.

Okey here's it is, due a previous error "Bad coerencing array to hash" which i already resolve by this code below:

if (ref ($error_info) eq 'ARRAY') {
      $size = @error_reject_info;
         #Array
      for($i=o; $i<$size; $i++) {
         $rejA = $error_info->[$i]->{'ERROR_CODE'};
         $rejB = $error_info->[$i]->{'ERROR_DESC'};
   }
   }else  {
      #Hash
         $rejA = $error_info->{ERROR_CODE};
         $rejB = $error_info->{ERROR_DESC};
   }

$error_info: Result of its dump var
Array of Hash

'ERROR _INFO' => [
          { 'ERROR _CODE' => 005, 'ERROR _DESC' => 'Error 1' },
          { 'ERROR _CODE' => 075, 'ERROR _DESC' => 'Error 2' },
                           ..... and so on 
                            ]

Print:

if (ref ($error_info) eq 'ARRAY') {
  
      for($i=o; $i<$size; $i++) {
         print 'error ', $rejA,"\n" ;
          print 'desc ',$rejB, "\n";
}}

All I want is to display those value of ERROR_CODE and ERROR_DECS.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.