in reply to Re: Read the lines until a string exists in the array
in thread Read the lines until a string exists in the array
I have converted an array to hash. How to read the data until the variable exists in an array. so that the output looks as below#!/usr/bin/perl use strict; use warnings; my $fh; my $tag; my $tag1; my %hash; my @metadata_tags; while(<DATA>){ chomp $_; if(/^{(.*)}/) { $tag = $1; push(@metadata_tags,$tag); } else { if($tag eq 'FILE') { if(defined($fh)){ print $fh "</ROOT>"; close($fh); } #Convert Array to hash my %hash=(); my $ctr = 1; $hash{$ctr++}=$_ foreach (@metadata_tags); my $filename = $_; open($fh, '>', "$filename.xml") or die "$filename: $!"; print $fh '<?xml version="1.0"?>',"\n"; print $fh "<ROOT>\n"; print $fh "<FILE>$filename</FILE>\n"; } elsif(defined($fh)) { if($_ ne ''){ #### Remove the blnak lines print $fh "<$tag>$_</$tag>\n"; } } } } exit(0); __DATA__ {FILE} sourcetag1 {NUMBER} 00000 {SOURCE} source1 {KEYWORD} {AUTHOR} author1 staff1 {HEADLINE} DISPOSABLE DECOR: THE CUTTING EDGE DULLS FAST\ STYLE AT A SPEED USUALLY ASSOCIATED WITH WARDROBE ITEMS. {FILE} sourcetag2 {NUMBER} 00002 {SOURCE} sourcenam2 {KEYWORD} {AUTHOR} author2 staff2
<?xml version="1.0"?> <ROOT> <FILE>sourcetag1</FILE> <NUMBER>00000</NUMBER> <SOURCE>source1</SOURCE> <AUTHOR>author1</AUTHOR> <AUTHOR>staff1</AUTHOR> <HEADLINE>DISPOSABLE DECOR: THE CUTTING EDGE DULLS FAST\ STYLE AT A SP +EED USUALLY ASSOCIATED WITH WARDROBE ITEMS.</HEADLINE> </ROOT>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Read the lines until a string exists in the array
by rovf (Priest) on Aug 14, 2009 at 09:21 UTC | |
by Anonymous Monk on Aug 14, 2009 at 09:27 UTC | |
by rovf (Priest) on Aug 14, 2009 at 09:47 UTC | |
by Anonymous Monk on Aug 14, 2009 at 17:29 UTC | |
by chromatic (Archbishop) on Aug 14, 2009 at 18:46 UTC | |
|