Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl use strict; use warnings; my $fh; my $tag; while(<DATA>){ chomp $_; if(/^{(.*)}/) { $tag = $1; push(my @metadata_tags,$tag); } else { if($tag eq 'FILE') { if(defined($fh)){ print $fh "</ROOT>"; close($fh); } 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
How to write $_, inside the tag until it finds the string in an array.
print $fh "<$tag>$_</$tag>\n";

Replies are listed 'Best First'.
Re: Read the lines until a string exists in the array
by rovf (Priest) on Aug 14, 2009 at 07:45 UTC
    until it finds the string in an array.

    Which string into which array? In general, for finding a string in an array, you use grep.

    BTW, your usage of @metadata looks odd. You are creating a local array at the end of a block, inside a function call????? So the whole push is meaningless. You are just creating an array, pushing something in it, and let the array disappear immediately.

    UPDATE: Question for more experienced monks than I am: If I have my @NAME as parameter to the function, is the lifetime of @NAME only until the function ends, or is it until the end of the enclosing block?
    -- 
    Ronald Fischer <ynnor@mm.st>
      #!/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
      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
      <?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>
        How to read the data until the variable exists in an array

        I guess you mean until the variable exists in a hash, because your question about testing the existence in an array was already answered. You can test whether a key exists in a hash using the - surprise, surprise! - exists function. If you want to know whether a certain value exist in the hash, use values, which gives you an array of the values, and see whether your string exists in that array.

        BTW, your usage of the hash $ctr looks funny to me. As far I can see, your keys of the hash are just the numbers 1,2,3..., so from a usage point of view, your hash is not different from an array (only that you start counting by 1 instead of 0).

        -- 
        Ronald Fischer <ynnor@mm.st>