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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple if's in sngle log file read in loop --Help 2

Status
Not open for further replies.

ickypick

IS-IT--Management
Sep 1, 2006
9
0
0
US
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 snippet:

while (<FILE>) {
($dow, $month, $day, $time, $year, $duration, $clientip, $size, $path, $ttype, $specialact, $type, $mode, $uid, $service, $authm, $authu, $st
atus) = split(/\s+/);

# 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";
}
# WILL 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,$appuid,$path,$account) or die "Couldn't execute statement: " . $sth->errstr;
}
}
 
you could set upt the aliases in a hash
Code:
my %aliases;
$aliases{'jdoe'} = 'abc'; 
$aliases{'bfoo'} = 'abc';
$aliases{'jjones'} = 'abc';
$aliases{'msmith'} = 'xyz';
$aliases{'zbills'} = 'asdf'; 
$aliases{'vgoo'} = 'asdf';
# etc

while loop over input
    $account = $aliases{$uid};
    # process account;

## at sartup you coould read hash from an alias file.
Jeb
\0
 
Code:
if ($uid eq "jdoe","bfoo","jjones") {

I don't know what you thought you were doing here. Were you trying to see if $uid equaled "jdoe" OR "bfoo" OR "jjones"?

This is the correct syntax:

Code:
if ($uid eq "jdoe" || $uid eq "bfoo" || $uid eq "jjones") {

In some cases it's easier to use a regular expression, but put a ^ in front and a $ at the end so that the entire value of $uid has to equal one of them:

Code:
if ($uid =~ /^(jdoe|bfoo|jjones)$/) {
 
I suggest you put the uids and the accounts in a table and query the db every time. This way you don't hard code anything in the script and it will take less than a second for a simple query like "select account from accounts where uid=$uid" to complete.

This way you don't even have to bother with any ifs and elses at all.

Or you could construct a hash as NullTerminator pointed but with a different structure...something like
Code:
%accounts = (
    'jdoe'   => 'acb',
    'bfoo'   => 'abc',
    'jjones' => 'abc',
    'msmith' => 'xyz',
    'zbills' => 'asdf',
    'vgoo'   => 'asdf'
);
which a bit more readable.

The reason why it doesn't work as it is, is very well spotted by Kirsle.


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
All excellent solutions. As this is a band-aid, I'd probably go for a hash loaded from a flat file rather than a full-on DB, as recommended by NullTerminator. Just add a check to see if the alias is found in the hash, and either issue a warning, assign a default billing account, or die as appropriate.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top