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: Beginner:
reading filehandle into array doesn't seem to work

 



oscarloaded
stranger

Jul 3, 2001, 5:27 PM

Post #1 of 3 (455 views)
reading filehandle into array doesn't seem to work Can't Post

Hello,

I have a script that is supposed to compare passed variables to a csv flatfile. The flatfile is 2 columns ( ex. 612,450) It seems like it is reading the first column and comparing it against the users input, but seems like it is completely dumping the other.

Here's the code:

#!/usr/local/bin/perl -w

use CGI;
$x = new CGI;

$bizres = $x ->param('bizres');
$areacode = $x ->param('areacode');
$dialprefix = $x ->param('dialprefix');

open(IN, "test.dat") or die;
while ($record = <IN>) {
chomp $record;
($aareacode, $ddialprefix)=split(/,/,$record,2);
$nnpa{$aareacode} = $aareacode;
$nnxx{$ddialprefix} = $ddialprefix;
}
close IN;

if (($nnpa{$areacode} ne $areacode) and ($nnxx{$dialprefix} ne $dialprefix)) {
print $x -> header();
print $x -> start_html("Error");
print $x -> p(), "Sorry, There are no services available in your area";
print $x ->end_html;
exit(0);
}

if (($areacode == "701") && ($bizres eq "residential")) {
print $x -> redirect('residential1.htm');
} elsif (($areacode == "701") && ($bizres eq "business")) {
print $x -> redirect('business1.htm');
} else {
if ($bizres eq "residential") {
print $x -> redirect('residential2.htm');
} elsif ($bizres eq "business") {
print $x -> redirect('business2.htm');
}}

exit(0);

Any help is greatly appreciated. Thank you.



Rivotti
User

Jul 4, 2001, 6:37 AM

Post #2 of 3 (444 views)
Re: reading filehandle into array doesn't seem to work [In reply to] Can't Post

Hi oscarloaded,

Instead of:

open(IN, "test.dat") or die;
while ($record = <IN>) {
chomp $record;
($aareacode, $ddialprefix)=split(/,/,$record,2);
$nnpa{$aareacode} = $aareacode;
$nnxx{$ddialprefix} = $ddialprefix;
}
close IN;

...you could...

open(IN, "test.dat") or die;
while (<IN>) {
chomp $_;
($nnpa{$aareacode}, $nnxx{$ddialprefix})=split(/,/,$_);
}
close (IN);

I've not test it but I think it will work...

regards
Rivotti



oscarloaded
stranger

Jul 5, 2001, 7:56 PM

Post #3 of 3 (429 views)
Re: reading filehandle into array doesn't seem to work [In reply to] Can't Post

Thanks for the suggestion. It seemed to work the same as the other filehandle input. I think I may have it though.

The problem seemed to have been with the >
if (($nnpa{$areacode} ne $areacode) and ($nnxx{$dialprefix} ne $dialprefix)) line.

I changed it to read >
unless (($nnpa{$areacode} == $areacode) and ($nnxx{$dialprefix} == $dialprefix))

That seems to have done the trick. Thanks a lot for the reply.


 
 


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

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