Rob Richardson wrote: > > Greetings! Hello,
> I am trying to write a Perl script for the Cuyahoga Valley Scenic > Railroad, so that volunteers can sign up to work on trains. The > schedule file has comma-separated lines that have the date of the > train, the name of the train, and the names of people who will be > working on the train (or empty entries, if nobody has signed for that > position yet). The date is a string of the form "MM.DD.YYYY". Why? If you store the date in a YYYYMMDD format you can sort it as is. > There > can be one or more trains for each date. Currently, there are never > more than two trains on one day, but I can't assume that, especially > since I know we're extending our route next season and another > extension could happen in a few years. I also can't assume that the > number of positions per train will remain constant. > > So, I am trying to build a data structure to hold the trains. I figure > the best way would be to use a hash keyed by the date string. The > values of the hash would be arrays, with one element per train. Each > array would contain the list of positions for the train. > > I tried to use a data structure as described in Sam's Teach Yourself > Perl in 21 Days. I am tripping over my own feet. Here's the code: > > while (<SCHEDULE>) > { > my ($trainsOnDate); > > chomp; # remove newline > @trainData = split /,/; > $trainDate = shift trainData; > > if (not exists $trainList{$trainDate}) > { > # Create the hash element, giving it an empty anonymous array > $trainList{$trainDate} = []; There is no need for this test or assignment as perl will do the right thing. > } > > # Create a reference to the train data > # $trainList{$trainDate} is a reference to the array of trains > $trainRef = \@trainData; > $trainsOnDate = \trainList{trainDate}; > > push $trainsList{$trainDate}, $trainRef; ^^^^^^^^^^^^^^^^^^^^^^^ This has to be an array (something that starts with @). > } > close (IN); > > This gave me the following error: > Type of arg 1 to push must be array (not hash element) at > c:\INDIGO~1\HTDOCS\CREW\CALENDAR.CGI line 246, near "})" This could be written as: while ( <SCHEDULE> ) { chomp; # remove newline my ( $trainDate, @trainData ) = split /,/; push @{$trainsList{$trainDate}}, @trainData; } close IN; John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]