http://qs321.pair.com?node_id=782261


in reply to Create hash from XML file

Hi ashok13123,

It is always best to use XML modules to parse these kind of files. Anyhow since it is simple one you can also use regex way.

Also in your xml file you have given in first <cat>

<cat> <channel>
I think it should be
<cat> <channelName>

Here is one way to accomplish your task.

use strict; use warnings; use Data::Dumper; my $input = " <cat> <channelName>CHANNEL123</channelName> <name>CATEGORY123</name> <shortName>TopCategory</shortName> <description>This is TopCategory channel's Category</description> <shortDescription>This is TopCategory channel's Category</shortDescrip +tion> <parentID>0</parentID> <categoryTags>Vod;Download</categoryTags> </cat> <cat> <channelName>Channel456</channelName> <name>cat456</name> <shortName>TopCategory</shortName> <description>This is TopCategory channel's Category</description> <shortDescription>This is TopCategory channel's Category</shortDescrip +tion> <parentID>0</parentID> <categoryTags>Vod;Download</categoryTags> </cat> <cat> <channelName>chann678</channelName> <name>cat678</name> <shortName>TopCategory</shortName> <description>This is TopCategory channel's Category</description> <shortDescription>This is TopCategory channel's Category</shortDescrip +tion> <parentID>0</parentID> <categoryTags>Vod;Download</categoryTags> </cat> "; my %hash; while ($input =~ /<channelName>((?:(?!<\/channelName>).)*)<\/channelNa +me>\s*<name>((?:(?!<\/name>).)*)<\/name>/gs){ $hash{$1} = $2; } print Dumper \%hash; output: ------- $VAR1 = { 'Channel456' => 'cat456', 'chann678' => 'cat678', 'CHANNEL123' => 'CATEGORY123' };

Prasad