in reply to Re: Re: Re (tilly) 3: hash/array
in thread hash/array

thanks tilly :) that worked. first success in 2 days. i appreciate the help.

Replies are listed 'Best First'.
Re (tilly) 7: hash/array
by tilly (Archbishop) on Feb 02, 2001 at 10:36 UTC
    Actually that was not me, it was eg...
      oops. well, thanks for your help too!
Re: Re: Re: Re: Re (tilly) 3: hash/array
by malaga (Pilgrim) on Feb 01, 2001 at 02:29 UTC
    hi tilly,
    i have been trying to tweak the script you wrote 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.
    i wanted to ask you if this script can work for this problem, and if so, where should changes be made.

    thank you for your help.
    malaga
      In general, you need an array of records.
      my @records; while(<FILE>) { chomp; my @courow = split(/\t/); if ($courow[0] eq $courowid) { my %coudata; @coudata{@coufields} = @courow; push @records, \%coudata; } } # and further down for my $ref (@records) { my %coudata = %$ref; print ul( map { li("$_: $coudata{$_}") } keys %coudata); print ("<br>" x 3); }
      Of course I'm not suggesting you use that as is.
        that did the trick. thanks! it looks like this now:
        sub getrows { my @records; my $value = param('name'); my $rowid = $ARGV[0] || $value; my %data = (); my @fields = split(/\t/, <FILE>); chomp @fields; while(<FILE>) { chomp; my @row = split(/\t/); if ($row[0] eq $rowid) { my %data; @data{@fields} = @row; push @records, \%data; } } close (FILE); print header; for my $ref (@records){ my %data = %$ref; print ul( map { li("$data{$_}") } keys %data); } }
        now i have to make it print nicely in tables. back to work.