Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String weirdness

Status
Not open for further replies.

LuckyKoen

Programmer
Feb 14, 2005
57
BE
I have a script that works with data of a Word document and I use the OLE module for this. I have a problem when trying to read all the paragraphs and comparing each of them with a paragraph

Code:
my $paragraphs = $doc->ListParagraphs;
foreach my $paragraph (in $paragraphs) {   
    my $text = $paragraph->Range->Text;
    print "$text" 
    print " post\n";
}

One of the paragraphs is named "Introduction" and instead of "Introduction post" I get "postoduction". Apparently the 2nd print command starts printing on the same location and acts like the first print never took place.

I already took care of this problem by typing substr($text,0,-1). However, I'd like to know why $text acts like this, why substr solves this and if there are any more elegant ways to deal with this than taking a substr from it ?
 
First of all, there is no in function in perl
And second, foreach doesn't work for scalars, only arrays and hashes.

Oh and third, which module exactly do you use? Cause i dont recal a method 'ListParagraphs', in any Win32-OLE module.

If its not a trouble, can you please give us the whole code?


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
perluserpengo, I'm no expert on Win32::OLE, but this is from the documentation:
in(COLLECTION)
If COLLECTION is an OLE collection object then "in $COLLECTION"
returns a list of all members of the collection. This is a
shortcut for "Win32::OLE::Enum-"All($COLLECTION)>. It is most
commonly used in a "foreach" loop:

foreach my $value (in $collection) {
# do something with $value here
}
LuckyKoen, try putting $| = 1 near the top of your script. This will cause Perl to flush the output buffer after each print instead of waiting 'til it sees a return. I think that may be what's behind the odd print behavior.

HTH

 
LuckyKoen sorry dude for my comment/question, i guess i was hungry.
mikevh thanks for the correction man.


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Try like this
Code:
   $paragraphs = $doc->Paragraphs();
   $enumerate = new Win32::OLE::Enum($paragraphs);
   while(defined($paragraph = $enumerate->Next()))
   {
       $text = $paragraph->{Range}->{Text};
       $text =~ s/[\n\r]//g;
       $text =~ s/\x0b/\n/g;
       print "$text post\n";
   }
Tell me if it works


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
It was the \r that needed to be removed, $text =~ s/[\r]//g took care of the problem. Thanks for the help Mike and Pengo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top