Ever wanted to know how to automatically generate a listbox full of values from an array, database or some sort? No? Me neither.. but nevertheless.. Here's how!
Firstly this is pretty much common sense so just ignore this if you are not a beginner (or scroll down to see the complete code for an overview). The HTML code for a listbox, you should probably already know.. but if not here it is for you.
<select>
<option value="1">One</option>
</select>
Note: select is the actual listbox, option are the items in the listbox.. value being what will be posted as a form (useful mostly for the server, unless it's a javascript or client application). The text in the middle is what will actually be shown in the listbox.
First you need your content (the items to display in the actual listbox), this will be read into an array.. For the sake of the tutorial I will be reading the content from a text file.
We open the file using "<" which ensures we are opening to read the file only, if the file fails to open we instantly kill the perl script from processing further with an error message.
open(FILE, "<items.txt") or die("I/O Error.\n"); # Open item.txt with our list of items for the listbox
We then read the file into an array, the array will add each line to a seperate element in the array, though that can be changed using the split and join functions. We set a variable for the value (alternately you can simply use the items from the file.). This value will increase for each line of the array creating a numerical value for each item which can be used to identify the selection.
my @farray = <FILE>; # Read the file into the array "farray" (based on new line character)
my $value = 1;
To print out the options we use a foreach loop to loop each item in the array, $item will be the text value for each line whilst @farray is the array from which the elements will be looped. We use the chomp function to remove the new line characters from the item ensuring there won't be any unnecessary new lines within the code we are looking to generate. We increase the value variable by 1 when we are done and ready for the next item.
foreach $item (@farray) { # loop each item in the array
chomp($item); # Remove new line character from item
print "<option value=\"$value\">" . $item . "</option>\n"; # Print the option with value and item
$value++; # Increase the value by 1, for each item
}
That's pretty much it for the basics, though here is a nifty way to print our HTML or other code within the perl script.
print <<EOF;
<html><head></head><body>
<select>
EOF
this will print anything below untill it reaches "EOF" which stands for end of file and is used commonly in programming, though here it is not nessecary.. you can use any word you like.
Here's the full code:
#!/usr/bin/perl
use Strict;
print "Content-type: text/html\n\n";
print <<EOF; # Print till "EOF" is found
<html><head></head><body>
<select>
EOF
open(FILE, "<items.txt") or die("I/O Error.\n"); # Open text file for reading
my @farray = <FILE>; # Read text file into array
my $value = 1;
foreach $item (@farray) { # Loop the array and print each item with HTML data
chomp($item);
print "<option value=\"$value\">" . $item . "</option>\n";
$value++; # Increase value for next use
}
print <<EOF; # Print till "EOF" is found
</select>
</body></html>
EOF
And there you have it! A simple way to dynamically create listboxes in perl. It can be quite useful for creating lists for locations, languages etc. etc. Goodnight!~
On a final note.. The next post might be a tutorial or information on HTML encoders, since writing code in blogspot is ULTIMATE FAIL!



