in reply to Read the lines until a string exists in the array

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>

Replies are listed 'Best First'.
Re^2: Read the lines until a string exists in the array
by Anonymous Monk on Aug 14, 2009 at 08:20 UTC
    #!/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>
        print $fh "<$tag>$_</$tag>\n"; I have to print the output of
        <AUTHOR>author1</AUTHOR> <AUTHOR>staff1</AUTHOR> <HEADLINE>DISPOSABLE DECOR: THE CUTTING EDGE DULLS FAST\</HEADLINE> <HEADLINE>STYLE AT A SPEED</HEADLINE> <HEADLINE>USUALLY ASSOCIATED WITH WARDROBE ITEMS.</HEADLINE>
        to
        <AUTHOR>author1 staff1<AUTHOR> <HEADLINE>DISPOSABLE DECOR: THE CUTTING EDGE DULLS FAST\ STYLE AT A SP +EED SUALLY ASSOCIATED WITH WARDROBE ITEMS.</HEADLINE>
        So wanted to store the value of $_ until it reads some value which exists in an array. So that I may get the result which I wanted