Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

DB Question

by lisaw (Beadle)
on Nov 11, 2002 at 15:14 UTC ( [id://211939]=perlquestion: print w/replies, xml ) Need Help??

lisaw has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have a question that I hope that you good people can help me with. In my script I am pulling a list of categories from a data file for use in a flat db file.
open(FILE, "$catdir/categories.dat") || &error_message("Can't fin +d data file - $catdir/categories.dat."); @list = <FILE>; close(FILE); @list= sort(@list); $numlist = @list; print "<select size=1 name='catlisting'>"; for ($a = 0; $a < $numlist; $a ++) { ($one, $nochop) = split(/\|/, $list[$a]); print "<option value='$one'>$one</option>"; } print "</select>"; close(FILE);
Dat File:
Categories Accounting Attorneys Automobile-Sales Automobile-Services Banks
Everything works fine until it gets added to the flat data file. Anything following the "catlisting" get sent to the next line. Any suggestions? thanks, lis

Replies are listed 'Best First'.
Re: DB Question
by FamousLongAgo (Friar) on Nov 11, 2002 at 15:29 UTC
    I'm not clear about your question -- where are things getting added to the flat data file? And what do you mean by "sent to the next line"?

    That said, here's a cleanup of the code you posted:
    open FILE, "$catdir/categories.dat" or error_message( "Can't find data file: $catdir/categories.dat" ); @list = <FILE>; close FILE; chomp @list; #remove newlines print "<select size=\"1\" name=\"catlisting\>\n"; foreach my $element ( sort @list ) { print "<option value=\"$element\">$element</option>\n"; } print "</select>";
    I replaced the loop with a more natural foreach loop, removed the duplicate "close" statement, and removed the mystery split statement ( which does nothing, based on your data file provided ).

      You also replaced the incorrect single quotes in the HTML tags with the correct double quotes, which was going to be my suggestion...

      --Kevin

      Thank you very much!! That did the trick!
Re: DB Question
by tadman (Prior) on Nov 11, 2002 at 15:32 UTC
    Just a quick note about reading from files. You can iterate through the array directly without the need for a separate looping and counter variable:
    foreach (sort @list) { my ($one, $nochop) = split(/\|/, $_); chomp($one); # Remove any trailing linefeeds print "<option value='$one'>$one</option>"; }
    I'm not sure what your $nochop variable is for, since it doesn't seem to be utilized. Further, you're closing FILE twice, which is irregular considering the first is sufficient.

    I'd also like to throw in my obligatory nitpick about using ampersands in function calls. The first line could be sufficiently written as:
    unless (open(FILE, "$catdir/categories.dat")) { error_message("Can't find data file - $catdir/categories.dat"); }
    Usually, I would put a return call in the unless block because there's little point in displaying a page if there's no data to put in it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://211939]
Approved by trs80
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-03-28 17:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found