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: Intermediate:
Help shortening long if/else statement

 



Warren Bell
Deleted

May 30, 2000, 1:45 PM

Post #1 of 5 (929 views)
Help shortening long if/else statement Can't Post

I wrote a database script (http://www2.jps.net/~wjbell/scripts/database.html) and I'm trying to clean up some code I know there's got to be an easier way.

What it does is open up a human readable config file, detects certain lines and words, then makes them into variables depending what numbers match what's in the query string. Write now I have a large else/if statement that runs through the file but I'm sure there's an easier way.

I'll post the code so it's easier to follow:

Config file
=================================

#-----------------------------
# database list
#-----------------------------
database1: customers
database2: personal

# database1 configuration
#-----------------------------
# don't change change these
#-----------------------------
1:datafile: data.txt

1:heading1: Misc1
1:heading2: Misc2

1:misc1: Opt1
1:smlmisc1: Gal
1:misc2: Opt2
1:smlmisc2: Gal
1:misc3: Opt3
1:smlmisc3: Gal

# database2 configuration
#----------------------------------
# don't change change these
#----------------------------------
2:datafile: data2.txt

2:heading1: Misc1
2:heading2: Misc2

2:misc1: Opt1
2:smlmisc1: Val
2:misc2: Opt2
2:smlmisc2: Val
2:misc3: Opt3
2:smlmisc3: Val

Else/if statement that runs every time the script is run to get the variables.
==========================================

open (FILE, "$configfile") | | &error("Unable to open $configfile");
&lock(FILE);
@config = <FILE>;
close(FILE);

foreach $line (@config) {

if ($line =~ /database(\d):\s+$db/) {
$current = "$1";
}
elsif ($line =~ /$current:datafile:\s+(.*)/) {
$datafile = "$1";
}
elsif ($line =~ /$current:heading1:\s+(.*)/) {
$heading1 = "$1";
}
elsif ($line =~ /$current:heading2:\s+(.*)/) {
$heading2 = "$1";
}
elsif ($line =~ /$current:misc1:\s+(.*)/) {
$misc1 = "$1";
}
elsif ($line =~ /$current:smlmisc1:\s+(.*)/) {
$smlmisc1 = "$1";
}
elsif ($line =~ /$current:misc2:\s+(.*)/) {
$misc2 = "$1";
}
elsif ($line =~ /$current:smlmisc2:\s+(.*)/) {
$smlmisc2 = "$1";
}
elsif ($line =~ /$current:misc3:\s+(.*)/) {
$misc3 = "$1";
}
elsif ($line =~ /$current:smlmisc3:\s+(.*)/) {
$smlmisc3 = "$1";
}

}


Thanks for any help with this, really appreciate it.


japhy
Enthusiast

May 30, 2000, 8:17 PM

Post #2 of 5 (929 views)
Re: Help shortening long if/else statement [In reply to] Can't Post

Use a hash.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open FILE, $file or die "can't open $file";
while (<FILE> ) {
# note -- there's no need to put this in
# an array and then go through the array
# just use a while loop like this

if (/database(\d):\s+$db/) {
$current = $1; # quotes aren't needed
}
elsif (/$current Frown\w+):\s+(.*)/) {
$DATA{$1} = $2;
}
}
close FILE;

# now the %DATA hash looks like
# $DATA{datafile} = 'data.txt';
# $DATA{heading1} = 'Misc1';
# etc...
</pre><HR></BLOCKQUOTE>

To learn how to use hashes, read the 'perldata' section of the Perl documentation, available on this web site at the "Perl Documentation" link.


Warren Bell
Deleted

May 30, 2000, 9:52 PM

Post #3 of 5 (929 views)
Re: Help shortening long if/else statement [In reply to] Can't Post

That didn't work. Only some of the variables showed up. It detected the only about 1/3 of the vars and the rest were nothing.

I removed the smilie from the code so that wasn't it


Peter Van Hoecke TEMP
Deleted

May 31, 2000, 3:58 PM

Post #4 of 5 (929 views)
Re: Help shortening long if/else statement [In reply to] Can't Post

Hum..., just out of curiosity...

you did replace the smiley by a : followed by a (

Frown => : followed by (

[This message has been edited by Peter Van Hoecke TEMP (edited 05-31-2000).]
(sorry... just kidding)

[This message has been edited by Peter Van Hoecke TEMP (edited 06-01-2000).]


Warren Bell
Deleted

May 31, 2000, 4:52 PM

Post #5 of 5 (929 views)
Re: Help shortening long if/else statement [In reply to] Can't Post

Yeah, I took out the smilie and replaced it with a : and a (. Smile

 
 


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

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