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

I have a database of lines separated by |. I have been opening my database and putting all lines in an @array then spliting them with a for each loop, then printing them. I now want to extract only a hundred lines and split them. But I need to start at a certian line number, for example read lines 1-100, 101-200, 201-300 and so on. Put those lines an array I can run a for each loop on and split them. Or maybe someone has a better solution. Please help. Thanks
  • Comment on How do I extract (x) number of lines from database?

Replies are listed 'Best First'.
Re: How do I extract (x) number of lines from database?
by AidanLee (Chaplain) on Sep 21, 2001 at 08:30 UTC

    if you've got them in an array it should be fairly easy to do this using an array slice.

    my @array = <FILE>; #or however you've retrieved your lines foreach my $line ( @array[99..199] ) { #munge line here }

    you can put variables in for the bottom and top of your slice range.

    p.s., you might really want to consider using either a templating system, CGI.pm, or at the very least HEREDOC syntax to deal with all that messy html output.

      Thanks Everybody! Peace Out! Mosley
Re: How do I extract (x) number of lines from database?
by jlongino (Parson) on Sep 21, 2001 at 07:19 UTC
    You stated that the lines are separated by "|". But the rest of your narrative implies that you meant that the fields of each line are separated by "|". So I am assuming the latter:
    use strict; my ($begline, $endline); open DATA, "<indb.txt"; my @lines = <DATA>; chomp @lines; foreach ($begline..$endline) { my ($var1, $var2, $var3) = split '\|', $lines[$_]; print "$var1 $var2 $var3\n"; # do whatever }
    If this isn't what you meant, please clarify.

    Update: Of course you need to initialize $begline, $endline as needed. Also, keep in mind that @lines begins with an index of zero, not one.

    @a=split??,'just lose the ego and get involved!';
    for(split??,'afqtw{|~'){print $a[ord($_)-97]}
      My current code is as follows, if you could show me how to extract lines 10 thru 20, Thanks.
      $linenum = 1; $altnum = 1; open(FILE, "<data/data.txt"); @lines = <FILE>; close(FILE); # prints out table header print "<center><table border=\"0\" cellpadding=\"3\" width=\"596\" bgc +olor=\"#000080\"><tr><td bgcolor=\"#000050\" colspan=\"2\">Free Host +Name<\/font><\/td><td bgcolor=\"#000050\" align=\"center\">Space<\/fo +nt><\/td><td bgcolor=\"#000050\" align=\"center\">Uploads<\/font><\/t +d><td bgcolor=\"#000050\" align=\"center\">Forced Ads<\/font><\/td><t +d bgcolor=\"#000050\" align=\"center\">Scripts<\/font><\/td><td bgcol +or=\"#000050\" align=\"center\">Features<\/font><\/td><\/tr>\n"; foreach $item (@lines) { @thisArray = split(/\|/, $item); # just alternating td bgcolor with these next two lines if ($altnum eq "1") { $altnum = 0; $bgc = "#000080"; } elsif ($altnum eq "0") { $altnum = 1; $bgc = "#000050"; } print "<tr><td bgcolor=\"#000050\" align=\"center\">@thisArray[0]<\/fo +nt><\/td><td bgcolor=\"$bgc\"><a href=\"goto.cgi?@thisArray[0]\">@thi +sArray[1]<\/a><\/font><\/td><td bgcolor=\"$bgc\" align=\"center\">@th +isArray[6] MB<\/font><\/td><td bgcolor=\"$bgc\">@thisArray[7]<\/font> +<\/td><td bgcolor=\"$bgc\" align=\"center\">@thisArray[5]<\/font><\/ +td><td bgcolor=\"$bgc\" align=\"center\">@thisArray[10]<\/font><\/td +><td bgcolor=\"#000050\" align=\"center\"><a href=\"more.cgi?@thisArr +ay[0]\">more<\/font><\/a><\/td><\/tr>"; $linenum++; } print "<\/td><\/tr><\/table>\n\n"; ___DATA____ 1|0 catch|english|personal|http://www.0catch.com|popup|100|ftp browser + fp|advanced basic|domainhosting subdomain counter form guestbook|col +lection| 2|100megsfree|english|personal|http://www.100megsfree.com|banner|100|f +tp browser email|advanced basic|subdomain messageboard form guestbook +|collection| 3|10mbfree|english|personal|http://www.10mbfree.com|other|10|browser|| +subdomain messageboard guestbook|collection| 4|15m4u|english|personal business game|http://www.15m4u.cjb.net|no ads +|15|ftp email||php subdomain ssi cgi-bin|cgi-bin| 5|1hwy|english|personal|http://www.1hwy.com|banner|0|ftp browser|advan +ced basic|subdomain counter form guestbook|collection| 6|20megsfree.com|english|personal business game|http://www.20megsfree. +com|banner|20|ftp browser email|advanced basic|pop email subdomain co +unter form guestbook|collection| 7|20megs|english|personal|http://www.20megs.com/||20|browser|advanced +basic|messageboard guestbook|collection| 8|2d adventure|english|game|http://www.2dadventure.com|button|999|brow +ser|basic||no| 9|30mbfree|english|personal|http://www.30mbfree.com|no ads|0|browser|b +asic|messageboard form guestbook|collection| 10|321website|english|personal|http://www.321website.com/|adframe|0|br +owser|advanced|messageboard shopping cart guestbook|collection| 11|3dracing|english|game|http://www.3dracing.com/hosting.htm|banner|20 +|ftp||domainhosting subdomain cgi-bin|cgi-bin| 12|3hosting4u|english|personal business|http://www.3hosting4u.com|no a +ds|10|browser|advanced basic|counter form guestbook|collection| 13|3megsfree|english|personal|http://www.3megsfree.com|footbanner|0|br +owser|advanced|counter guestbook|collection| 14|4business|english|business|http://www.4business.net/|banner|20|ftp +fp||domainhosting subdomain cgi-bin|cgi-bin| 15|4eit|english|personal business game|http://www.4eit.com|no ads|20|f +tp email||php realvideo realaudio pop email domainhosting subdomain m +ysql ssi cgi-bin form guestbook|cgi-bin| 16|4nx2|english|personal business|http://www.4nx2.com|no ads|20|browse +r|advanced|php mysql|php| 17|4office|english|business|http://www.4office.net/|banner|999|ftp||su +bdomain cgi-bin|cgi-bin| 18|50megs|english|personal|http://www.50megs.com/|topbanner|50|ftp bro +wser|advanced basic|domainhosting subdomain messageboard form guestbo +ok|collection| 19|75megs|english|personal business|http://www.75megs.com/|popup/banne +r|75|ftp browser|advanced basic|domainhosting subdomain counter form +guestbook|collection| 20|800mph|english|personal|http://800mph.com/|adframe|0|browser|advanc +ed|realvideo realaudio pop email subdomain messageboard counter form +guestbook|collection|
        This is the modified code. Note that I commented out the external file I/O stuff and just used the internal __DATA__ (which incidently should use only two "_"s before and after. you had three). Also, I added "use strict", which you should get into the habit of using, and "my"s where appropriate. Think also about using indentation to make your control structures more readable.
        use strict; my $linenum = 1; my $altnum = 1; my $bgc; # open(FILE, "<data/data.txt"); # my @lines = <FILE>; my @lines = <DATA>; # close(FILE); # prints out table header print "<center><table border=\"0\" cellpadding=\"3\" width=\"596\" bgc +olor=\"#000080\"><tr><td bgcolor=\"#000050\" colspan=\"2\">Free Host +Name<\/font><\/td><td bgcolor=\"#000050\" align=\"center\">Space<\/fo +nt><\/td><td bgcolor=\"#000050\" align=\"center\">Uploads<\/font><\/t +d><td bgcolor=\"#000050\" align=\"center\">Forced Ads<\/font><\/td><t +d bgcolor=\"#000050\" align=\"center\">Scripts<\/font><\/td><td bgcol +or=\"#000050\" align=\"center\">Features<\/font><\/td><\/tr>\n"; + foreach my $item (@lines[9..19]) { my @thisArray = split(/\|/, $item); # just alternating td bgcolor with these next two lines if ($altnum eq "1") { $altnum = 0; $bgc = "#000080"; } elsif ($altnum eq "0") { $altnum = 1; $bgc = "#000050"; } print "<tr><td bgcolor=\"#000050\" align=\"center\">@thisArray[0]< +\/font><\/td><td bgcolor=\"$bgc\"><a href=\"goto.cgi?@thisArray[0]\"> +@thisArray[1]<\/a><\/font><\/td><td bgcolor=\"$bgc\" align=\"center\" +>@thisArray[6] MB<\/font><\/td><td bgcolor=\"$bgc\">@thisArray[7]<\/f +ont><\/td><td bgcolor=\"$bgc\" align=\"center\">@thisArray[5]<\/font +><\/td><td bgcolor=\"$bgc\" align=\"center\">@thisArray[10]<\/font>< +\/td><td bgcolor=\"#000050\" align=\"center\"><a href=\"more.cgi?@thi +sArray[0]\">more<\/font><\/a><\/td><\/tr>"; $linenum++; } print "<\/td><\/tr><\/table>\n\n"; __DATA__ 1|0 catch|english|personal|http://www.0catch.com|popup|100|ftp browser + fp|advanced basic|domainhosting subdomain counter form guestbook|col +lection| 2|100megsfree|english|personal|http://www.100megsfree.com|banner|100|f +tp browser email|advanced basic|subdomain messageboard form guestbook +|collection| 3|10mbfree|english|personal|http://www.10mbfree.com|other|10|browser|| +subdomain messageboard guestbook|collection| 4|15m4u|english|personal business game|http://www.15m4u.cjb.net|no ads +|15|ftp email||php subdomain ssi cgi-bin|cgi-bin| 5|1hwy|english|personal|http://www.1hwy.com|banner|0|ftp browser|advan +ced basic|subdomain counter form guestbook|collection| 6|20megsfree.com|english|personal business game|http://www.20megsfree. +com|banner|20|ftp browser email|advanced basic|pop email subdomain co +unter form guestbook|collection| 7|20megs|english|personal|http://www.20megs.com/||20|browser|advanced +basic|messageboard guestbook|collection| 8|2d adventure|english|game|http://www.2dadventure.com|button|999|brow +ser|basic||no| 9|30mbfree|english|personal|http://www.30mbfree.com|no ads|0|browser|b +asic|messageboard form guestbook|collection| 10|321website|english|personal|http://www.321website.com/|adframe|0|br +owser|advanced|messageboard shopping cart guestbook|collection| 11|3dracing|english|game|http://www.3dracing.com/hosting.htm|banner|20 +|ftp||domainhosting subdomain cgi-bin|cgi-bin| 12|3hosting4u|english|personal business|http://www.3hosting4u.com|no a +ds|10|browser|advanced basic|counter form guestbook|collection| 13|3megsfree|english|personal|http://www.3megsfree.com|footbanner|0|br +owser|advanced|counter guestbook|collection| 14|4business|english|business|http://www.4business.net/|banner|20|ftp +fp||domainhosting subdomain cgi-bin|cgi-bin| 15|4eit|english|personal business game|http://www.4eit.com|no ads|20|f +tp email||php realvideo realaudio pop email domainhosting subdomain m +ysql ssi cgi-bin form guestbook|cgi-bin| 16|4nx2|english|personal business|http://www.4nx2.com|no ads|20|browse +r|advanced|php mysql|php| 17|4office|english|business|http://www.4office.net/|banner|999|ftp||su +bdomain cgi-bin|cgi-bin| 18|50megs|english|personal|http://www.50megs.com/|topbanner|50|ftp bro +wser|advanced basic|domainhosting subdomain messageboard form guestbo +ok|collection| 19|75megs|english|personal business|http://www.75megs.com/|popup/banne +r|75|ftp browser|advanced basic|domainhosting subdomain counter form +guestbook|collection| + 20|800mph|english|personal|http://800mph.com/|adframe|0|browser|advanc +ed|realvideo realaudio pop email subdomain messageboard counter form +guestbook|collection|
        Update: If it wasn't so late, I'd have used a hash with the first field (linenum/ID) as the key field. Maybe a monk in a different timezone could illustrate. As it is, I just fudged the 10-20 with 9-19, which is fine for quick and dirty, but not too aesthetic.
        @a=split??,'just lose the ego and get involved!';
        for(split??,'afqtw{|~'){print $a[ord($_)-97]}