Start w/: use strict, use perl -w. Trust us, it'll really
help you here. But, enabler that I am: you're mixing up
your arrays and hashes. You're also trying to read
FILE twice:
my @coufields = split(/\t/, <FILE>);
should eat all of FILE, leaving nothing left for the
while loop. Leave that line out and try:
my $debug = 5;
print STDERR "Looking for id $courowid \n" if $debug > 8;
while (<FILE>) {
chomp;
my ($id, $course) = split(/\t/);
print STDERR "trying id $id, course $course\n" if $debug > 8;
if ( $id == $courowid ) {
print STDERR "found id $id, course $course\n" if $debug > 5;
# if we match the ID, push the course name onto the
# the array in the coudata hash, key $id
push @{$coudata{$id}}, $course;
}
}
print header;
if ( %coudata ) {
# found, display data by taking each course back outof
# the array
foreach my $id ( keys %coudata ) {
print STDERR "show id $id\n" if $debug > 6;
foreach my $course ( @{$coudata{$id}} ) {
print STDERR "show id $id, course $course\n" if $debug > 8;
print ul("$id: $course");
} # foreach my course
} # foreach my id
}else {
# not found, show error
print h1("Can't find row '$courowid'");
}
Its a big step in perl, but names are only
half the battle: %coudata and $coudata{14146} are
related but the first is the entire hash, the latter a
single element found by key '14146'. @coudata is
an entirely different kettle of data, its an array and
the element $coudata[0] has no relation to the
hash element $coudata{0}; just as @coudata doesn't have
any relation to %coudata.
Plus, your variable names are troublesome: use
english-ish things:
course_list, course_id, search_id etc. You'll
avoid typos (courdata coudata) and it'll make
more sense. Just 'cause you can, doesn't mean
its a good idea to to name an array and
a hash the same thing. Its generally a bad idea,
esp. early on. A few more keystrokes won't kill ya'
a
In reply to Re: Re: push
by a
in thread push
by malaga
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.