Multiple if statements in single while loop--HELP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ickypick
    New Member
    • Sep 2006
    • 1

    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.

    Code:
            while (<FILE>) {
                    ($dow, $month, $day, $time, $year, $duration, $clientip, $size, $path, $type, $uid) = split(/\s+/);
    
    $date = "$dow,$month,$day,$time,$year";
    
    # ATTACH USERS TO MASTER ACCOUNTS
    # -------------------------------
                    if ($uid eq "jdoe","bfoo","jjones") {
                            $account = "abc";
    }                elsif ($uid eq "msmith") {
                            $account = "xyz";
    }                elsif ($uid eq "zbills","vgoo") {
                            $account = "asdf";
    }
    # 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?
    #
    # CONVERT TYPE TO LEGACY DB METHOD
    # --------------------------------
                    if ($type eq "o") {
                            $method = RETR;
                    } elsif ($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;
                    }
            }
  • HermannJens
    New Member
    • Aug 2006
    • 7

    #2
    while (<FILE>)
    {
    my ($dow, $month, $day, $time, $year, $duration, $clientip, $size, $path, $type, $uid) = split(/\s+/);
    my $date = "$dow,$month,$d ay,$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,$dura tion,$size,$met hod,$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.

    Comment

    Working...