in reply to Re: Re: hash/array
in thread hash/array

What is good for the goose is good for the gander.

You should do like perlstyle says and include all relevant information in the error message. In this case that includes the filename:

my $file = "home/you/your.file"; open (FILE, "< $file") or die "Cannot read '$file': $!";
If this appears within a function I would normally use Carp and then confess to the sad situation...

Replies are listed 'Best First'.
Re: Re (tilly) 3: hash/array
by malaga (Pilgrim) on Jan 26, 2001 at 10:34 UTC
    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.

      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.
        thanks eg. i guess i thanked tilly for this one by mistake. your solution worked well.