469,334 Members | 4,388 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,334 developers. It's quick & easy.

Multiple if statements in single while loop--HELP

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.         }
Sep 1 '06 #1
1 5861
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.
Sep 10 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Shmuel | last post: by
2 posts views Thread by Richard Adams | last post: by
1 post views Thread by Erik Haugen | last post: by
3 posts views Thread by d.schulz81 | last post: by
2 posts views Thread by Annie D via AccessMonster.com | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by haryvincent176 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.