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.
- What could make this better?
- OO approach?
- Less loops?
- /dev/null?
I thank you all in advance.
#!/home/bbq/bin/perl
# Trust no1!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.