On Dec 12, 2003, at 6:50 AM, Larry Sandwick wrote: [..]
I thought it would be done with hashes and every time
I get into hashes I get a headache. I do see that I
have much to learn about Perl especially hashes.

Ok, let's go back over what has been played so far. John has offered One way towards the hash solution. Allow me to Steal from it and from RJN's most excellent analysis and perchance save some of my mungedEgo.

First off a 'hash' is simply an 'associative array'.
which you can think of in terms of

$hash{$key} = $value;

from there things Get Crazy quickly, since in our
case we will want a bit more complex hash something
on the order of

        {
                ID => { letters => string
                                account_id => string
                                date => string
                                items => array_ref_of_strings
                                }
        }

{ don't you just love:
<http://nntp.x.perl.org/group/perl.beginners/56917>
what a Cheat... }

My solution is now up at:
<http://www.wetware.com/drieux/pbl/perlTrick/HA/pile_driver.plx>

I of course opted for a Hash Reference
while keeping my older form of the parser.
The psuedo sql-ing in the foreach afterwards
should suffice:

foreach my $id (keys(%$hash))
{
my $letters = $hash->{$id}->{'letters'};
my $account_id = $hash->{$id}->{'account_id'};
$date = $hash->{$id}->{'date'};

print "update Table account ID=$id and letters=$letters\n";
print "update Table invoice ID=$id and account_id=$account_id and date=$date\n";

foreach my $item (@{$hash->{$id}->{'items'}})
{
print "update Table line_item: $id|$account_id|$date|$item\n";
}
}


Yes, that can be a bit garish at first blush. So let's
go over it a bit. The Keys in the hash_ref are our
"ID" values - those thing that were in column one.
They are in turn a reference to a hash. So to
access the 'letters' we found for 24165 we would
ask for

$hash->{24165}->{'letters'}

so too for the account_id and date elements. That part
is reasonably simple enough, eh no.

No for the

I Drank What?

part - that WAY wacked section

@{$hash->{$id}->{'items'}}

Well, we did say that 'items' was gonna be a reference
to an array, eh no? so we re-reference it with

@{$array_ref}

It's just that we have a Bit Longer 'throw'
to get down into the structure for that 'array_ref'...

May I recommend getting schwartz's learning
perl objects references and modules.



ciao
drieux

---


p0: once upon a time, when I was young and dashing and an undergrad, the class assignment was to convert a punchcard column formated cobol wingDingDingDing style solution into a nice pretty 'c' style data structure and the persons without prior military service said, Who would ever have such a thing, and I thought back to our friend TARF - the tactical reporting form, that was a lovely punchcard column number of columns wide.... and at the time still on active duty... and I began to cry...

p1: my profs promised me, that this was just an
exercise, and this would not happen in the real world...
They were not truthful... and I cry...


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to