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

Okay, use <> (see "I/O Operators" in the perlop manpage) to read successive lines of your table. For each line you read, use split (perlfunc)to split up the line into an array (perldata) and if (perlsyn) to check if it's the right one. If it is, you can use a hash slice (perldata again) to easily assign the row's values to a hash to be used.

use strict; use Data::Dumper; my $rowid = $ARGV[0] || 'roy'; my %data = (); my @fields = split(/\t/, <DATA>); chomp @fields; while(<DATA>) { chomp; my @row = split(/\t/); if ($row[0] eq $rowid) { @data{@fields} = @row; last; } } if ( keys %data ) { # found, display data print Dumper \%data; } else { # not found, show error print STDERR "Can't find row '$rowid'\n"; } __DATA__ rowid course1 course2 course3 course4 marge psychology math engineering pe roy math reading art cooking

So now you'll need to replace reading from DATA with reading from an open filehandle, use CGI to get the $rowid passed from the form and put whatever HTML you want in the found/not found sections.

Good luck.

Replies are listed 'Best First'.
Re: Re: Re: Re (tilly) 3: hash/array
by Anonymous Monk on Jan 26, 2001 at 23:17 UTC
    thanks tilly :) that worked. first success in 2 days. i appreciate the help.
      Actually that was not me, it was eg...
        oops. well, thanks for your help too!
      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.
Re: Re: Re: Re (tilly) 3: hash/array
by malaga (Pilgrim) on Feb 02, 2001 at 23:23 UTC
    thanks eg. i guess i thanked tilly for this one by mistake. your solution worked well.