Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Fetch Data From DataFile In perl

Status
Not open for further replies.

6681510

Programmer
Aug 29, 2007
16
FI
Hi iam new in perl i have a data file which looks like

(this is value) (price) (Currency)
| | |
FCWQBF1NVBAIN 734,54 SEK
FCWQBF1NVBBIN 550,91 SEK
FCWQBF1NVBCIN 462,76 SEK
FCWQBF1NVBEIN 315,95 SEK
FCWQBF1NVBHIN 146,93 SEK
. | |
. tab tab
.
Now what i want to do here is when i select a value from dropdown list from a web form then the program should return a price from the datafile

if some body have some ideas how to do that please help me
 
#Since you didn't ask I'm assuming you know how to get the first data there.. if not look at use CGI in perl.

#Not tested in any way!

$input = $q->param(datafromdropdown);

#Open data file and read it to an array
open(FILE, "</path/to/file") or die "Can't open /path/to/file:$!\n";
@file = <FILE>;
chomp @file;
close FILE;

for $line (@file) {
#Split each line by white space
@tmp = split /\s+/, $line;
#Check if first data is equal and if it is print second values
if ($tmp[0] eq $input) {
print "$tmp[1]\n";
}
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Thanks man you have given very quick responce but actually there is one little problem that my datafile is very big it has like 50000 rows so now the program is crashing and taking too much time to fetch the data and put it in array . so now the code i have is below .

#!/usr/bin/perl -w

use CGI qw:)all);


print header;

##creating html page
print start_html('Offer Calculator'),
h1(' Offer Calculator'),
start_form,
print '<TR><td class="text">';

print"<select name='Product' >";
print "<option value='FCWQBF1NVBAIN' class='text' checked='checked'>'Product1'<br>";
print "<option value='FCWQBF1NVBBIN' class='text'>'Product2'<br>";
print "<option value='FCWQBF1NVBCIN' class='text'>'Product3'<br></td><td class='text' valign='top'>\n";
print"</select>";
end_form,
my $input = param('Product');

#Open data file and read it to an array
open(FILE, "C:/usr/Offer_Calculator/M_Offer_cal/pricedata_se_siebal.txt") or die "Can't open C:/usr/Offer_Calculator/M_Offer_cal/pricedata_se_siebal.txt:$!\n";
@file = <FILE>;
chomp @file;
close FILE;

for $line (@file) { # if there is another way to fetch the data from datafile which is much more quicker
#Split each line by white space
@tmp = split /\s+/, $line;
#Check if first data is equal and if it is print second values
if ($tmp[0] eq $input) {
print "$tmp[1]\n";
}
}
print end_html;



So if u can Just tell me how to do deal with huge text files that.


But Thanks Very very Much about ur Quick reply
 
open(FILE, "C:/usr/Offer_Calculator/M_Offer_cal/pricedata_se_siebal.txt") or die "Can't open C:/usr/Offer_Calculator/M_Offer_cal/pricedata_se_siebal.txt:$!\n";

while(<FILE>) { # if there is another way to fetch the data from datafile which is much more quicker
#Split each line by white space
@tmp = split /\s+/, $_;
#Check if first data is equal and if it is print second values
if ($tmp[0] eq $input) {
print "$tmp[1]\n";
}
}

close FILE;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 

#####Display Text Box where Veli can Input The message by him self########

my $password;
my $Message;
print start_html('Massage'),
start_form,

print "<table align=center width=600 border=0 cell padding=3 cellspacing=1>";
print TR(td(br,'Message',textfield(-name=>'Message', -size=>'50',-maxlength=>'50')));
print TR(td(br,'Password',password_field(-name=>'Password', -size=>'10',-maxlength=>'10')));
print TR(td(br,submit(-name=>'Submit',-value=>Submit)));
print "</table>";
end_form,
$password = param('Password');
$Message = "";


if ($password eq "veli"){
$Message = param('Message');
print $Message;
}
if ($password ne "veli"){
print "Wrong Password";
}


print end_html;

##################################################################################


the code above is like that when a user input a message and a password then the message will be print but first he have to type the password and if the password is correct then it will display other wise it should display "worng password"
but when iam runing this script it is always display wrong password because password is always not equals to 'veli' in the beging so do u have any idea how to stop displaying the password when i run the script in the beging . but when i rite the password then it should display and when the password is wrong then display an error message . Please Help me in this case as soon as possible thanks
 
#####Display Text Box where Veli can Input The message by him self########

my $password;
my $Message;
print start_html('Massage'),
start_form,

print "<table align=center width=600 border=0 cell padding=3 cellspacing=1>";
print TR(td(br,'Message',textfield(-name=>'Message', -size=>'50',-maxlength=>'50')));
print TR(td(br,'Password',password_field(-name=>'Password', -size=>'10',-maxlength=>'10')));
print TR(td(br,submit(-name=>'Submit',-value=>Submit)));
print "</table>";
end_form,
$password = param('Password');
$Message = "";


if ($password eq "veli"){
$Message = param('Message');
print $Message;
}
if ($password ne "veli"){
print "Wrong Password";
}


print end_html;

##################################################################################


the code above is like that when a user input a message and a password then the message will be print but first he have to type the password and if the password is correct then it will display other wise it should display "worng password"
but when iam runing this script it is always display wrong password because password is always not equals to 'veli' in the beging so do u have any idea how to stop displaying the password when i run the script in the beging . but when i rite the password then it should display and when the password is wrong then display an error message .the text which will be input by the user should go to the a textfile and when i input the text again then it should delete the old data in the textfile and input the new data in that text file .
 
if that is all to the script
if (param()) {
if ($password eq "veli"){
$Message = param('Message');
print $Message;
}
if ($password ne "veli"){
print "Wrong Password";
}
}

if your passing other variables to this part you might change that to
if(param(Message) ne "" && param(Password) ne ""){

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Thanks Soo Much for ue reply it worked but now i have another problem coming up i would like to know how can i save the $Message variable in a datafile and when i want to get the $message variable value from datafile how can i do that and file will always have one value when ever i input the value in file then it should first delete the old value and input the new value rite now the code is like that

#####Display Text Box where Veli can Input The message by him self########

my $password;
my $Message;
print start_html('Massage'),
start_form,

print "<table align=center width=600 border=0 cell padding=3 cellspacing=1>";
print TR(td(br,'Message',textfield(-name=>'Message', -size=>'50',-maxlength=>'50')));
print TR(td(br,'Password',password_field(-name=>'Password', -size=>'10',-maxlength=>'10')));
print TR(td(br,submit(-name=>'Submit',-value=>Submit)));
print "</table>";
end_form,
$password = param('Password');
$Message = "";


if (param()) {
if ($password eq "veli"){
$Message = param('Message');

}
if ($password ne "veli"){
print "Wrong Password";
}
}


print end_html;



#######################################################
 
there is no need to check if the password does not equal 'veli'.

Code:
         if ($password eq "veli"){
            $Message = param('Message');
                    
         }
         else {
            print "Wrong Password";
         }

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
examples:
open(FILE, ">/path/to/file") or die "can't open /path/to/file:$!\n";
print FILE "This is a test\n";
close FILE;


open(FILE, "</path/to/tile" or die "can't open /path/to/file:$!\n";
@file = <FILE>;
chomp @file;
close FILE;
print "@file\n";

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top