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

my @msg = <DATA>; my $msg = join("", @msg); my $f = _extractInfoFromMsg($msg); my %f = %$f; print 'NEW TICKET NUMBER: ' . $f{TICKET_NUMBER} . "\n"; print 'OLD TICKET NUMBER: ' . $f{OLD_TICKET_NUMBER} . "\n"; print ' MESSAGE TYPE: ' . $f{MESSAGE_TYPE} . "\n"; print ' LEAD TIME: ' . $f{LEAD_TIME} . "\n"; sub _extractInfoFromMsg { $_ = shift; my %fields; $_ =~ /TICKET NUMBER-+\[(.*?)\]/; $fields{TICKET_NUMBER} = $1; $_ =~ /OLD TICKET NUMBER-+\[(.*?)\]/; $fields{OLD_TICKET_NUMBER} = $1; $_ =~ /MESSAGE TYPE-+\[(.*?)\]\s+LEAD TIME-+\[(.*?)\]/; $fields{MESSAGE_TYPE} = $1; $fields{LEAD_TIME} = $2; return \%fields; } __DATA__ TICKET NUMBER--[051580160] OLD TICKET NUM-[051540724] MESSAGE TYPE---[NORMAL] LEAD TIME--[72] PREPARED-------[06/07/05]

produces

D:\user\punkish>test.pl NEW TICKET NUMBER: 051580160 OLD TICKET NUMBER: 051580160 MESSAGE TYPE: NORMAL LEAD TIME: 72

instead of

NEW TICKET NUMBER: 051580160 OLD TICKET NUMBER: 051540724 MESSAGE TYPE: NORMAL LEAD TIME: 72

What is the obvious stupid thing that I am doing here? That is, why is the OLD TICKET NUMBER not correct?

--

when small people start casting long shadows, it is time to go to bed

Replies are listed 'Best First'.
Re: Regexp matching blues
by cmeyer (Pilgrim) on Jun 28, 2005 at 22:28 UTC

    Your second regex in _extractInfoFromMsg() isn't matching (regex wants "NUMBER", data says "NUM"), so $1 still has the value that the first regex set it to.

    You could do something like:
    if ( $_ =~ /OLD TICKET NUMBER-+\[(.*?)\]/ ) { $fields{OLD_TICKET_NUMBER} = $1; } else { $fields{OLD_TICKET_NUMBER} = 'NOT FOUND'; }

    -Colin.

    WHITEPAGES.COM | INC

      I am embarassed, shame-faced, and ashamed. I am going to go and shave my head off leaving only a thin wedge sticking out in the middle. Please, don't anyone else bother wasting your time replying to this stupid thread.

      By the way, cmeyer, much gratitude to you for slapping me out of my glazed stupor.

      --

      when small people start casting long shadows, it is time to go to bed
        I think that I'll go and ... not shave my head at all. :)

        -Colin.

        WHITEPAGES.COM | INC

Re: Regexp matching blues
by Samy_rio (Vicar) on Jun 29, 2005 at 04:00 UTC

    Hi, you change the below line.

    $_ =~ /OLD TICKET NUMBER-+\[(.*?)\]/;

    should be

    $_ =~ /OLD TICKET NUM-+\[(.*?)\]/;

    I think its helps you

    Regards,
    Velusamy

Re: Regexp matching blues
by l.frankline (Hermit) on Jun 29, 2005 at 14:37 UTC
    the following statements gives solutions to you...

    Code as
    $_ =~ /TICKET NUMBER\-\+\[(.*?)\]/;
    $_ =~ /OLD TICKET NUMBER\-\+\[(.*?)\]/;

    Instead of

    $_ =~ /TICKET NUMBER-+\[(.*?)\]/;
    $_ =~ /OLD TICKET NUMBER-+\[(.*?)\]/;

    * Frank *