CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Retreiving from flat file database

 



DavyC
Deleted

Sep 27, 2000, 5:17 PM

Post #1 of 2 (681 views)
Retreiving from flat file database Can't Post

Hi

I have wrote a few scripts all fairly simple but I am working on a new soccer site, part of the site requires the next four matches and past four matches.

I use dbman from Gossamer Threads as the database holding the match details, as I cannot find a script that will allow me to pull the matches as explained I will try and write it my self, here is a record in the file

23/10/2000|3|Away|Bangor|etc|etc

the first field is the date in European format dd/mm/yyyy the next field is the id number.

each record held on a new line

I have played about but I cannot find the best logic to work this out, I intend to use ssi to call the script that performs the search and produces the desired results.

Any assistance would be greatly appreciated - even help with the logic that I should be using would help.

Many Many thanks

DavyC
Smile


Kanji
User

Sep 27, 2000, 8:27 PM

Post #2 of 2 (681 views)
Re: Retreiving from flat file database [In reply to] Can't Post

There's a few way you could do this, including playing games with seek(), tell(), and eol indicies, but a fairly simple method (caching lines in an array) would be ...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

my @lines;
my $matched = 0;


open( FILE, "< $file" ) or die( "$file: $!" );
while ( $line =~ <FILE> ) {
$matched++ if $line =~ /pattern/;


# Keep the array in check by truncating
# it to the number of lines you want to
# keep before the one that matched.
# Once we've got our match, tho', stop
# truncating.
shift @lines
if @lines == 4
&& ! $matched;


# Add the current line to our array
# of lines.
push @lines, $line;


# Stop searching if our array has
# enough ( previous lines wanted,
# matched line, and post lines wanted
# [ 4 + 1 + 4 ] ) elements.
last if @lines == 9;
}


# @lines now has the db entries you want.</pre><HR></BLOCKQUOTE>

That won't play nice if the match is on the first or last few lines, 'though ( in the former's case, your match would be trailed by the followin 8 lines! ), but I'll leave that as an exercise for to solve. :-)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives