At 14:44 25.06.2001 -0400, Brian Bukeavich wrote:
>Thanks for the input.
>My input data (which is coming out of a flat ascii file) looks similar to:
>Jones, John 35 20 02 05/02/2001 F 060506050705040405047 11 04 01
>Jones, John 35 20 02 05/09/2001 F 050604050705040405045 10 13 02
>Jones, John 35 20 02 05/16/2001 F 050505040704040509048 09 01 03
>
>How could I "pass this function an array, or format the string for a
>split() or something"?
open (IN, "file") || die("Couldn't open file $!");
while(<IN>)
{
chomp;
my @asLines = split(/[\t\s]+/, $_);
Load_Stat_Rec(\@asLines);
}
close IN;
and now your function declaration becomes
sub Load_Stat_Rec($)
{
my($raLines) = @_;
# now you don't need to cut up the string, you just access the
index of the array that you want.
$name[$i] = $raLines->[0];
$course[$i] = $raLines->[1];
...
...
}
But I think you need to take a look at how you're storing this
information. First of all, your global $i is dangerous and not very
clear. If you need to set up a structure that remembers all the data you
pick up from the file, much better a hash of arrays ie
my %hFileInfo =
(
name => [];
course => [];
tee => [];
...
...
);
now you have an hash (associative array) that contains an entry for all of
your fields. Each entry is a reference to an array that will pick up all
of your file input. So your function becomes:
sub Load_Stat_Rec($)
{
my($raLines) = @_;
# now you don't need to cut up the string, you just access the
index of the array that you want.
push(@{ $hFileInfo{name} }, $raLines->[0]);
push(@{ $hFileInfo{course[} }, $raLines->[1]);
...
...
}
which is still WAY too verbose and not very elegant, but at least now
you've got a structure you can work with easily.
If you have trouble following my syntax, it breaks down like this
%hFileInfo -- this is a hash, or associative array. That means it is
composed of name value pairs (name=Joe, age=21, etc.). If I make a simple hash
my %hHash =
(
name => "Frank",
age => 21,
);
I can get the value of an entry (or key) out like so
print $hHah{name}; # prints Frank
Your hash contains arrays at each entry. So, you want to push() each value
onto it, building your array as you go.
push(@{ $hFileInfo{name} }, $raLines->[0]);
you have to tell perl to put your hash value 'name' into list (or array)
context to use the function push() on it, as push only works with an array,
and you do that as in the example above, putting it inside @{} (some people
prefer @$hFileInfo{name}, but I think the curly brackets make it more
obvious that you meant to do that and that it isn't a typo, and makes it
easier to read)
>Another thing I would really like to clean up is the redundancy in output...
>is there a way I can write to both:
> printf OF (" %3u", @hole[$q]);
> printf OF2 (" %3u", @hole[$q]);
Perhaps you're writing to two files needlessly? Or, if you need two
copies, can't you write to one and then copy it to another file at the
end? Why are you writing the same thing to two files?
Aaron Craig
Programming
iSoftitler.com