Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

accessing/printing specific rows in a flat file DB

by Anonymous Monk
on Aug 01, 2003 at 16:06 UTC ( [id://280026]=perlquestion: print w/replies, xml ) Need Help??

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

I have a flat-file DB and I want the script to just access the rows starting with the prefix
“B00” so that when I print the data in a HTML file it DOES'NT print ALL the records.

The code I am using is

open(INF,"products") or dienice("Can't open productsDB: $!"); @data = <INF>; close(INF); foreach $i (@data) { chomp($i); ($productId,$nmPrice,$mPrice,$itemName +,$shortdesc,$image,$address) = split(/\|/,$i);

The database I am using is a flat file one like below but much much bigger, called
“products”

B001|72.00|65.00|Medicine Hands|Book|medicinehands.gif|medicinehands.html
B002|72.00|65.00|Amma Therapy|Book|medicinehands.gif|medicinehands.html
C001|55.00|65.00|T-shirt|Clothing|tshirt.gif|tshirt.html
C001|100.00|200.00|Jacket|Clothing|jacket.gif|jacket.html
X001|400.00|567.00|Golf Clubs|Sport|golf.gif|golf.html

When I print the data in the HTML file it prints all the records.
I.e. when I use the following command

print qq~ $itemName, $nmPrice ~;

it prints…

Medicine Hands, 72.00
Amma Therapy, 72.00
T-shirt, 55.00
Jacket, 100.00
Golf Clubs, 400.00

I only want to access the rows starting with the prefix “B00” so that
it prints only

Medicine Hands, 72.00
Amma Therapy, 72.00

Thanx in advance
Mark
inlimbo17@hotmail.com

Replies are listed 'Best First'.
Re: accessing/printing specific rows in a flat file DB
by projekt21 (Friar) on Aug 01, 2003 at 16:11 UTC

    Try:

    print qq~ $itemName, $nmPrice ~ if $productId =~ /^B00/;

    alex pleiner <alex@zeitform.de>
    zeitform Internet Dienste

Re: accessing/printing specific rows in a flat file DB
by Cine (Friar) on Aug 01, 2003 at 16:15 UTC
    Just filter when you are reading in your data.
    open(INF,"products") || dienice("Can't open productsDB: $!"); while(<>) { chomp; my ($productId,$nmPrice,$mPrice,$itemName,$shortdesc,$image,$address) + = split/\|+/; print qq~ $itemName, $nmPrice ~; if $productId =~ /^B00/; } close INF;


    T I M T O W T D I
Re: accessing/printing specific rows in a flat file DB
by yosefm (Friar) on Aug 01, 2003 at 16:12 UTC
    print qq~ $itemName, $nmPrice ~ if $productId =~ "^B00";
    Also, perldoc perlre for using regular expressions.
      yosefm,
      One minor nit:
      print qq~ $itemName, $nmPrice ~ if $productId =~ m"^B00";

      You have to make the m explicit if you are going to change the default delimiters in a match.

      Cheers - L~R

      Update: Apparently I am wrong and my apologies. It seems that the only time you have to make the m explicit if you are also dropping the binding operator by working on the default $_ as in if /^foo/ can't be done as if |^foo| it needs to be done as if m|^foo|

        That was what I thought when reading, too, but it seems to work.:

        > perl -e '$a="abcd"; print "yes\n" if $a =~ "^abc";' yes > perl -e '$a="abcd"; print "yes\n" if $a =~ "^yyy";' >

        Strange, anyone knows more about this?

        alex pleiner <alex@zeitform.de>
        zeitform Internet Dienste

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://280026]
Approved by Cine
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-24 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found