Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old February 1st, 2001, 07:48 AM
swirling swirling is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Location: New Jersey, USA
Posts: 26 swirling User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 10 m 56 sec
Reputation Power: 0
The script below uses Grep to search a flat db file that contains
$username, $company, $redirect fields in the following format,

jsmith|Yahoo|yahoo.com
jstock|Google|google.com
bdoe|Lycos|lycos.com

The user enters their "username" into a html form.
A "perfect / exact" match will send that person to their specific $redirect variable,
otherwise sending them to an error page.

The problem that I'm having is if the user doesn't enter their "exact" username,
the script is sending the user to the closest match in the string.
(If jstock simply entered "js" he might end up at yahoo.com instead of google.com.

I hope I've explained myself well and I'm looking forward to your reply(s).

Thanks in advance,

-= John


#!/usr/bin/perl
$datafile = "usernames.db";
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/~!/ ~!/g;
$FORM{$name} = $value;
}

$searchstr = $FORM{'username'};

open(INF,$datafile);
@mydata = <INF>;
close(INF);

@results = grep(/$searchstr/i,@mydata);
if ($#results >= 0) {
foreach $i (@results) {
chomp($i);
($username,$company,$redirect) = split(/\|/,$i);
print "Content-type:text/html\n";
print "Refresh:0; URL=http://www.$redirect/\n\n";
print "</body></html>\n";
}
} else {

print "No results found.<p>\n";
}
print "</body></html>\n";




Reply With Quote
  #2  
Old February 1st, 2001, 09:08 AM
mickalo's Avatar
mickalo mickalo is offline
Ole` Timer
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Location: N.W. Iowa
Posts: 469 mickalo User rank is Private First Class (20 - 50 Reputation Level)mickalo User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 5 h 19 sec
Reputation Power: 8
Send a message via AIM to mickalo Send a message via MSN to mickalo
If the script must find an EXACT Match in order to redirect them to the correct URL, I would do something like this:
Code:
$searchstr = $FORM{'username'};
$url = undef;
$user = '';
$prefix="http://www."; 
open(INF,$datafile) || die $!; 
@mydata = <INF>; 
close(INF);
 
  CHECK: foreach $user (@mydata) {
  chomp $user;
  @results = split(/\|/,$user);
  if ($searchstr eq $results[0]) {
      $url = $results[2];
      last CHECK;
     } 
    }

if (defined($url)) {
chomp $url if $url =~ /\n$/;
print ("Location: $prefix$url\n\n");
exit;
 } else {
print ("Content-type: text/html\n\n"); 
print "No results found.<p>\n"; 
} 


Now this will check for an exact username match, generate the redirect url if found.

Hope this helps

P.S disregard the .... in the code!
Mickalo

[Edited by mickalo on 02-01-2001 at 08:28 AM]
__________________

Thunder Rain Internet Publishing

Custom Programming & Database development
Providing Personal/Business
Internet Solutions that work!

Reply With Quote
  #3  
Old February 1st, 2001, 03:21 PM
swirling swirling is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Location: New Jersey, USA
Posts: 26 swirling User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 10 m 56 sec
Reputation Power: 0
Talking

Thank You Mickalo! It works perfectly!

Reply With Quote
  #4  
Old February 1st, 2001, 04:01 PM
mickalo's Avatar
mickalo mickalo is offline
Ole` Timer
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Location: N.W. Iowa
Posts: 469 mickalo User rank is Private First Class (20 - 50 Reputation Level)mickalo User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 5 h 19 sec
Reputation Power: 8
Send a message via AIM to mickalo Send a message via MSN to mickalo
Talking No problems mate, glad to help when I can!

cheers,

Mickalo

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Searching a flat DB file using Grep


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway