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' ] } ];

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller



In reply to Re: Can't read files content by BrowserUk
in thread Can't read files content by bh_perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.