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

Hi monks im trying extract some tags from .doc files.

Document name : check.doc apple.pst Entry No: 00001 monks.prj Enrty No: 00002

Im trying to extract the .pst and .prj name and the corresponding entry no to an excel sheet..Im currently learning and i dont know how to make an OR condition in perl .i.e i have to extract both .pst and .prj names.. The output should be

Output.xls Name Entry No apple.pst 00001 monks.prj 00002

The snippet im trying is

my @files; @files=glob('*.doc'); print @files; foreach my $file (@files) { my $var; $var = $SDD_Var."\\".$file; print $var ; my $document = Win32::OLE -> GetObject("$var"); print "Extracting Text ...\n"; my @array; my $lineno =0; my $paragraphs = $document->Paragraphs(); my $enumerate = new Win32::OLE::Enum($paragraphs); while(my $paragraph = $enumerate->Next()) { $lineno++; my $text = $paragraph->{Range}->{Text}; $text =~ s/[\n\r\t]//g; $text =~ s/\x0B/\n/g; $text =~ s/\x07//g; chomp $text; my $Data .= $text; $array[$lineno] = $text; } my $i; for ($i = 0; $i <@array; $i++) { if($array[$i] =~ /$ .pst/) { $sdd = $1; { for ($count = $i; $count >= 1; $count++) { if ($array[$count] =~ /^Entry\s no /) { $Fun = $1; # print "$Fun \n"; #$Fun = $1; my $Mycell1 = $Sheet->Ran +ge($Sheet->Cells($row, $col),$Sheet->Cells($row, $col+2)); $Mycell1->{Value}=["$sdd","$Fun"]; $row++; # print $out_fh $sdd."\t".$Fun."\t".$File."\n" +; goto breakingfunction; } } } breakingfunction: #} } } }

The snippet above is not extracting anything and how to extract both .pst and .prj in one regex..pls rectify ...

Replies are listed 'Best First'.
Re: Or condition in Perl
by tobyink (Canon) on Mar 11, 2012 at 08:57 UTC

    You've got:

    if($array[$i] =~ /$ .pst/)

    This will never match anything. The "$" matches the end of the string. So what you're trying to match is the end of the string, followed by five characters. Clearly you can't have characters after the end of the string!

    What you want is:

    if($array[$i] =~ /\.(pst|prj)$/)

    This matches a literal full stop character, then either "pst" or "prj", and then the end of the string.

      Thanks tobyink ..When i tried with the regex each and evry time CONVERT FILE Window is opening ...Any idea where im making wrong..Once again Thanks Dude

        How did you make sure that the problem is related to the regular expression supplied by tobyink?

        Most likely, this is another error in your script that is totally unrelated to the regular expression.

        Maybe you should eliminate all other code and only keep the code that relates to the "convert file window", whatever that is. I don't see your code trying to load or save a file in the block governed by the regular expression, so you are either not showing the relevant code or are misinterpreting how your program behaves.

Re: Or condition in Perl
by Corion (Patriarch) on Mar 11, 2012 at 09:06 UTC

    Where in your code are you trying to match .pst and .prj?

    How does your code currently fail to work?

    Is the rest of your setup needed to test the functionality of matching .pst or .prj?

    Please provide a short, self contained, compiling example that also has the input data as text. You also might want to read perlre and/or perlretut to learn about regular expressions and how they work. Also see YAPE::Regex::Explain.

A reply falls below the community's threshold of quality. You may see it by logging in.