SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2001
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Bizarre -- or rather unexpected -- Perl output

    I have a list of 250 items. I need to print the first 5 items to one file and then 20 each to successive files.

    I've got a bug somewhere in here because the output is 5 items each in all files.

    Here is the code: (btw, I've tried a bunch of other test statements before this all yeilding the same results)

    Code:
    $results_on_first_page = 5;
    $results_per_page = 20;
    $number_of_pages = 16;
    
    for ($i = 1; $i <= $number_of_pages; $i++)	{
    
    SWITCH: for ($i)	{
    	($i eq 1)  && do {
    		$schools_on_THIS_page = $results_on_first_page;
    		print "$i: $schools_on_THIS_page\n";
    		last;
    		};
    	($i > 1) && do  {
    		$schools_on_THIS_page = $results_per_page;
    		print "$i: $schools_on_THIS_page\n";
    		last;
    		};
    	die "No match: $!\n";
    }
    	#PRINT STATEMENTS HERE
    }
    Which outputs:
    Code:
    1: 5
    2: 5               </div>
    3: 5               </div>
    4: 5               </div>
    5: 5               </div>
    6: 5               </div>
    7: 5               </div>
    8: 5               </div>
    9: 5               </div>
    10: 5               </div>
    11: 5               </div>
    12: 5               </div>
    13: 5               </div>
    14: 5               </div>
    15: 5               </div>
    16: 5               </div>
    And for some reason, omitting the new line character prevents the whole line from being printed.
    <<< johnny chidiac
    johnnydaboy@mac.com

  2. #2
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    bizarre code produces bizarre results

    Since your question is a bit vague, all I can do is correct the code you posted, and write it in perlish fashion instead of C++ fashion:

    Code:
    $results_on_first_page = 5;
    $results_per_page = 20;
    $number_of_pages = 16;
    
    for (1 .. $number_of_pages)	{
       if ($_ == 1) {
          print "$_: $results_on_first_page\n";
          next;
       }
       print "$_: $results_per_page\n";
    }
    which outputs:

    1: 5
    2: 20
    3: 20
    4: 20
    5: 20
    6: 20
    7: 20
    8: 20
    9: 20
    10: 20
    11: 20
    12: 20
    13: 20
    14: 20
    15: 20
    16: 20

    I have no idea if thats what you want though.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •