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

i'm restating my problem, because i tried out each of the suggestions, and i couldn't get where i need to go with any of them. i am very much a beginner and am working hard on this, but spinning my wheels - i appreciate the help.

i have a table like this: rowid course1 course2 course3 course4 marge psychology math engineering pe roy math reading art cooking etc. one of these fields will have lots of text with line breaks. i have a form. the form will send a request to a cgi, passing a value + which matches a value in "rowid" from the table. i need to open the file that contains the table, find the row that mat +ches the value passed by the form, read that row into an array (or ha +sh?) and print it out in html. i'm able to open the file and put it into an array and print it, but i + don't know enough perl yet to take just the row i want and assign la +bels to each value.

Replies are listed 'Best First'.
Re: Re: Re (tilly) 3: hash/array
by eg (Friar) on Jan 26, 2001 at 13:01 UTC

    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.

      thanks tilly :) that worked. first success in 2 days. i appreciate the help.
        Actually that was not me, it was eg...
        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
      thanks eg. i guess i thanked tilly for this one by mistake. your solution worked well.