Line separator issues using OLE / outlook

N

niall.macpherson

I am using the outlook interface for the first time . I am attempting
to read a mail message which has a number of lines which should match
the pattern
/(.*),(\d\d\d\d-\d\d-\d\d),(\d\d:\d\d:\d\d),(\d*)/


To test the pattern match I used the following test program
-------------------------------------------------------------------------------------------------------------
use strict;
use warnings;
use Data::Dumper;

my $bigstring = '';
while(<DATA>)
{
$bigstring .= $_;
}
print Dumper $bigstring;
while($bigstring =~
/(.*),(\d\d\d\d-\d\d-\d\d),(\d\d:\d\d:\d\d),(\d*)/g)
{
my ($tablename, $date, $time, $ticks) = ($1, $2, $3, $4);

print Dumper $tablename, $date, $time, $ticks;
}
__END__
fxticks,2006-04-19,09:40:04,412130617,
bondticks,2006-04-19,09:40:04,361784391,
ir_swapticks,2006-04-19,09:40:04,241879716,
intrateticks,2006-04-19,09:40:04,232578257,
fraticks,2006-04-19,09:40:04,209225323,
rtrspttick,2006-04-19,09:40:04,205294165,

-------------------------------------------------------------------------------------------------------------------------

which produced the desired output

C:\develop\NiallPerlScripts>multimatch.pl
$VAR1 = 'fxticks,2006-04-19,09:40:04,412130617,
bondticks,2006-04-19,09:40:04,361784391,
ir_swapticks,2006-04-19,09:40:04,241879716,
intrateticks,2006-04-19,09:40:04,232578257,
fraticks,2006-04-19,09:40:04,209225323,
rtrspttick,2006-04-19,09:40:04,205294165,';
$VAR1 = 'fxticks';
$VAR2 = '2006-04-19';
$VAR3 = '09:40:04';
$VAR4 = '412130617';
$VAR1 = 'bondticks';
$VAR2 = '2006-04-19';
$VAR3 = '09:40:04';
$VAR4 = '361784391';
$VAR1 = 'ir_swapticks';
$VAR2 = '2006-04-19';
$VAR3 = '09:40:04';
$VAR4 = '241879716';
$VAR1 = 'intrateticks';
$VAR2 = '2006-04-19';
$VAR3 = '09:40:04';
$VAR4 = '232578257';
$VAR1 = 'fraticks';
$VAR2 = '2006-04-19';
$VAR3 = '09:40:04';
$VAR4 = '209225323';
$VAR1 = 'rtrspttick';
$VAR2 = '2006-04-19';
$VAR3 = '09:40:04';
$VAR4 = '205294165';

C:\develop\NiallPerlScripts>

-----------------------------------------------------------------------------------------------------------------------------------

However, when I incorporated this into my program which reads the data
from outlook as follows

use strict;
use warnings;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Outlook';
use Data::Dumper;

my $outlook = Win32::OLE->new('Outlook.Application') or die
"Error!\n";

my $namespace = $outlook->GetNamespace("MAPI");
my $folder = $namespace->GetDefaultFolder(olFolderInbox);
my $items = $folder->Items;

my %tickstats;

for my $itemIndex (1..$items->Count)
{
my $message = $items->item($itemIndex);
next if not defined $message;

if($message->{Subject} =~ /Tick Status Test/)
{
print Dumper 'Body ' . $message->{Body};

while( $message->{Body} =~
/(.*),(\d\d\d\d-\d\d-\d\d),(\d\d:\d\d:\d\d),(\d*)/g)
{
my ($tablename, $date, $time, $ticks) = ($1, $2, $3, $4);

print Dumper $tablename, $date, $time, $ticks;
$tickstats{ $tablename } { $date } = $ticks;
}
}
}
print Dumper \%tickstats;

exit(0);
--------------------------------------------------------------------------------------------------------------------------------
I ended up with an infinite loop - the first line just keeps getting
re-processed

C:\develop\NiallPerlScripts>outlook.pl
$VAR1 = 'Body fxticks,2006-04-19,09:40:04,412130617,
bondticks,2006-04-19,09:40:04,361784391,
ir_swapticks,2006-04-19,09:40:04,241879716,
intrateticks,2006-04-19,09:40:04,232578257,
fraticks,2006-04-19,09:40:04,209225323,
rtrspttick,2006-04-19,09:40:04,205294165,
';
$VAR1 = 'fxticks';
$VAR2 = '2006-04-19';
$VAR3 = '09:40:04';
$VAR4 = '412130617';
$VAR1 = 'fxticks';
$VAR2 = '2006-04-19';
$VAR3 = '09:40:04';
$VAR4 = '412130617';
$VAR1 = 'fxticks';
$VAR2 = '2006-04-19';
$VAR3 = '09:40:04';
$VAR4 = '412130617';
$VAR1 = 'fxticks';
$VAR2 = '2006-04-19';
$VAR3 = '09:40:04';
$VAR4 = '412130617';
$VAR1 = 'fxticks';
$VAR2 = '2006-04-19';
$VAR3 = '09:40:04';
$VAR4 = '412130617';
$VAR1 = 'fxticks';
$VAR2 = '2006-04-19';
$VAR3 = '09:40:04';
$VAR4 = '412130617';
.....................................................................
....................................................................,.
ad infintum .......

I cannot see why - can anyone help ?

Thanks
 
B

Brian McCauley

(e-mail address removed) wrote:

[ Let me take this opportunity to congratuate Niall on a question well
asked ]
I am using the outlook interface for the first time . I am attempting
to read a mail message which has a number of lines which should match
the pattern
/(.*),(\d\d\d\d-\d\d-\d\d),(\d\d:\d\d:\d\d),(\d*)/

To test the pattern match I used the following test program
while($bigstring =~
/(.*),(\d\d\d\d-\d\d-\d\d),(\d\d:\d\d:\d\d),(\d*)/g)
which produced the desired output
However, when I incorporated this into my program which reads the data
from outlook as follows
my $namespace = $outlook->GetNamespace("MAPI");
my $folder = $namespace->GetDefaultFolder(olFolderInbox);
my $items = $folder->Items;
for my $itemIndex (1..$items->Count)
{
my $message = $items->item($itemIndex);
while( $message->{Body} =~
/(.*),(\d\d\d\d-\d\d-\d\d),(\d\d:\d\d:\d\d),(\d*)/g)
I cannot see why - can anyone help ?

$message is not a simple hash but an object that presents a hash-like
interface. When you say $message->{Body} you are actually calling a
method tied(%{$message})->FETCH('Body').

Now the way m//g in a scalar context works is rather odd. Every scalar
lvalue in Perl has a special hidden attibute called 'pos' that can be
manipulated via the pos() function which records the "current search
position". But when you call the FETCH method in a loop as you do above
you get a fresh scalar value each time with a pos of 0.

Note: this is still unavoidably the case even if the FETCH() method is
given an lvalue attribute. IMNSHO this is a bug in perl.

Try the following

my $body = $message->{Body};
while( $body =~ /(.*),(\d\d\d\d-\d\d-\d\d),(\d\d:\d\d:\d\d),(\d*)/g)

This way pos($body) gets to retain the current search position within
the string between iterations of the loop.
 
N

niall.macpherson

Brian said:
Now the way m//g in a scalar context works is rather odd. Every scalar
lvalue in Perl has a special hidden attibute called 'pos' that can be
manipulated via the pos() function which records the "current search
position". But when you call the FETCH method in a loop as you do above
you get a fresh scalar value each time with a pos of 0.

Note: this is still unavoidably the case even if the FETCH() method is
given an lvalue attribute. IMNSHO this is a bug in perl.

Try the following

my $body = $message->{Body};
while( $body =~ /(.*),(\d\d\d\d-\d\d-\d\d),(\d\d:\d\d:\d\d),(\d*)/g)

This way pos($body) gets to retain the current search position within
the string between iterations of the loop.

Thanks for the suggestion and the explanation Brian - I have changed
the code as suggested and all is now working as expected !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top