Thread Zugriff auf Elemente einer Zeile (2 answers)
Opened by cohama at 2014-04-09 11:05

rosti
 2014-04-09 20:05
#174681 #174681
User since
2011-03-19
3472 Artikel
BenutzerIn
[Homepage]
user image
Wenn die Spalten so schön ausgerichtet sind, kannst Du eine Schablone drüberlegen. Jede Zeile kriegt einen hash, die Schlüssel entsprechend anpassen.

Code (perl): (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
use strict;
use warnings;
use Data::Dumper;

# da kommen die Daten hin
my $data = [];

# Daten einlesen
while(<DATA>){ # DATA means Handle
    my %line = ();
    @line{qw(lfdnr dev date time nr1 nr2 nr3 xnr well lastnr)} = unpack 'A9 A4 A11 A9 A7 A7 A13 A13 A17 A11', $_;
    push @$data, \%line;
}

print Dumper $data;


__DATA__
3434     OUT 2014-04-08 07:17:43 16     25     3338762777   X            unknown          615860      
3434     OUT 2014-04-08 07:38:14 200    209    3338762777   X            unknown          615860      
3851     OUT 2014-04-08 10:11:22 104    118    3338768047   3338768047   unknown          3116        
3851     OUT 2014-04-08 10:44:01 37     46     3338768047   3338359382   unknown          03094519504 
3851     OUT 2014-04-08 11:46:40 53     85     3338768047   3338768047   unknown          765900      
3851     OUT 2014-04-08 13:10:22 40     85     3338768047   3338768047   unknown          0309443582  
3935     OUT 2014-04-08 08:33:48 8      21     3338757522   3338757522   unknown          459687      
3965     OUT 2014-04-08 08:13:34 34     61     3338700192   3338700192   unknown          703553



und das gibt aus:
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
$VAR1 = [
{
'nr1' => '16',
'xnr' => 'X',
'dev' => 'OUT',
'time' => '07:17:43',
'date' => '2014-04-08',
'well' => 'unknown',
'lastnr' => '615860',
'nr3' => '3338762777',
'nr2' => '25',
'lfdnr' => '3434'
},
{
'nr1' => '200',
'xnr' => 'X',
'dev' => 'OUT',
'time' => '07:38:14',
'date' => '2014-04-08',
'well' => 'unknown',
'lastnr' => '615860',
'nr3' => '3338762777',
'nr2' => '209',
'lfdnr' => '3434'
},
{
'nr1' => '104',
'xnr' => '3338768047',
'dev' => 'OUT',
'time' => '10:11:22',
'date' => '2014-04-08',
'well' => 'unknown',
'lastnr' => '3116',
'nr3' => '3338768047',
'nr2' => '118',
'lfdnr' => '3851'
},
{
'nr1' => '37',
'xnr' => '3338359382',
'dev' => 'OUT',
'time' => '10:44:01',
'date' => '2014-04-08',
'well' => 'unknown',
'lastnr' => '03094519504',
'nr3' => '3338768047',
'nr2' => '46',
'lfdnr' => '3851'
},
{
'nr1' => '53',
'xnr' => '3338768047',
'dev' => 'OUT',
'time' => '11:46:40',
'date' => '2014-04-08',
'well' => 'unknown',
'lastnr' => '765900',
'nr3' => '3338768047',
'nr2' => '85',
'lfdnr' => '3851'
},
{
'nr1' => '40',
'xnr' => '3338768047',
'dev' => 'OUT',
'time' => '13:10:22',
'date' => '2014-04-08',
'well' => 'unknown',
'lastnr' => '0309443582',
'nr3' => '3338768047',
'nr2' => '85',
'lfdnr' => '3851'
},
{
'nr1' => '8',
'xnr' => '3338757522',
'dev' => 'OUT',
'time' => '08:33:48',
'date' => '2014-04-08',
'well' => 'unknown',
'lastnr' => '459687',
'nr3' => '3338757522',
'nr2' => '21',
'lfdnr' => '3935'
},
{
'nr1' => '34',
'xnr' => '3338700192',
'dev' => 'OUT',
'time' => '08:13:34',
'date' => '2014-04-08',
'well' => 'unknown',
'lastnr' => '703553',
'nr3' => '3338700192',
'nr2' => '61',
'lfdnr' => '3965'
}
];

Prozess erfolgreich beendet.


Das lässt sich bestimmt prima weiterverarbeiten. Nur so als Idee...

PS: Falls Du die Datei selber schreibtst, mach das nicht mit sprintf sondern nimm einfach dieselbe Schablone in der Funktion pack()

PSPS: http://rolfrost.de/textfiles.html (Mehr dazu)
Last edited: 2014-04-10 14:49:05 +0200 (CEST)

View full thread Zugriff auf Elemente einer Zeile