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

I a seeking the knowledge for a way to assign a single HTML text field 2 names such as 'keywords' and 'search'. I am perl based and use Fcntl.
$main_template =~ s/%%keywords%%/$fields{'keywords'}/g; $main_template =~ s/%%searcresults%%/$pitem/g;
$main_template =~ s/%%keywords%%/$fields2{'search'}/g; $main_template =~ s/%%premiumlistings%%/$pitem/g;
This along with 3 already implemented codes combined will aid me in my agenda.

Edited by Chady -- fixed typo in title

Replies are listed 'Best First'.
Re: Multiple HTML field names
by tilly (Archbishop) on May 08, 2004 at 01:37 UTC
    First of all you really should use an existing templating system. Lots exist, and they've already solved lots of problems that you're going to run into.

    That said, you can tackle this one by doing one substitution that puts in both:

    $main_template =~ s/%%keywords%%/$fields{'keywords'} $fields2{'search' +}/g;
Re: Multiple HTML field names
by Roger (Parson) on May 08, 2004 at 10:53 UTC
    I often do something similar to what you are doing, but with a callback function on the replacement side, together with the /e (evaluation) modifier.
    #!/usr/bin/perl -w use strict; my $main_template = do { local $/; <DATA> }; $main_template =~ s/%%([^%]+)%%/Callback($1)/ge; print $main_template, "\n"; sub Callback { my $field = shift; my $val; our $keycount; if ($field eq 'searchresult') { $val = 'SEARCH RESULT HERE'; } elsif ($field eq 'premiumlistings') { $val = 'PREMIUM LISTINGS HERE'; } elsif ($field eq 'keywords') { $keycount++ ? do { $val = 'FIELD(SEARCH)' } : do { $val = 'FIELD(KEYWORDS)' }; } return $val; } __DATA__ <HTML> <BODY> %%keywords%% <BR/> %%searchresult%% <BR/> %%keywords%% <BR/> %%premiumlistings%% <BR/> <BODY> </HTML>

    And the output -
    <HTML> <BODY> FIELD(KEYWORDS) <BR/> SEARCH RESULT HERE <BR/> FIELD(SEARCH) <BR/> PREMIUM LISTINGS HERE <BR/> <BODY> </HTML>

      This was initially supposed to be a single flat file search utilizing the file handle <SIDX> and <SIDX2>. I have implemented past knowledge (successful) yet I have found that after implementation, I may have still painted myself in a corner. I have been told that most questions can be answered within the first 20 lines of text, however, this is not the case. Any knowledge would be appreciated. The result: A Google type search result with standard listings in 1 column, and premium listings in the other. <SIDX2> serves as the db for premium listings. You are on the right track, however, I feel I need to supple efficient code for you to access what I am actualy trying to do. I will provide a link the text file as it may be considered rude (bandwidth?) to submit full code here. This is a typefull for me, however, someone more knowledgeable may be able to spread (speed read)

      http//:www.urhosted.com/search.txt

        First, thanks for your response. I have not not been able to successfully integrate it into my code. I have however been able to successfully open both files, read them simultaneously and print out independent file content results (keywords not in SIDX, are in SIDX2 by design). I now need to be able to regain the keyword search results in @skeyw, and split them into 2 return results. One will be inserted the html placeholder %%searchresults%% and the other in premiumresults:
        $main_template =~ s/%%keywords%%/$fields{'keywords'}/g; $main_template =~ s/%%searcresults%%/$pitem/g; $main_template =~ s/%%keywords%%/$fields{'keywords'}/g; $main_template =~ s/%%premiumresults%%/$pitem/g; print "Content-type: text/html\n\n"; $main_template = tseek::insert_tmpl_includes($main_template); print "$main_template"; exit;
        Here is where I do the search:
        sub normal_search { ### PERFORM SEARCH $search_line = $fields{'keywords'}; $icnt = 0; $toadk = ""; (@skeyw) = split(/ /,$search_line); $nrkeywords = 0; foreach $item (@skeyw){$nrkeywords++;} open (SIDX, "$data_dir/search.idx"); open (SIDX2, "$data_dir/search2.idx"); if ($file_locking ne "No"){flock (SIDX, LOCK_SH) or die "Can't set lo +ck for file: $data_dir/search.idx, $data_dir/search2.idx $!\n";} while($line3 = <SIDX>) { $line2 = <SIDX2>; $sline = "${line2}${line3}"; foreach $kwr (@skeyw) { if (($sline =~ /$kwr/i) and ($kwr ne "")) { $toadk = "true"; } } if ($toadk eq "true") { $resultline[$icnt] = "${line2}"; $premium_resultline[$icnt] = "${line3}"; $toadk = false; $icnt++; } } #if ($file_locking ne "No"){flock (CIT, LOCK_UN);} close (SIDX); close (SIDX2); }
        and here is where I need to split the keyword search results from both files that are still contained in @skeyw. Search results from <SIDX> assigned to ($search_line) = @_; and the search results from <SIDX2> assigned to ($premium_search_line) = @_;
        sub get_search_ready { my ($search_line) = @_; $reline = $search_line; $reline =~ s/[+\[\]()*^.\$?\\~<>;]//g; return ($reline); } sub get_search_ready2 { my ($premium_search_line) = ${line2}; $premium_reline = $premium_search_line; $premium_reline =~ s/[+\[\]()*^.\$?\\~<>;]//g; return ($premium_reline); }