Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hash->Hash->Array! 1

Status
Not open for further replies.

Zippeh

Programmer
Sep 24, 2002
56
GB
I've got this XML file that I'm trying to parse to take out some values to put into a CSV file. One part of the file is some "tax_lines" that are associated with some invoices. I need to take out the tax_lines so that I can put them with the invoice lines in the CSV. To do this I have decided to use a Hash->Hash->Array data structure. With the first key being the sequence, the second key being the sub_sequence numbers and the array containing the information. Here is what I have so far:

Code:
      my %taxlines;
      foreach my $taxline ($transaction->getElementsByTagName("tax_line")) {
        my $tax_type = $taxline->getAttributeNode("tx_tax_type")->getValue;
	my $tax_sequence = $taxline->getAttributeNode("tx_sequence")->getValue;
	my $tax_sub_sequence = $taxline->getAttributeNode("tx_sub_sequence")->getValue;
	my $tax_code = $taxline->getAttributeNode("tx_tax_code")->getValue;
	my $tax_goods_value = $taxline->getAttributeNode("tx_goods_value")->getValue;
	my $tax_value = $taxline->getAttributeNode("tx_tax_value")->getValue;
	my @tax_array = [$tax_code, $tax_goods_value, $tax_value];
	$taxlines{$tax_sequence}{$tax_sub_sequence} = [@tax_array];
      
      }
      print $taxlines{1}{1}[2];

I think I can put the information into the Hash fine, but I can't reference it at the end.

Any ideas?
 
my @tax_array = [$tax_code, $tax_goods_value, $tax_value];
$taxlines{$tax_sequence}{$tax_sub_sequence} = [@tax_array];
is your problem. [] constructs an anonymous array and returns a reference to it - it doesn't return the array. When you assign to @tax_array, you're putting reference as the sole member of your new array, @tax_array.

Try
Code:
$taxlines{$tax_sequence}{$tax_sub_sequence} = [$tax_code, $tax_goods_value, $tax_value];
or, if you want the intermediate variable,
Code:
my [red]$[/red]tax_array = [$tax_code, $tax_goods_value, $tax_value]; #arrayref
$taxlines{$tax_sequence}{$tax_sub_sequence} = [red]$[/red]tax_array;
or, the other way,
Code:
my @tax_array = [red]([/red]$tax_code, $tax_goods_value, $tax_value[red])[/red]; # array
$taxlines{$tax_sequence}{$tax_sub_sequence} = [red]\@[/red]tax_array;

That should get you going,

f

"As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilkes
 
That's spot on mate!

Thanks so much.

You can have a star for that :D
 
Working on the same problem,

There are going to be occasions when the index into the hash doesn't match anything in there. I.E. sequence number isnt in the first one or sub-sequence isnt in the second one.

How can you check for this? What does it return if you try to access an invalid key?
 
use "exists" to check for existance of a hash key. Don't be tempted to use defined - that checks the associated value.

If you assign to a non-existant element, it is auto-vivified - ie it springs into existance.

Quick contrived example:
Code:
my %h = (
   A => 14,
   B => undef,
);
foreach my $key (qw{ A B C D }) {
   if ( exists $h{$key} ) {
      print "$key is a key of %h\n";
      if ( defined $h{$key} ) {
         print "...and it is defined\n";
      }
    } else {
      print "$key is not a key of %h\n";
    }
}

$h{C} = undef;

foreach my $key (qw{ A B C D }) {
   if ( exists $h{$key} ) {
      print "$key is a key of %h\n";
      if ( defined $h{$key} ) {
         print "...and it is defined\n";
      }
    } else {
      print "$key is not a key of %h\n";
    }
}

Yours,

fish

"As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilkes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top