in reply to Can't read files content
I agree with artist that it's not very clear from your post what you are trying to achieve, or what you are having problems with. The following code shows how I would read the file.
It use 'paragraph mode' (See perlvar:$INPUT_RECORD_SEPERATOR) to read each record in one chuck.
It then uses a large and complicated looking but quite simple to derive, regex to parse the record into $1 .. $23. (Note: The use of the /m option. See perlre for details)
When the regex matches, it builds a hash with the first field on each line as the key name, and the second & third fields in an anonymous array as the value. Except for the 'extension' line which doesn't appear to have a third field, though this is easily catered for if that can be present. If the record matches and the hash is populated, then the hash is pushed onto the array of records, else a diagnostic message is emitted. Finally, I just dumped @records which should make it clear how to access the structure in order to perform the rest of your processing.
#! perl -slw use strict; use Data::Dumper; local $/=''; # Paragraph mode my $re = qr/ ^(Name) \s+ : \s > ( [^<]* ) < \s+ \[ ( [^\x5D]* ) \] \s+ + # 1, 2, 3 ^(IC) \s+ : \s ' ( [^']* ) ' \s+ \[ ( [^\x5D]* ) \] \s+ + # 4, 5, 6 ^(PC) \s+ : \s ( \d* ) \s+ \[ ( [^\x5D]* ) \] \s+ + # 7, 8, 9 ^(Number) \s+ : \s > ( [^<]* ) < \s+ \[ ( [^\x5D]* ) \] \s+ + # 10, 11, 12 ^(Location) \s+ : \s > ( [^<]* ) < \s+ \[ ( [^\x5D]* ) \] \s+ + # 13, 14, 15 ^(extension) \s+ : \s > ( [^<]* ) < \s* + # 16, 17 ^(Capability) \s+ : \s > ( [^<]* ) < (?: \s+ \[ ( [^\x5D]* ) \] )? \s* + # 18, 19, 20 ^(Info) \s+ : \s > ( [^<]* ) < (?: \s+ \[ ( [^\x5D]* ) \] )? + # 21, 22, 23 /mx; my @records; while( <DATA> ) { my %hash; @hash{$1, $4, $7, $10, $13, $16, $18, $21} = ( [$2, $3], [$5, $6], [$8, $9], [$11, $12], [$14, $15], $17, [$19, $20], [$22, $23], ) if m[$re]; if( %hash ) { push @records, \%hash; } else { warn "Record $.\n'$_'\nfailed to match."; } } print Dumper \@records; __DATA__ Name : >Mick< [Mick] IC : '91919191929' [9191919129] PC : 123 [123] Number : >1960132400000< [1960132400000] Location : >000e 0036< [000e 0036] extension : >< Capability : >"CARES< ["CARES] Info : >6005494c523142< [6005494c523142] Name : >Nick< [Nick] IC : '1235467000' [1235467000] PC : 124 [124] Number : >1960192500000< [1960192500000] Location : >000f 0034< [000f 0034] extension : >< Capability : >< Info : >< Name : >Nick< [Nick] IC : '1235467000' [1235467000] PC : 124 [124] Number : >< [00] Location : >000f 0034< [000f 0034] extension : >< Capability : >.< Info : >< Name : >Nick< [Nick} IC : '1235467000' [1235467000] PC : 124 [124] Number : >< [00] Location : >000f 0034< [000f 0034] extension : >< Capability : >.< Info : ><
Output
P:\test>junk Record 4 'Name : >Nick< [Nick} <<< Deliberate error IC : '1235467000' [1235467000] PC : 124 [124] Number : >< [00] Location : >000f 0034< [000f 0034] extension : >< Capability : >.< Info : >< ' failed to match. at P:\test\junk.pl8 line 37, <DATA> chunk 4. $VAR1 = [ { 'Capability' => [ '"CARES', '"CARES' ], 'extension' => '', 'PC' => [ '123', '123' ], 'Number' => [ '1960132400000', '1960132400000' ], 'IC' => [ '91919191929', '9191919129' ], 'Info' => [ '6005494c523142', '6005494c523142' ], 'Location' => [ '000e 0036', '000e 0036' ], 'Name' => [ 'Mick', 'Mick' ] }, { 'Capability' => [ '', undef ], 'extension' => '', 'PC' => [ '124', '124' ], 'Number' => [ '1960192500000', '1960192500000' ], 'IC' => [ '1235467000', '1235467000' ], 'Info' => [ '', undef ], 'Location' => [ '000f 0034', '000f 0034' ], 'Name' => [ 'Nick', 'Nick' ] }, { 'Capability' => [ '.', undef ], 'extension' => '', 'PC' => [ '124', '124' ], 'Number' => [ '', '00' ], 'IC' => [ '1235467000', '1235467000' ], 'Info' => [ '', undef ], 'Location' => [ '000f 0034', '000f 0034' ], 'Name' => [ 'Nick', 'Nick' ] } ];
|
|---|