CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Help, Testin Data in file with Field in form.

 



CuzDesign
Deleted

May 30, 2000, 10:59 AM

Post #1 of 4 (703 views)
Help, Testin Data in file with Field in form. Can't Post

Hello,
I am sure this has probably been answered here before but I can't find it. Here is my question.

I have a form that writes to a flatfile database. Each ehtry seperated by ":" in the data file.

Here is how the data file is layout.

none:P12364:something:something:something
none:1287R:something:something:something

What I am trying to do is check and see if
one of the form fields is the same as the
data in possition 2 in the data file and if
it is the same then do one thing if it is different do another.

Example:
Field Stock: 1234R

if Stock is the same as any of the data entries in the data file in possition 2 then do one thing if it does not match do another.

The filed may contain both letters and numbers or just numbers.

Any help on this would be greatly appreciated.

I have also thought of it this way.

Writing a seperate datafile just for that field eg.

1234
1254
16789
P1297

and so on then testing it on that file.
But think there has to be away to test the current data file for this.

If you know a tutorial that can help I would be most appreciated.

Thank you,
Ray


perlkid
stranger

May 30, 2000, 12:23 AM

Post #2 of 4 (703 views)
Re: Help, Testin Data in file with Field in form. [In reply to] Can't Post

 
I remember this method to be the one that can do that.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open(DATA, "</path/file.db");
@all=<DATA>;
close(DATA);

foreach $line (@all)
{
@array=split(/:/, $line);
if ($array[1] eq "$array[3]")
{
do something
}
}
</pre><HR></BLOCKQUOTE>

Note that $array[0] is the first field in the data base and so on. Also this code is case sensitive but it does exact matching. If you want it to still do exact matching but be case insenitive than do this.


<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open(DATA, "</path/file.db");
@all=<DATA>;
close(DATA);

foreach $line (@all)
{
@array=split(/:/, $line);
$field2 = lc($array[1]);
$field4 = lc($array[3]);
if ($field2 eq "$field4")
{
do something
}
}
</pre><HR></BLOCKQUOTE>

So Give That A Try,

Good Luck

perlkid


CuzDesign
Deleted

May 30, 2000, 1:50 AM

Post #3 of 4 (703 views)
Re: Help, Testin Data in file with Field in form. [In reply to] Can't Post

Thanks perlkid for the fast reply. I tried
what you gave me here and works great for the testing the first code you put up, cause
I need it to be case sensative. The only
problem I am having is while it is testing
it just keeps looping even after it has found a match.

How do you make it stop after it finds a match?

Here is the code I am using as you put it in:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$stock = param('stock');

open(DATA, "<cars.dat");
@all=<DATA>; close(DATA);
foreach $line (@all){
@array=split(/:/, $line);
if ($array[1] eq "$stock"){
print "Stock number already exsists\n";
}else{
print "Stock number added\n";
}
}
</pre><HR></BLOCKQUOTE>

That causes a loop to print to the page so I tried it this way and doesnt test all, but
always says already added even when not in
data file:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$stock = param('stock');

open(DATA, "<cars.dat");
@all=<DATA>;
close(DATA);
foreach $line (@all){
@array=split(/:/, $line);
if ($array[1] eq "$stock") {
$good = 1;
last;
}
}

if (!$good) {
print "Stock number already exsists\n";
}else {
print "Stock number added\n";
}
</pre><HR></BLOCKQUOTE>

Can anyone see why this doesn't work?

Thanks,
Ray




CuzDesign
Deleted

May 30, 2000, 8:32 AM

Post #4 of 4 (703 views)
Re: Help, Testin Data in file with Field in form. [In reply to] Can't Post

Thanks for the input you gave me the first time.

After messing with it for a bit I figured out
what I was doing wrong. If anyone else was
interested this is what I came up with.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$stock = param('stocknum');

my $line;
open (DATA,"<cars.dat") or die "Couldn't open cars.dat $!\n";
while (<DATA> ){
chomp;
my @list = split(/:/,$_);
if ($list[1] eq $stock){
$good = 1;
}
}
if ($good == 1) {
print "Match\n";
}else{
print "No Match\n";
}
</pre><HR></BLOCKQUOTE>

Again thank you for your time.

Ray

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives