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

How to read the text and store the value to a variale until it finds the string & and ; and also check if it exists in an array.
#!/usr/bin/perl my %text = map { $_ => 1 } ('&amp;','&rdquo;','&ldquo','&apos;'); while($line = <DATA>){ $line =~ s/(&(\w+?;)*)(\w+)(&(\w+?;)*)/exists $text{$1} ? $1 : defined + ($2)/eg; print $3; chomp; } __DATA__ &rdquo; How big are &amp; the Brothers? &rdquo; &ldquo; One day hiere last wee +k, not knowing the cause, some floors of Rockefeller Center were evacuated when the floor shaking and screams of the lads&apos; fans during a TV
Suppose in the example,
I have to read the text line wise and store the string in a variable.
” is a string which contains & and ; and also exists in a array and also read until it finds another string with & and ; i.e &
So the text 'How big are' should be stored in a variable.
Form ” to “ there is no text between so it has to print "There is no value".

Replies are listed 'Best First'.
Re: check in an array for variable
by moritz (Cardinal) on Aug 13, 2009 at 07:32 UTC
    I don't quite understand what you are trying to achieve, the expected output would have been helpful.

    But I see three problems in your code:

    • The quantified capture (\w+?;)* probably won't do what you think it does.
    • defined ($2) produces either the empty string or a 1, probably not what you expect.
    • You don't use strict and warnings.
      $line =~ s/(&\w+?;)(\w+)(&\w+?;)/exists $text{$2} ? $2 : defined($2)/eg; I am trying to find the string between two values which is present in array.
      ie; regular expression to find the string between one text in an another to another.
Re: check in an array for variable
by baxy77bax (Deacon) on Aug 13, 2009 at 08:33 UTC
    ok,

    also having trouble understanding what you were aiming for....

    but here is what i figured you were looking for...

    #!/usr/bin/perl while(<DATA>){ chomp($_); my @array = split("&amp;|&rdquo;|&ldquo;|&apos;",$_); foreach (@array){ ($_ eq ' ') ? (print "--Value missing--") : (print $_) } } __DATA__ &rdquo; How big are &amp; the Brothers? &rdquo; &ldquo; One day hiere last wee +k, not knowing the cause, some floors of Rockefeller Center were evacuated when the floor shaking and screams of the lads&apos; fans during a TV
    cheers

    Update:

    while(<DATA>){ chomp($_); my @array1 = split(/(&amp;|&rdquo;|&ldquo;|&apos;)/,$_); foreach (@array1){ ($_ eq ' ') ? (print "--Value missing--\n") : (print $_ . "\n") } } result &rdquo; How big are &amp; the Brothers? &rdquo; --Value missing-- &ldquo; One day hiere last week, not knowing the cause, some floors of Rockefeller Center were evacuated when the floor shaking and screams of the lads &apos; fans during a TV
    is this ok :)

    if the spitted sentence (One day hiere...) is annoying you it is up to you to fix it :)

      Hi, The output should print even the elements of the array in between the text. ex: output should print like this:
      &rdquo; How big are &amp; the Brothers? &rdquo; Value missing &ldquo; One day hiere last week, not knowing the cause, some floors of Rockefe +ller Center were evacuated when the floor shaking and screams of the +lads &apos; fans during a TV
        I'd suggest splitting on /(\s*&\w+;\s*)/, then iterate over the return list and insert a "Value missing" where appropriate. That'll be much easier and more maintainable than trying to write it one regex.
Re: check in an array for variable
by ikegami (Patriarch) on Aug 13, 2009 at 13:35 UTC