Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: push

by malaga (Pilgrim)
on Feb 01, 2001 at 05:27 UTC ( [id://55624]=note: print w/replies, xml ) Need Help??


in reply to push

i posted this question for tilly but i should have posted it here so everyone can see it. please excuse the duplication
i have been trying to tweak this script to cover one more aspect of what i am doing - i need to search for all rows that have a particular id, not just the first one. here's where the script is:
open FILE , "/courses.txt" or die "Cannot open: $!"; my $couvalue = param('name'); my $courowid = $ARGV[0] || $couvalue; my %coudata = (); my @coufields = split(/\t/, <FILE>); chomp @coufields; while(<FILE>) { chomp; my @courow = split(/\t/); if ($courow[0] eq $courowid) { @coudata{@coufields} = @courow; last; } } close (FILE); print header; if ( keys %coudata ) { # found, display data print ul( map { li("$_: $coudata{$_}") } keys %coudata); } else { # not found, show error print h1("Can't find row '$courowid'"); } #######this is what the table looks like: 1414 "Nutitional Epidemiology" 1414 "Nutritional assessment" 1414 "Undernutrition in the United States" 1371 "Health Politics and Policy" 1371 "Advanced Health Politics?" 1371 "Introduction to Health Policy & Management" 1371 "Health and Public Policy Seminar"
i only get one row back.
i've tried taking out "last;"
i've tried to do a foreach instead of an if, but i can't get it to work. i've even posted another sopw, in trying to use "push" to get all the lines that match into one array. can this script work for this problem, and if so, where should changes be made?

thank you for your help.
malaga

Replies are listed 'Best First'.
Re: Re: push
by a (Friar) on Feb 01, 2001 at 12:19 UTC
    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

      thanks a, this was really helpful. - malaga

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://55624]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-03-28 11:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found