Akopia Akopia Services

[Date Prev][Date Next][Thread Prev][Thread Next][Minivend by date ][Minivend by thread ]

Re: [mv] Getting @keys from %DB in Perl structures



******    message to minivend-users from Mike Heins <mikeh@minivend.com>     ******

Quoting Randy Moore (ramoore@axion-it.net):
> ******    message to minivend-users from Randy Moore <ramoore@axion-it.net>     ******
> 
> Hi All,
> 
> I'm trying to learn to properly use the %Db structure inside of a [perl] tag.
> 
> I'm close, but I'm missing one (probably trivial) thing:  How to get an 
> array of keys for a database.
> 
> Specifically, I'm trying to loop through every item in my products 
> database.  If I pass the list of keys into my Perl through a scratch 
> variable, my code works fine, but this seems grossly inefficient.
> 
> The docs at
> http://www.minivend.com/minivend/docindex/10.00.MiniVend_Perl_Objects.html
> show examples using an '@keys' array, but never show how to create that array.
> 
> Here is the bit of code I'm playing with (it prepares an alphabetical index 
> to my product titles, suppressing letters where no product title starts 
> with that letter)
> 

Scrub this.

> [seti name=codelist][tag each products][loop-code],[/tag][/seti]

This would work:

 [perl products]


          my($title, $retval, $code, $ref, %seen, $first);
          my(@codelist) = split(/,/, $Scratch->{codelist});
          $ref = $Db{products};
          my $key;
          while( $key = $ref->each_record() ) {
                  $code = $_;
                  $title = $ref->field($code, "title");
                  $first = uc(substr($title,0,1));
                  $seen{$first}++;
          }
          foreach $Alpha ('A' .. 'Z') {
                  if ($seen{$Alpha}) {
                          $retval .= "[page href=\"alpha\" arg=\"$Alpha\"]
                                          $Alpha</A> \n";
                  }
          }
          return "$retval";
 [/perl]

But this is probably better:

[query arrayref=products sql="select title from products" st=db][/query]
[perl]
          my($retval, %seen, $first);
          # Recall the value stored by query tag
          my $ary = $Tmp->{products};
          foreach my $record ( @$ary ) {
                  $first = uc(substr($record->[0],0,1));
                  $seen{$first}++;
          }
          foreach my $Alpha ('A' .. 'Z') {
                  if ($seen{$Alpha}) {
                          $retval .= "[page href=\"alpha\" arg=\"$Alpha\"]
                                          $Alpha</A> \n";
                  }
          }
          return $retval;
[/perl]

The arrayref=products causes the results of a query to be stored as
an array of arrays, i.e.

[
    ["The Last Supper",],
    ["Mona Lisa",],
    ["Frame",],
] 

The $Tmp hash reference starts each page transaction empty and
holds the query reference.

-- 
Internet Robotics, 131 Willow Lane, Floor 2, Oxford, OH  45056
phone +1.513.523.7621 fax 7501 <mikeh@minivend.com>

Any man who is under 30, and is not liberal, has not heart; and any man
who is over 30, and is not a conservative, has not brains.
 -- Winston Churchill
-
To unsubscribe from the list, DO NOT REPLY to this message.  Instead, send
email with 'UNSUBSCRIBE minivend-users' in the body to Majordomo@minivend.com.
Archive of past messages: http://www.minivend.com/minivend/minivend-list


Search for: Match: Format: Sort by: