Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello
I have a .html file with 22 form fields that I want to match against a .csv file that I have opened into an array.

Previously I have only used IF's but this isn't going to be practical here as I want to match against all of the 22 fields the user decides to use.

Incoming data is like this only times 22: $from = param('from');

And where I am at with the matching part is:

open(DB, "</location/db.csv") || Error ('open','file'); while ($line = <DB>) { @split_sp_data = split(/,/, $line); if ($from =~ /@split_sp_data[1]/) { print qq (<a href="$split_sp_dat +a[0]">$split_sp_data[0]</a>); } } close(DB);

Can anyone advise of the best way to approach this.
Thanks Si

Replies are listed 'Best First'.
Re: Matching multiple variables against array
by ikegami (Patriarch) on Feb 28, 2006 at 17:52 UTC

    Something like this?

    my @form = map { $cgi->param("form$_") } 0..21; while (my $line = <DB>) { my @split_sp_data = split(/,/, $line); for my $i (0..21) { if (index($split_sp_data[$i], $form[$i]) >= 0) { print(qq(<a href="$split_sp_data[$i]">$split_sp_data[$i]</a>) +); } } }

    index is faster than regexps when search for a static string (as opposed to a regexp).

    Update: oops! Changed index(...) != 0 to index(...) >= 0. Thanks, InfiniteSilence.

Re: Matching multiple variables against array
by Anonymous Monk on Mar 01, 2006 at 00:16 UTC

    I can read neither your code nor your English, but I gather that your question has something to do with processing a CSV file.

    Please look at Tie::CSV_File

      Thanks for your help

      I am confused by this bit tho and how I can get the different form values into @form

      my @form = map { $cgi->param("form$_") } 0..21;

      When trying to run this at the mo. I get the error message: Can't call method "param" on an undefined value line 31 which is where the above line sits.

      Thanks again
        When trying to run this at the mo. I ...

        What do you mean by "mo."?

        It seems you have been using only the functional interface of (i assume) CGI.pm or equivalent module. Object oriented interface was used in ikegami's reply, where $cgi was some CGI* object. If you just use param( ... ) instead of $cgi->param( ... ), you should get moving along.

        Also, it looks like you are not using warnings & strict pragmas. If you were, you would have seen different error message.