Thread Text blockweise parsen (9 answers)
Opened by Lauvia at 2014-03-21 11:07

renee
 2014-03-24 12:41
#174386 #174386
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;
use List::Util qw(first);

my $file = 'test.txt';

my %frames_found;

{
open my $fh, '<', $file or die "$file: $!";

# change block separator to have one complete frame
# read per iteration
local $/ = "\nFRAME NAME:";

# read the blocks
while ( my $frame = <$fh> ) {

next if $. == 1; # skip the "header"

chomp $frame; # remove block separator

# get frame name
my ($name) = $frame =~ m{\A [^\(\[]* [\(\[] (\w+) [\)\]] }x;

# skip the frame when no name is given
next if !$name;

# check if user wants to get info about frame. Skip the frame if the
# user didn't mentioned it
next if !first{ $name eq $_ }@ARGV;

# get all signals
my @signals = grep{ $_ }$frame =~ m{^ \s* :+ 0x\d+ (.*?) \r?\n? $}xmg;

my @found;
for my $signal ( @signals ) {

# skip signals that contains "FREI" or "(ID2)"
next if index( $signal, 'FREI' ) != -1;
next if index( $signal, '(ID2)' ) != -1;

# skip signal if it does not contain _FA, or _A,
next if $signal !~ m{ _F?A, }x;

my ($value) = $signal =~ m{ \( ([^\)]+) \) }x;
push @found, $value if $value;
}

$frames_found{$name} = @found ? \@found : 1;
}
close $fh;
}

print Dumper \%frames_found;
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/

View full thread Text blockweise parsen