Line separator issues using OLE / outlook

Discussion in 'Perl Misc' started by niall.macpherson@ntlworld.com, Apr 19, 2006.

  1. Guest

    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
    , Apr 19, 2006
    #1
    1. Advertising

  2. 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.
    Brian McCauley, Apr 19, 2006
    #2
    1. Advertising

  3. Guest

    Brian McCauley wrote:

    > 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 !
    , Apr 20, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick.O.Ige

    Ole ole

    Patrick.O.Ige, Jul 16, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    468
    Patrick.O.Ige
    Jul 16, 2006
  2. Drew Pihera
    Replies:
    0
    Views:
    614
    Drew Pihera
    Feb 4, 2004
  3. Lee Sander
    Replies:
    6
    Views:
    283
    Hendrik van Rooyen
    Jun 1, 2007
  4. Steve Howell
    Replies:
    3
    Views:
    269
    George Sakkis
    Jun 2, 2007
  5. Lance Hoffmeyer
    Replies:
    0
    Views:
    219
    Lance Hoffmeyer
    Nov 17, 2003
Loading...

Share This Page