perl - ACARS message parsing -


i need parse acars messages xml format.

there simple messages:

rx_idx: 13 acars mode: o, message label: 5v acars ml description: vdl switch advisory aircraft reg: .ei-eux, flight id: un0323 block id: 57,  msg. no: s91a message content:-  ----------------------------------------------------------[05/05/2013 08:58]  rx_idx: 14 acars mode: 2, message label: 1l acars ml description: off message aircraft reg: .d-airo, flight id: lh1490 aircraft vendor: airbus, short type: a321, full type: a321-131, cn: 0563 carrier iata: lh, icao: dlh, remarks: lufthansa airlines: lufthansa block id: 56,  msg. no: m03a message content:- 00002216743go,x,55655 ----------------------------------------------------------[05/05/2013 09:24] 

each message starts rx_idx , ends date (eg. [05/05/2013 09:24]).

i found perl script, doesn't recognize attributes after commas.

#!/usr/local/bin/perl use strict; use warnings;  @keys = (      'rx_idx',     'acars mode',     'message label',     'acars ml description',     'aircraft reg',     'flight id',     'aircraft vendor',     'short type',     'full type',     'cn',     'carrier iata',     'icao',     'remarks',     'airlines',     'block id',     'msg. no',     'message content' );  my( %keys, %tags ); $keys{$_} = 1 @keys; $tags{$_} = $_ . '' @keys; $tags{$_} =~ s/ /_/g @keys;  $file = 'data8.txt'; open( $fh, '<', $file) or die("can't open $file: $!");  %record = map { $_, '' } @keys; while( $line = <$fh> ) {     chomp($line);     if( $line =~ m{ \a (.+?) : \s* (\s+) }x ) {         $record{$1} = $2 if $keys{$1};         if( $1 eq $keys[$#keys] ) {             print "<message>\n";             print "<$tags{$_}>$record{$_}</$tags{$_}>\n" @keys;             print "</message>\n";             %record = map { $_, '' } @keys;         }     } } 

regards

the problem if condition of regex match once each line. try match regular expression until if fails in while loop. added \g assertion when in next loop begin when left last time. changed little bit avoid matching @ beginning of line (\a) , added possible match of comma @ end, (i copied relevant part of code):

while( $line = <$fh> ) {      chomp($line);     while ( $line =~ m{ \g \s* (.+?) \s* : \s* ([^,]+) \s* (?:,|$) }xg ) {          $record{$1} = $2 if $keys{$1};         if( $1 eq $keys[$#keys] ) {              ...         }        }    } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -