in reply to mathematical equation

Assuming $top is supposed to be at the top of the page and $bottom is supposed to be at the bottom of the page and pages start at one and the first image index you want is zero, then you want:

$top = $page * 20 - 20; $bottom = $top + 19;
If pages and image indexes both start at zero, you want:
$top = $page * 20; $bottom = $top + 19;
If image indexes start at one and pages start at one, you want:
$top = $page * 20 - 19; $bottom = $top + 19; # A clearer Alternative: $bottom = $page * 20; $top = $bottom - 19;
If image indexes start at one and pages start at zero, you want:
$top = $pages * 20 + 1; $bottom = $top + 19;
This stuff really isn't that difficult. I can't imagine why everyone has had so much trouble with it.

I assume, BTW, that you are using Tie::IxHash or something. Otherwise you absolutely cannot rely on the order of the keys.

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: mathematical equation
by Anonymous Monk on Jul 28, 2003 at 04:35 UTC
    I was wondering if you could clear a few things up..

    I need different equations depending on what page I'm on, so I devised

    if ($page < 1) { for ( grep defined($_), ( reverse keys %upload )[ -19 .. 0 ] ) { } elsif ($page eq 2) { my $top = ($page * 20); my $bottom = $top - 19; } else ($page > 2) { my $top = $page * 20; my $bottom = $top - 19; }
    On the first if statement, it's printing incorrectly. It's printing images 4, 3, 2, 1, 2 instead of just 4, 3, 2, 1. It must be pulling a range that's to be used with one of the other loops, have any idea how? Because ?page=2 returns images 1, 2, 10, 9... (one of these two aren't supposed to be pulling the 1, 2 but I can't figure out which one).

    All pages=3 and up are working properly, it's just page 1 and two which are sharing the same pictures. (When I say picture numbers, it just means I uploaded test images with numbers on them so I could see which order they went in).

    I'm using something like that module you said. Any ideas on what part of the equations is wrong?

      I need different equations depending on what page I'm on, so I devised

      Well, you should test the code you "devise" before asking for help on it. It only takes a glance to see that won't even compile. You're missing a curly on your for loop and that else in else ($page > 2) would need to be an elsif. That's syntax... There's the logic error too. Even if that was an elsif you'd still be missing a case for $page == 1. You have cases for $page < 1 and $page eq 2 (which is another error, btw; it should be $page == 2). And that's ignoring the fact that your last case is exactly the same as your second one!

      And all that aside, why would you need to special case pages 1 and 2? It seems to me that you are making a simple task far too hard.

      Frankly, you aren't explaining yourself very well either and since you seem to have some command of idiomatic English, I don't think it is an ESL issue. If you can state your problem clearly and provide code that you've actually tried, I'm sure you'll be successfully helped. As things are now, you've posted essentially the same question twice, gotten a half dozen or more replies, and you seem to be no further along with your task.

      -sauoq
      "My two cents aren't worth a dime.";
      
      ( reverse keys %upload )[ -19 .. 0 ]

      That's not going to do what you think unless there are exactly 20 keys.

      my @array = 1 .. 40; print $_ for @array[-19 .. 0];

      Also, by grepping the defined elements out of your 20 element slice, you risk having less than 20 elements to work with. You probably want to grep the defined elements first and then work with the appropriate 20 element slice.

      -sauoq
      "My two cents aren't worth a dime.";