Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

reaper9187:

If you don't mind storing your data in a hash, it doesn't have to be adaptive. Instead you can parse the line pairs as hash keys & values respectively. Something like this:

$ cat t.pl #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %CELLS; my $tmp; my $curCELL; my $curCELLR; while (<DATA>) { # skip blank lines next if /^\s*$/; # Line isn't empty, so current line is a list of field names # and the next line is a list of the values my @names = split /\s+/, $_; my @values = split /\s+/, <DATA>; $curCELL = $values[0] if $names[0] eq 'CELL'; $curCELLR = $values[0] if $names[0] eq 'CELLR'; # Store the name/value pairs (if we have both keys) next unless defined $curCELL and defined $curCELLR; @{$CELLS{$curCELL}{$curCELLR}}{@names} = @values; } print Dumper(\%CELLS); __DATA__ NEIGHBOUR RELATION DATA CELL LUC325C CELLR DIR CAND CS LUC325B MUTUAL BOTH NO KHYST KOFFSETP KOFFSETN LHYST LOFFSETP LOFFSETN 3 0 3 0 TRHYST TROFFSETP TROFFSETN AWOFFSET BQOFFSET 2 0 5 3 HIHYST LOHYST OFFSETP OFFSETN BQOFFSETAFR 5 3 0 3 CELLR DIR CAND CS LUC116A MUTUAL BOTH NO KHYST KOFFSETP KOFFSETN LHYST LOFFSETP LOFFSETN 3 0 3 0 TRHYST TROFFSETP TROFFSETN AWOFFSET BQOFFSET 2 0 5 3 HIHYST LOHYST OFFSETP OFFSETN BQOFFSETAFR 5 3 0 3 CELLR DIR CAND CS LUC204A MUTUAL BOTH NO KHYST KOFFSETP KOFFSETN LHYST LOFFSETP LOFFSETN 3 0 3 0 TRHYST TROFFSETP TROFFSETN AWOFFSET BQOFFSET 2 0 5 3 HIHYST LOHYST OFFSETP OFFSETN BQOFFSETAFR 5 3 0 3 CELL LUC082B

When I run it, I get the following:

$ perl t.pl $VAR1 = { 'LUC325C' => { 'LUC204A' => { 'KHYST' => '', 'OFFSETN' => '0', 'TROFFSETN' => '0', 'OFFSETP' => '3', 'LOFFSETN' => undef, 'DIR' => 'MUTUAL', 'LOHYST' => '5', 'LHYST' => '3', 'CAND' => 'BOTH', 'BQOFFSET' => '3', 'LOFFSETP' => '0', 'CELLR' => 'LUC204A', 'KOFFSETN' => '0', 'KOFFSETP' => '3', 'TRHYST' => '', 'HIHYST' => '', 'CS' => 'NO', 'AWOFFSET' => '5', 'BQOFFSETAFR' => '3', 'TROFFSETP' => '2' }, 'LUC116A' => { 'KHYST' => '', 'OFFSETN' => '0', 'TROFFSETN' => '0', 'OFFSETP' => '3', 'LOFFSETN' => undef, 'DIR' => 'MUTUAL', 'LOHYST' => '5', 'LHYST' => '3', 'CAND' => 'BOTH', 'BQOFFSET' => '3', 'LOFFSETP' => '0', 'CELLR' => 'LUC116A', 'KOFFSETN' => '0', 'KOFFSETP' => '3', 'TRHYST' => '', 'HIHYST' => '', 'CS' => 'NO', 'AWOFFSET' => '5', 'BQOFFSETAFR' => '3', 'TROFFSETP' => '2' }, 'LUC325B' => { 'KHYST' => '', 'OFFSETN' => '0', 'TROFFSETN' => '0', 'OFFSETP' => '3', 'LOFFSETN' => undef, 'DIR' => 'MUTUAL', 'LOHYST' => '5', 'LHYST' => '3', 'CAND' => 'BOTH', 'BQOFFSET' => '3', 'LOFFSETP' => '0', 'CELLR' => 'LUC325B', 'KOFFSETN' => '0', 'KOFFSETP' => '3', 'TRHYST' => '', 'HIHYST' => '', 'CS' => 'NO', 'AWOFFSET' => '5', 'BQOFFSETAFR' => '3', 'TROFFSETP' => '2' } }, 'LUC082B' => { 'LUC204A' => { 'CELL' => 'LUC082B' } } }; $

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: Adaptive parser for tab delimited text file by roboticus
in thread Adaptive parser for tab delimited text file by reaper9187

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (7)
    As of 2014-12-28 21:07 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (182 votes), past polls