[ic] incrementing array index in [perl]

Ryan Grace interchange-users@icdevgroup.org
Thu Feb 6 12:24:01 2003


--- Ryan Grace <ryan_grace@yahoo.com> wrote:
> Hi All,
> 
> I've been banging my head on this one for hours now
> and have searched the archives to no avail.  I'm
> hoping someone knows this one.
> 
> I've got a block of perl in a [perl] tag.  I'm
> trying
> to figure out the current page to show, the next
> page
> to show, and the previous page to show, all for some
> navigation links furthur down the page.  I'm
> assigning
> page numbers from the database to scratch variables
> to
> keep track of the next, current, and previous pages.
> 
> In the following code, the pagemin and pagemax
> scratch
> variables are set correctly, so I know the array of
> values form the DB is good.  The viewpage scratch
> variable is also set correctly.  I've got some
> [scratch] tags further down the page that show me
> the
> values they're set to each time.  However, the last
> two variables I can not get right.  I can't get the
> nextpage and prevpage vars to set.  I think it's a
> problem with the array indexes.  If I set one of the
> scratches like:  $Scratch->{prevpage} = $pages[2];
> it
> works fine.  But the way it is below does not work. 
> Any ideas?
> 
> ---------------------------
> 
> [perl tables=products interpolate=1]
>     my $db = $Db{products};
> 
>     $set = $db->query("SELECT page FROM products
> WHERE
> inactive != 1 AND collec\
> tion = '[cgi cn]' ORDER BY 1 ASC");
> 
>     @pages = ();
> 
>     foreach my $row (@$set) {
>         push (@pages, $$row[0]) if ($$row[0] ne
> "");;
>     }
> 
>     $Scratch->{pagemin} = $pages[0];
>     $Scratch->{pagemax} = $pages[$#pages];
> 
>     if ($CGI->{page}) {
>         $Scratch->{viewpage} = $CGI->{page};
>     }
>     else {
>         $Scratch->{viewpage} = $pages[0];
>     }
> 
>     my $incr = 0;
> 
>     foreach (sort @pages) {
>         if ($_ = $Scratch->{viewpage}) {
>             $prev = $Scratch->{incr} - 1;
>             $next = $Scratch->{incr} + 1;
>             $Scratch->{nextpage} = $pages[$next];
>             $Scratch->{prevpage} = $pages[$prev];
>         }
> 
>         $Scratch->{incr}++;
>     }
> 
>     return;
> [/perl]
> 
> Of course I've also tried the more obvious
> $Scratch->{prevpage} = $pages[$incr-1], which is my
> preferred method, but that doesn't work either.
> 
> Thanks for any insights.  It's making me crazy. :)
> 
> Ryan
> 

Well, I've got this fixed now and have learned
something about Interchange's perl along the way. 
Part of my problem was that in my sql query I wasn't
selecting distinct page numbers so I was getting an
array with repeated page numbers in it.

The other things is that Interchange still apparently
doesn't like doing math in the array index and prefers
a single variable or number instead.  For example, for
the life of me I could not get this or any variation
of this to work:

$Scratch->{prevpage} = $pages[$incr-1];

That's a pretty common thing to be able to do in Perl
and I do it all the time, but it doesn't fly in
Interchange.  Instead, I finally had luck doing this:

$prev = $incr - 1;
$Scratch->{prevpage} = $pages[$prev];

Anyone think I'm wrong?  Is there a way to get the top
example to work?

Thanks,

Ryan

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com