Skip to Content
Programming the Perl DBI
book

Programming the Perl DBI

by Tim Bunce, Alligator Descartes
February 2000
Intermediate to advanced
364 pages
11h 47m
English
O'Reilly Media, Inc.
Content preview from Programming the Perl DBI

Querying Data

Since delimited files are a very low-level form of storage manager, any manipulations that we wish to perform on the data must be done using operating system functions and low-level query logic, such as basic string comparisons. The following program illustrates how we can open a data file containing colon-separated records of megalith data, search for a given site, and return the data if found:

#!/usr/bin/perl -w # # ch02/scanmegadata/scanmegadata: Scans the given megalith data file for # a given site. Uses colon-separated data. # ### Check the user has supplied an argument for ### 1) The name of the file containing the data ### 2) The name of the site to search for die "Usage: scanmegadata <data file> <site name>\n" unless @ARGV == 2; my $megalithFile = $ARGV[0]; my $siteName = $ARGV[1]; ### Open the data file for reading, and die upon failure open MEGADATA, "<$megalithFile" or die "Can't open $megalithFile: $!\n"; ### Declare our row field variables my ( $name, $location, $mapref, $type, $description ); ### Declare our 'record found' flag my $found; ### Scan through all the entries for the desired site while ( <MEGADATA> ) { ### Remove the newline that acts as a records delimiter chop; ### Break up the record data into separate fields ( $name, $location, $mapref, $type, $description ) = split( /:/, $_ ); ### Test the sitename against the record's name if ( $name eq $siteName ) { $found = $.; # $. holds current line number in file last; } } ### If we did find the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Perl, 4th Edition

Programming Perl, 4th Edition

Tom Christiansen, brian d foy, Larry Wall, Jon Orwant
Programming Perl, 3rd Edition

Programming Perl, 3rd Edition

Larry Wall, Tom Christiansen, Jon Orwant
Learning Perl, 8th Edition

Learning Perl, 8th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix
Learning Perl, 7th Edition

Learning Perl, 7th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix

Publisher Resources

ISBN: 1565926994Supplemental ContentErrata Page