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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.