Search routine through flat file database 
Author Message
 Search routine through flat file database

Hi All,

I need to do a routine that will search through a flat file database by
category and print the results to a html file with 10 item per page.

For example:

items 1-10 on 1st page
items 11-20 on 2nd page
etc...

I have created a subroutine that creates an array of the items. see below:

---- SNIP ----
sub open_productfile {
 open(FILE,"<$productfile") || die "Can't Open File: $!";

 close FILE;
# You can block the sort by commenting out this line:


 $x=0;

  s/^ //;
      ($itemcode[$x], $category[$x], $price[$x], $shipping[$x],
$itemname[$x], $description[$x], $keywords[$x], $status[$x], $imagename[$x],
$colors[$x], $sizes[$x], $comments[$x], $dummy[$x]) = split(/::/, $item);
  $x++;
   }

Quote:
}

---- SNIP ---

Now I want to search on the $category[$x] variable and print out the
results. I have tried everything I know to do with no avail.

Any help will much appreciated.

TIA...
Gregory D. Fox

<>< <>< <>< <>< <>< ><> ><> ><> ><> ><>
Liberty Computer Solutions/Liberty Marketing
Web Site Hosting and Design
<>< <>< <>< <>< <>< ><> ><> ><> ><> ><>
(401) 823-5252/Fax: (401) 828-7057
<>< <>< <>< <>< <>< ><> ><> ><> ><> ><>
Visit our website at: www.libertymarketing.com
<>< <>< <>< <>< <>< ><> ><> ><> ><> ><>



Tue, 03 Sep 2002 03:00:00 GMT  
 Search routine through flat file database

Quote:

>   s/^ //;

$item =~ s/^ //;

Do you only want to remove the first white space? Do you really want to
remove _all_ leading whitespaces?

Quote:
> $colors[$x], $sizes[$x], $comments[$x], $dummy[$x]) = split(/::/, $item);

I'm pretty use you have to \:\: the double-colons.


Thu, 05 Sep 2002 03:00:00 GMT  
 Search routine through flat file database
Hi Ron,

I appreciate your input.

Quote:
> Do you only want to remove the first white space? Do you really want to
> remove _all_ leading whitespaces?

Maybe this is causing my problem. My array pointers are do not seem to
working right.

When I pass the variables, $category[$x] does not seem to be the right
category after the first 10. It is usually another category.

Here is more on it...

This is the GET method:
http://ecommercecart.com/cgi-bin/lawtechstore/shopcart.cgi?adminbutto...
h+By+Category&categorybutton=Accounting

This is the code that prints the first 10 items.

--- SNIP ---

$matchmarker = 10;

$offset = 0;
 for ($x=0; $x<=$matchmarker+$offset; $x++){
  if ($categorybutton ne $category[$x] && $categorybutton ne
l"){
   $offset++;
   if ($x >= $numlines-21){$offset--;}
  }

  if ($categorybutton eq $category[$x] || $categorybutton eq "All"){
   if ($status[$x] > 0 && $x < ($matchmarker + $offset)){
    if ($imagename[$x] eq "none"){
         &print_catmatches_no_image;

    }
    else{
    $imagename[$x]=$defaultgraphic;
    }
   }
  } # close "if equals" loop
 } # close "for" loop

---- END SNIP ---

Below is the code that I am using to generate 11-20, 21-30, etc. The code above seems to work ok. It's the pages after.

---SNIP---

$oldoffset = $offset;
 for ($x=$matchmarker+oldoffset; $x<$matchmarker+$oldoffset+10+$offset; $x++){
  if ($categorybutton ne $category[$x] && $categorybutton ne "All"){
   $offset++;
   if ($x>= $nummatches){$offset--;}
  }
  if ($categorybutton eq $category[$x] || $categorybutton eq "All"){
   if ($status[$x] > 0 && $x>=$matchmarker+$oldoffset && $x < ($matchmarker+10 + $offset)){
    if ($imagename[$x] eq "none"){
      &print_catmatches_no_image;

    }
    else{
    $imagename[$x]=$defaultgraphic;
    &print_catmatches;
    }
   }
  }

Quote:
}
> > $colors[$x], $sizes[$x], $comments[$x], $dummy[$x]) = split(/::/, $item);

> I'm pretty use you have to \:\: the double-colons.

No, this delimiter i
s working ok.

Any help will be appreciated.

Thanks.

Greg


Quote:

> >   s/^ //;

> $item =~ s/^ //;

> Do you only want to remove the first white space? Do you really want to
> remove _all_ leading whitespaces?

> > $colors[$x], $sizes[$x], $comments[$x], $dummy[$x]) = split(/::/,
$item);

> I'm pretty use you have to \:\: the double-colons.



Thu, 05 Sep 2002 03:00:00 GMT  
 Search routine through flat file database


...

Quote:
> > $colors[$x], $sizes[$x], $comments[$x], $dummy[$x]) = split(/::/, $item);

> I'm pretty use you have to \:\: the double-colons.

s/ use / sure /;

What make you 'pretty sure'?  Did you check the documentation before
posting?  Did you run a trivial test before posting?

Unsupported conjectures don't add value -- especially when they are
wrong.

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Thu, 05 Sep 2002 03:00:00 GMT  
 Search routine through flat file database


...

Quote:
> sub open_productfile {
>  open(FILE,"<$productfile") || die "Can't Open File: $!";

>  close FILE;
> # You can block the sort by commenting out this line:


You are sorting lexicographically by what you later describe as
'itemcode'.  Without seeing the data, one cannot tell if that is right.

Quote:

>  $x=0;

>   s/^ //;
>       ($itemcode[$x], $category[$x], $price[$x], $shipping[$x],
> $itemname[$x], $description[$x], $keywords[$x], $status[$x], $imagename[$x],
> $colors[$x], $sizes[$x], $comments[$x], $dummy[$x]) = split(/::/, $item);
>   $x++;
>    }
> }

Those are C-like indexes.  In Perl, it is better done using push().

But creating 13 parallel arrays is decidely unPerlish.  Imagine how
tedious it would be if you used 'use strict;' (as any sane Perl
programmer would).

If you really need to save the results of the split and access the ones
you want by name (which I doubt, but you haven't shown any code beyond
this), then an array of hash [references] might be best.  But I would
just do it using array indexes.  You can always equate them to named
constants if you want to.

Quote:
> Now I want to search on the $category[$x] variable and print out the
> results. I have tried everything I know to do with no avail.

How can one help without at least an example of what you tried, what you
hoped for, and what you got?

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Thu, 05 Sep 2002 03:00:00 GMT  
 Search routine through flat file database

Quote:
> > I'm pretty use you have to \:\: the double-colons.

> s/ use / sure /;

> What make you 'pretty sure'?  Did you check the documentation before
> posting?  Did you run a trivial test before posting?

Adding the slashes does not break the program.


Fri, 06 Sep 2002 03:00:00 GMT  
 Search routine through flat file database


Quote:
> > > I'm pretty use you have to \:\: the double-colons.

> > s/ use / sure /;

> > What make you 'pretty sure'?  Did you check the documentation before
> > posting?  Did you run a trivial test before posting?

> Adding the slashes does not break the program.

Please relate this (correct) observation to your previous (incorrect)
statement [emphasis added]:

  'you *have to* \:\: the double-colons'

Would it occur to you that your offhand advice might have led the
original poster to waste effort slashing the colons, to no avail?

A bit more humility would be appropriate.

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Fri, 06 Sep 2002 03:00:00 GMT  
 Search routine through flat file database

Quote:

>>   s/^ //;

> $item =~ s/^ //;

> Do you only want to remove the first white space? Do you really want to
> remove _all_ leading whitespaces?

>> $colors[$x], $sizes[$x], $comments[$x], $dummy[$x]) = split(/::/, $item);

> I'm pretty use you have to \:\: the double-colons.

I'm pretty sure you dont - they arent special in a regular expression.

/J\
--
Jonathan Stowe
http://www.gellyfish.com
http://www.tackleway.co.uk



Fri, 06 Sep 2002 03:00:00 GMT  
 Search routine through flat file database

Quote:

> > > I'm pretty use you have to \:\: the double-colons.

You're pretty surely wrong.

Quote:
> > s/ use / sure /;

> > What make you 'pretty sure'?  Did you check the documentation before
> > posting?  Did you run a trivial test before posting?

> Adding the slashes does not break the program.

Right, that's another entry in the filter list.


Fri, 06 Sep 2002 03:00:00 GMT  
 Search routine through flat file database
Hi Larry,

Here is more on it...

This is the GET method:
http://ecommercecart.com/cgi-bin/lawtechstore/shopcart.cgi?adminbutto...
h+By+Category&categorybutton=Accounting

This is the code that prints the first 10 items.

--- SNIP ---

$matchmarker = 10;

$offset = 0;

for ($x=0; $x<=$matchmarker+$offset; $x++){

    if ($categorybutton ne $category[$x] && $categorybutton ne
"){

    $offset++;

    if ($x >= $numlines-21){$offset--;}

    }

    if ($categorybutton eq $category[$x] || $categorybutton eq "All"){

        if ($status[$x] > 0 && $x < ($matchmarker + $offset)){

            if ($imagename[$x] eq "none"){

                &print_catmatches_no_image;

    }

    else{

        $imagename[$x]=$defaultgraphic;

     }

Quote:
}
}
}

---- END SNIP ---

Below is the code that I am using to generate 11-20, 21-30, etc. The code above seems to work ok. It's the pages after.

---SNIP---

$oldoffset = $offset;

for ($x=$matchmarker+$oldoffset; $x<$matchmarker+$oldoffset+10+$offset; $x++){

if ($categorybutton ne $category[$x] && $categorybutton ne "All"){

$offset++;

if ($x>= $nummatches){$offset--;}

Quote:
}

if ($categorybutton eq $category[$x] || $categorybutton eq "All"){

if ($status[$x] > 0 && $x>=$matchmarker+$oldoffset && $x < ($matchmarker+10 + $offset)){

if ($imagename[$x] eq "none"){

&print_catmatches_no_image;

Quote:
}

else{

$imagename[$x]=$defaultgraphic;

&print_catmatches;

Quote:
}
}
}
}

Any help will be appreciated.

Greg

Quote:



> ...

> > sub open_productfile {
> >  open(FILE,"<$productfile") || die "Can't Open File: $!";

> >  close FILE;
> > # You can block the sort by commenting out this line:



Quote:

> You are sorting lexicographically by what you later describe as
> 'itemcode'.  Without seeing the data, one cannot tell if that is right.


> >  $x=0;

> >   s/^ //;
> >       ($itemcode[$x], $category[$x], $price[$x], $shipping[$x],
> > $itemname[$x], $description[$x], $keywords[$x], $status[$x],
$imagename[$x],
> > $colors[$x], $sizes[$x], $comments[$x], $dummy[$x]) = split(/::/,
$item);
> >   $x++;
> >    }
> > }

> Those are C-like indexes.  In Perl, it is better done using push().

> But creating 13 parallel arrays is decidely unPerlish.  Imagine how
> tedious it would be if you used 'use strict;' (as any sane Perl
> programmer would).

> If you really need to save the results of the split and access the ones
> you want by name (which I doubt, but you haven't shown any code beyond
> this), then an array of hash [references] might be best.  But I would
> just do it using array indexes.  You can always equate them to named
> constants if you want to.

> > Now I want to search on the $category[$x] variable and print out the
> > results. I have tried everything I know to do with no avail.

> How can one help without at least an example of what you tried, what you
> hoped for, and what you got?

> --
> (Just Another Larry) Rosler
> Hewlett-Packard Laboratories
> http://www.hpl.hp.com/personal/Larry_Rosler/




Fri, 06 Sep 2002 03:00:00 GMT  
 Search routine through flat file database

Quote:
> A bit more humility would be appropriate.

I find it humorous that no one was able to give the OP any help with his
problem for three days until I posted my suggestion. Since then, every
subsequent post has concerned itself with my suggestion rather than to
help the original poster. The original poster knew my suggestion was
wrong

Quote:
>  "No, this delimiter is working ok."

Still no one offered any ideas for why is code was malfunctioning.

Heaven forbid we add in two extra characters in the fishing rod's user
manual while teaching someone to fish. Correcting this error is FAR more
important than some old pros sharing their knowledge with the beginner.



Sat, 07 Sep 2002 03:00:00 GMT  
 Search routine through flat file database
Thank you Ron...

I can still use help with this...

--
Gregory D. Fox

<>< <>< <>< <>< <>< ><> ><> ><> ><> ><>
Liberty Computer Solutions/Liberty Marketing
Web Site Hosting and Design
<>< <>< <>< <>< <>< ><> ><> ><> ><> ><>
(401) 823-5252/Fax: (401) 828-7057
<>< <>< <>< <>< <>< ><> ><> ><> ><> ><>
Visit our website at: www.libertymarketing.com
<>< <>< <>< <>< <>< ><> ><> ><> ><> ><>


Quote:
> > A bit more humility would be appropriate.

> I find it humorous that no one was able to give the OP any help with his
> problem for three days until I posted my suggestion. Since then, every
> subsequent post has concerned itself with my suggestion rather than to
> help the original poster. The original poster knew my suggestion was
> wrong

> >  "No, this delimiter is working ok."

> Still no one offered any ideas for why is code was malfunctioning.

> Heaven forbid we add in two extra characters in the fishing rod's user
> manual while teaching someone to fish. Correcting this error is FAR more
> important than some old pros sharing their knowledge with the beginner.



Sat, 07 Sep 2002 03:00:00 GMT  
 Search routine through flat file database

Quote:

>I need to do a routine that will search through a flat file database by
>category and print the results to a html file with 10 item per page.

>For example:

>items 1-10 on 1st page
>items 11-20 on 2nd page
>etc...

That is for later. But first:

Quote:
>I have created a subroutine that creates an array of the items. see below:

Not ONE array. 11 arrays. That's a it much.

Quote:
>sub open_productfile {
> open(FILE,"<$productfile") || die "Can't Open File: $!";


Each record still ends with a newline. Don't you want to get rid of
those?


Quote:
> close FILE;
># You can block the sort by commenting out this line:


> $x=0;

>  s/^ //;

Not sure what this is for. If it to get rid of leading whitespace,
shouldn't you sort AFTER that whitespace is removed?

Quote:
>      ($itemcode[$x], $category[$x], $price[$x], $shipping[$x],
>$itemname[$x], $description[$x], $keywords[$x], $status[$x], $imagename[$x],
>$colors[$x], $sizes[$x], $comments[$x], $dummy[$x]) = split(/::/, $item);
>  $x++;

As someone said: very C++ like. Alternatively, you can create a hash for
each record:

        my %record;

          description keywords status imagename colors sizes
          comments dummy)} = split(/::/, $item);

or, replacing the original array:

        $_ = \%record;

Quote:
>  }
>}
>Now I want to search on the $category[$x] variable and print out the
>results. I have tried everything I know to do with no avail.

grep() is good for that. Suppose you're looking for "coffee", to name
just a thing. Assuming my hash-style records:



the key "category" contains the string "coffee".

Now, how to you take a slice of this? In one of two ways (array indices
start at zero, so we skip the first 10 results):


or


There's a difference in memory usage and spice, but most of all: If
thereare fewer than 20 items in the array, one will fill up the results
with undef() so you get 10 items in total (slice), the other one will
NOT do that, and return fewer items than 10 (splice). It's the atter you
want. But, again , you can get rid of the undef's by doing:


How do you turn this into HTML (or text)? For example, by doing:


Product: $_->{product}
Category: $_->{category}
--
#EOT

As for HTML you need to escape the strings properly, either apply a

that trick using a tied hash to a function call, which might save some
typing. Actually, it only looks a bit less confusing. Gee, if only I
remembered an URL. MJD wrote about it, I recall, but I can't find it
back. Does  anybody remember?

        "$encode{$_->{category}}"

I can reconstruct the code, if I must.

So, to recap: you have to do the whole search for every page, y
ou take a selection taking an array slice or by splice, making sure you
get rid of undef's, and you fill up a string with the data from each
hash in that array.

Does that help?

--
        Bart.



Sat, 07 Sep 2002 03:00:00 GMT  
 Search routine through flat file database

Quote:

>As for HTML you need to escape the strings properly, either apply a

>that trick using a tied hash to a function call, which might save some
>typing. Actually, it only looks a bit less confusing. Gee, if only I
>remembered an URL. MJD wrote about it, I recall, but I can't find it
>back. Does  anybody remember?

That's the `Interpolation' module, available from:

        http://www.plover.com/~mjd/perl/Interpolation/

Or rather, it will be if Bell Atlandic ever decides to do something
about fixing their network problem so that plover.com is on the
Internet again.

In the meantime, Interpolation.pm is also available from CPAN.



Mon, 09 Sep 2002 03:00:00 GMT  
 
 [ 14 post ] 

 Relevant Pages 

1. searching a flat file database

2. Flat file databases >> mysql databases

3. How to convert flat file database to dbm databases

4. searching on a flat database

5. HELP: Flat Database Search Tools

6. Flat text file, CSV file or database?

7. Flat File Database module wanted.

8. Overwriting individual fields in a flat file database.

9. Flat file or a REAL database?

10. Allowing User to delete image files via HTML page in flat text database

11. Perl: extract requested fields of items that matched in a flat file database

12. Flat-File Database -- Reasonable Limits [\]

 

 
Powered by phpBB® Forum Software