[HRPM] Perl beginner looking for a little help

Collin Cusce' cusce at home.com
Mon May 21 22:24:45 CDT 2001


    Firstly, I would never store something this immense that is only going
to grow more in a flat file. If you are willing to learn a bit about
databases (if you dont know it already) , you might want to consider
transfering your data into one and just run SQL queries on that. Postgres is
my personal choice, but MySQL will do your needs nicely. Trust me, this
makes life a WHOLE lot easier and your data a whole lot less corruptable
(maybe that is just a false sense of security on my part, though). But I
digress.
    Unfortunatly, pretty much the full extent of Perl's data structures has
been hacked together by Larry Wall as simple, quick fixes. There isnt true
datatyping in perl ie: nothing like a 'struct' or a real 'class' (from what
I understand Perl 6 will have this, though 8-). There is an example in
Advanced Perl Programming by O'Reilly that seems to be very similar to the
problem you have. I'll include the source from the book and the data it
takes in below, but I'll address your problem, too.
    Ok, so you want to create a complex datastructure in Perl. Firstly, you
need to create a sort of consrtuctor function that will alter a main data
srtucutre based on the values you pass into it.You will need a main
datastrucutre, array or hash, that will contain all the data and then you
build functions to navigate that datascructure.  This is a quick job I threw
together in a few minutes which probably wont work to your needs and is
buggy, but hopefully it will give you the idea.
                my(%games,%teams);
                #parameters for enter_game passed in as follows:
                    #enter_game($date, \$teams{$vis_team},
\$teams{$home_team}, $vscore,$hscore,$ratio);
                sub enter_game{
                   my($date, $visitors,$home,$vscore,$hscore,$ratio) = @_;
                   %thisgame = {
                        'date' => $date,
                        #note, the next to values are references to the
teams, not a copy
                        'visitor' => $visitors,
                        'home' => $home,
                        'vis_score' => $vscore,
                        'home_score' => $hscore,
                        'ratio' => $ratio
                   };
                $keyname = $visitor->$name."vs".$home->$name."on:"$date;
                $games{$keyname} =  \$thisgame;
                };
                sub enter_team {
                    my($name,$won,$lost,$tied,$pf,$pa,$rating);
                    %team{$name} = {
                        'won' => $won,
                        'lost' => $lost,
                        'tied' => $tied,
                        'pf' => $pf,
                        'pa' => $pa,
                        'rating' => $rating
                    };
                };
I hope that is what you are looking for. It most definetly has bugs in
it....sorry. Also, this might not be the best way to do this. I dont use
Perl nearly as much as I'd like to, and I'm responding mostly as a refresher
for myself.

I will try and attatch the example from Advanced Perl in hopes the mailer
accepts attatchments. If not, I'll send out another post with the source in
the text. I hope I helped!
-------------- next part --------------
A non-text attachment was scrubbed...
Name: oscar.pl
Type: application/octet-stream
Size: 1311 bytes
Desc: not available
Url : http://mail.pm.org/archives/norfolk-pm/attachments/20010521/f2f7b6e4/oscar.obj
-------------- next part --------------
1995:Actor:Nicholas Cage
1995:Picture:Braveheart
1995:Supporting Actor:Kevin Spacey
1994:Actor:Tom Hanks
1994:Picture:Forrest Gump
1928:Picture:WINGS



More information about the Norfolk-pm mailing list