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

Hello, I trying to change a section of my code that opens a flat db with only one entry per line and allow it to open a flat db with multiple entries. The flat file that I'm working with is delineated with ``..anyone have any suggestions?
sub BuildMMList { local ( $style ) = @_; local ( $ret, $idx ); local ( *LIST ); local ( $line, @data, $key, %user, @ch ); $ret = ''; $idx = 0; open( LIST, "<$MM_FILE" ) || &Error( "internal", "Missing page lis +t" ); while ( $line = <LIST> ) { # see if it's a page. next if ( $line =~ /^#/ ); next if ( $line =~ /^\s+$/ ); $idx++; # remove white space. $line =~ s/^\s*//; $line =~ s/\s*$//; # display the table row if ( $style eq 'delete' ) { # !EMBEDDED HTML! $ret .= <<EOF; <TR> <TD> <INPUT NAME="page$idx" TYPE="hidden" VALUE="$line"> <INPUT NAME="action$idx" TYPE="checkbox" VALUE="delete"> <CODE>$line<\/CODE> </TD> </TR> EOF } else { # !EMBEDDED HTML! $ret .= <<EOF; <OPTION>$line EOF } } close( LIST ); $_ = $ret; } Data File Example: default1``default2``default3
thanks...still kinda new to this :(

edited: Wed Oct 23 21:17:35 2002 by jeffa - remember kids, always escape those embedded code tags ;)

Replies are listed 'Best First'.
Re: Split a database
by lisaw (Beadle) on Oct 23, 2002 at 21:23 UTC
    Sorry for messing up the code...I was able to figure it out about 5 minutes after posting my question. Here's my solution:
    sub BuildMMList { $list = $FORM{'list'}; open(FILE, "$MM_FILE") || &error_message("Can't find data file - $dat +adir/lists.txt."); @list = <FILE>; close(FILE); $numlist = @list; for ($a = 0; $a < $numlist; $a ++) { ($one, $nochop) = split(/``/, $list[$a]); print "<option value='$one'>$one</option>\n"; } close(FILE); }
    This seems to be working great but if anyone has any other methods please send them my way. Thanks

      it seems you've fixed your immediate problem, although i see you've left yourself open to more problems in the future. i have a few suggestions:

      sub BuildMMList { ## enforce strict programming methods (see strict) ## if you can't do this for the whole script, do it in each ## subroutine until the whole script has been refactored ## while you're at it, use warnings, too (see warnings) use strict; use warnings; ## pass parameters to the subroutine -- global variables are trouble ## assign the passed parameters to variables local to the scope of t +he sub my( $list, $mm_file ) = @_; ## use the three parameter form of open (see perlfunc) ## localize your filehandles to avoid namespace pollution ## don't quote variables unneccessarially (see perlvar) ## don't use the '&' sigil when calling subs (see perlsub) ## make your error messages verbose open( local *FILE, '<', $mm_file ) or error_message( "$0: Can't open data file: $mm_file: $!"); ## use a while loop to process the file in memory ## this avoids temp variables, and avoids c-like for loops while( <*FILE> ) { ## use the 3-arg form of split to limit fields ## throw away other fields by assigning to 'undef' my( $opt, undef ) = split /``/, $_, 2; ## use quote operators, so quote characters don't need to be escap +ed print qq{<option value="$opt">$opt</option>\n}; } }

      i haven't tested this code, so there may be a bug or two lurking. anywhere i have a parenthesised note, you should use perldoc to find more info, or supersearch perlmonks for more.

      i'm sorry i can't offer you more help (like using taint mode to safely process cgi params), but i hope this will get you started, and you'll go looking for more as you need it.

      ~Particle *accelerates*

Re: Split a database
by robartes (Priest) on Oct 23, 2002 at 21:29 UTC
    I'm a bit unclear on what you actually want, but from what I can understand, you are looking for the split function:
    use strict; my $data='default1``default2``default3'; my @elements= split /``/, $data;

    CU
    Robartes-

    Update: My help seems superfluous - lisaw figured it out by (her|him)self. :)