A while ago I had submitted to the snippets section a, well, snippet, called Open Flat File. The idea is to provide an x/y mechanism of navigating through data of pseudo-CSV files. Last night, one of my co-workers had a few questions about hashes, flat files, chomp, and miscelanous debris so I decided to pull the snippet back up. While I was trying to explain what it did, and how one should always use strict and -w it struck me: dog, I'm so lame!

So, here is my second revision of Open Flat File, with a few minor changes allowing to run under -w and use strict; Although it works, I feel very uncomfortable looking at it the way it is, and would very much appreciate any ideas that other monks may have on it. Here we go:
sub OpenFF { my %hash; my ($file,$dlmt) = @_; $dlmt = "\t" if ! $_[1]; open(READ,$file) or return(0); my @file = <READ>; chomp(@file); close(READ); my @headers = split($dlmt,shift(@file)); foreach my $line (@file) { my $x = 0; my @columns = split($dlmt,$line); while ($x < scalar @headers) { $hash{$headers[$x++]}{$columns[0]} = $columns[$x]; } } return(%hash); }
Having that said, please consider this sample file:
foo.txt ------- ID NAME1 NAME2 AGE 1 donald duck 50 2 mickey mouse 48 3 peter pan 62 4 madre theresa 108 5 banana split 2
And this sample script:
#!/usr/bin/perl -w use strict; my %foo = OpenFF('foo.txt') or die("Couldn't parse foo.txt, $!\n"); print "The keys are:\n"; print join("\t",keys %foo)."\n"; print "Row 3 is:\n"; foreach (qw'ID NAME1 NAME2 AGE') { print "$foo{$_}{'3'}\t"; } print "Column 'NAME2' is:\n"; foreach (1..5) { print "$foo{'NAME2'}{$_}\n"; }
...and the output would be:
The keys are: NAME1 NAME2 ID AGE Row 3 is: 3 peter pan 62 Column 'NAME2' is: duck mouse pan theresa split
It is my opinion that this could be vastly explored and improoved, but at this point I have reached a point where I can't go any further without suggestions from my fellow monks. I thank you all in advance.

#!/home/bbq/bin/perl
# Trust no1!

In reply to Another flatfile thingy by BBQ

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.