Connecting Tech Pros Worldwide Help | Site Map

Multiple if statements in single while loop--HELP

Newbie
 
Join Date: Sep 2006
Posts: 1
#1: Sep 1 '06
I am writing a parser to read in ftp log files and insert into a databse. I have everything working fine, except the legacy billing system being used generates bills for each user instead of the master account. Therefore, as a bandaid, I need to attach an account field to each uid for each insert. I figured I could do this using more if statements within my loop, which seemed to work for one, but as I added an additional it stopped working properly. Any help would be appreciated. Here is the code that I am having trouble with.

Expand|Select|Wrap|Line Numbers
  1.         while (<FILE>) {
  2.                 ($dow, $month, $day, $time, $year, $duration, $clientip, $size, $path, $type, $uid) = split(/\s+/);
  3.  
  4. $date = "$dow,$month,$day,$time,$year";
  5.  
  6. # ATTACH USERS TO MASTER ACCOUNTS
  7. # -------------------------------
  8.                 if ($uid eq "jdoe","bfoo","jjones") {
  9.                         $account = "abc";
  10. }                elsif ($uid eq "msmith") {
  11.                         $account = "xyz";
  12. }                elsif ($uid eq "zbills","vgoo") {
  13.                         $account = "asdf";
  14. }
  15. # MAY NEED TO ADD MANY MORE elsif's OR COULD THESE BE ALL if's?; OR HOW COULD I REFERENCE A TEXT FILE OR DATABASE TABLE TO MATCH UID'S AND RETURN THE ACCOUNT TO BE ATTACHED TO EACH LINE OF LOG FILE BEING PARSED AND INPUT INTO DATABASE?
  16. #
  17. # CONVERT TYPE TO LEGACY DB METHOD
  18. # --------------------------------
  19.                 if ($type eq "o") {
  20.                         $method = RETR;
  21.                 } elsif ($type eq "i") {
  22.                         $method = STOR;
  23.                 }
  24.                 unless ($size eq "-" || $duration eq "-") {
  25.                         $sth->execute($date,$clientip,$duration,$size,$method,$uid,$path,$account) or die "Couldn't execute statement: " . $sth->errstr;
  26.                 }
  27.         }
Newbie
 
Join Date: Aug 2006
Posts: 7
#2: Sep 11 '06

re: Multiple if statements in single while loop--HELP


while (<FILE>)
{
my ($dow, $month, $day, $time, $year, $duration, $clientip, $size, $path, $type, $uid) = split(/\s+/);
my $date = "$dow,$month,$day,$time,$year";

my %uid_uaccount = (
hemmi => 14222,
Jonathan=> 28890,
olav => 33221);

#instead of having a gazillion if statements you just go through the hash:
while (($cuid, $accountno) = each %uid_accountno)
{
# if this part of the hash is what youre looking for, make $account
# the value of this element. And then quit iterating this hash.
if ($cuid == $uid) {$account = $accountno; last;}
}

if ($type eq "o") {$method = RETR;}
if($type eq "i") {$method = STOR;}

unless ($size eq "-" || $duration eq "-")
{
$sth->execute($date,$clientip,$duration,$size,$method,$ uid,$path,$account) or die "Couldn't execute statement: " . $sth->errstr;
}
}
[/code][/quote]


# now each extra user only requires you to add one element to %uid_uaccount.
Reply