Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Adaptive parser for tab delimited text file

by roboticus (Chancellor)
on Nov 01, 2012 at 12:51 UTC ( #1001819=note: print w/ replies, xml ) Need Help??


in reply to Adaptive parser for tab delimited text file

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.


Comment on Re: Adaptive parser for tab delimited text file
Select or Download Code
Re^2: Adaptive parser for tab delimited text file
by choroba (Canon) on Nov 01, 2012 at 13:06 UTC
    Your code does not handle the empty fields correctly. KOFFSETP is always 0 in the data, but is 3 in your output.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      choroba:

      Thanks, I didn't notice. I think I'll leave it alone, though, as you've already posted a working one, and it's only a proof-of-concept anyway.

      ...roboticus

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1001819]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (13)
As of 2015-07-06 18:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (80 votes), past polls