http://qs321.pair.com?node_id=55668


in reply to Re: push
in thread push

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

Replies are listed 'Best First'.
Re: Re: Re: push
by malaga (Pilgrim) on Feb 02, 2001 at 03:23 UTC
    thanks a, this was really helpful. - malaga