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

Hi Everyone, I have a quick(hopefully) question regarding a flat database. I'm trying to figure out how to "Skip" the first entry line in the DB...the first entry line being:
company_name|street|city_st_zip|phone|fax|website|email|category|alpha
Here's the code that I'm using:
$database='entries.dat'; # Check to see if there was a database specified # if ($input{'database'} eq ''){ $db=$database; }else{ $db=$input{'database'}; } open (ORGDB,"<$database"); @ODB=<ORGDB>; close (ORGDB); #This should sort the lines by company alpha-order @ODB = sort(@ODB); print "Content-type: text/html\n\n"; foreach $rec (@ODB){ chomp($rec); ($company_name,$street,$city_st_zip,$phone,$fax,$website,$email,$c +ategory,$alpha)=split(/\|/,$rec); print "<tr><td valign=top width=90><font size='2'><a href='business_ad +min_edit2.cgi?company_name=$company_name&street=$street&city_st_zip=$ +city_st_zip&phone=$phone&fax=$fax&website=$website&email=$email&categ +ory=$category&alpha=$alpha'><img src='gfx/button_edit.gif' border=0>< +/a> </font></td> <td valign=top><font size='2'><B>Company</B> $company_na +me<BR> <B>Street Address:</B> $street<BR> <B>City, St., Zip:</B> $cit +y_st_zip<BR> <B>Phone:</B> $phone<BR> <B>FAX:</B> $fax<BR> <B>Website +:</B> $website<BR> <B>Email:</B> $email<BR> <B>Category:</B> $categor +y<BR> <B>Alpha Listing:</B> $alpha<BR><BR></td></tr>\n"; } # Code to get the data from GET & POST # sub parse_form { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); if (length($buffer) < 5) { $buffer = $ENV{QUERY_STRING}; } @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $input{$name} = $value; } }
Thanks for any help that you can give me guys! LisaW

Replies are listed 'Best First'.
Re: Flat Database: Skip the first line
by grep (Monsignor) on Jan 26, 2002 at 00:21 UTC
Re: Flat Database: Skip the first line
by impossiblerobot (Deacon) on Jan 26, 2002 at 01:29 UTC
    You can use the input operator in a void context to throw away a single line:
    open (ORGDB,"<$database"); <ORGDB>; # Throw away header line @ODB=<ORGDB>; close (ORGDB);

    Impossible Robot
Re: Flat Database: Skip the first line
by petral (Curate) on Jan 26, 2002 at 04:38 UTC
    You can also *use* the first line!   Something like:
    . . . close (ORGDB); my @fields = split/\|/, shift @ODB; . . . @vals = split(/\|/,$rec); my $outflds = join "&", map "$fields[$_]=$vals[$_]", 0..$#fields;
    and/or make a hash for the textnames:
    my %fldname = ( company_name => 'Company', street => 'Street Address', . . . ); . . . my %fldval; @fldval{@fields} = @vals; my $htmltxt = join '<br> <b>', map "$fldname{$_}:</b> $fldval{$_}", @fields; . . .

      p
Re: Flat Database: Skip the first line
by Ineffectual (Scribe) on Jan 26, 2002 at 01:14 UTC
    You can also do it during the while loop. This is also handy if you need to remove the \n character.
    while (<ORGDB>) { chomp; next if /^company/; }
    Change the ^company to whatever you can get away with. ie: if company appears at the front of the line (the word company) then you can change it to be "company name" or whatever is unique in the top line of your file.

    'Fect
Re: Flat Database: Skip the first line
by vek (Prior) on Jan 26, 2002 at 00:44 UTC
    Perhaps use a scalar to keep track of whether you've seen the first line:
    my @ODB; my $firstLine++; open (ORGDB, $database) || die "open $database -$!\n"; while (<ORGDB>) { next if ($firstLine); push (@ODB, $_); $firstLine = ''; } close (ORGDB)
    Or if you can be sure that first line will not change:
    my @ODB; open (ORGDB, $database) || die "open $database - $!\n"; while (<ORGDB>) { next if (substr($_, 0, 12) eq 'company_name'); push (@ODB, $_); } close (ORGDB);