use strict; use warnings; use DBI; use xml::twig; use utf8; my $dbh= connect_to_db(); our ($SKUgroupNUMBER, $smallPACKAGE, $sellingpoint1, $sellingpoint2, $sellingpoint3, $summarySELLING, $longSELLING, $skuGROUPimage, $skuGROUPaltIMAGE); my $file =<"SyncItemMaster.xml">; my $filename = <"Hashstructure.txt">; my $twig=XML::Twig->new( twig_handlers=> { 'oa:Codes[@name="Small_Package_Indicator"]'=> \&smallpack, 'oa:Note[@status="Selling_Point_1"]'=> \&sell1, 'oa:Note[@status="Selling_Point_2"]'=> \&sell2, 'oa:Note[@status="Selling_Point_3"]'=> \&sell3, 'oa:Note[@status="Summary_Selling_Statement"]'=> \&summary, 'oa:Note[@status="Long_Selling_Copy"]'=> \&longsell, 'us:SkuGroupImage' => \&image, 'us:SkuGroupAlternateImage' => \&altimage, 'oa:Code[@name="SKU_Group_Id"]'=> \&skuid, } ); $twig->parsefile("500SyncItemMaster.xml"); our %mirror; sub skuid{ my ($t, $node)=@_; my $prefix=$node->parent->parent->parent->first_child->next_sibling->text; my $stock=$node->parent->parent->parent->first_child->next_sibling->next_sibling->text; my $itemnumber="$prefix$stock"; my $value=$node->text; my $column="skuGROUPnumber"; $mirror{"$itemnumber"}={ $column =>"$value"}; #...lots of similar subs... open(my $fh,'>:utf8', "$filename") or die "Could not open $filename:$!"; my @k = (sort keys %mirror); foreach my $k (@k){ my $v = $mirror{$k}; my %hash = %$v; foreach my $h (keys %hash){ print $fh ("$k => \n $h => $hash{$h}\n")}; # print the hash }