Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re (tilly) 3: alternating row colors

by tilly (Archbishop)
on May 18, 2001 at 05:38 UTC ( [id://81413]=note: print w/replies, xml ) Need Help??


in reply to (tye)Re2: alternating row colors
in thread alternating row colors

On both 5.005 and 5.6 (Linux) I do not see behaviour like that. Here is my code:
use Devel::Peek; my @a = 1..2; for (my $i = 0; 1; $i++) { print "Stats for $i\n"; Dump(\@a); <STDIN>; push @a, shift @a; }
The line to look for is "ARRAY =..." When there is an offset it will say "(offset=2)". I find it goes 0, 1, 2, then back to 0, then up to 10, back to zero, up to 10, and so on. Now if I try it with an array of 3 elements it goes up to 1, 0-9, 0-25, 0-25, 0-25... With 4 the same thing only it goes up to 24. Etc.

If anyone wants to play with this, here is an easy script to hack around with. Play with the array size and run it as many times as you want...

use strict; use IPC::Open3; my $array_size = shift || 2; my $code = <<'CODE'; use Devel::Peek; my @a = 1..COUNT; for (my $i = 0; 1; $i++) { print STDERR "Iteration=$i\n"; Dump(\@a); print STDERR "\nITERATE\n"; push @a, shift @a; } CODE $code =~ s/COUNT/$array_size/; open3(\*PIPE, ">&STDOUT", \*OUTPUT, "perl") or die "Cannot run perl: $ +!"; $/ = "ITERATE"; print PIPE $code; close PIPE; while (<OUTPUT>) { my $i; if (/Iteration=(\d+)/) { $i = $1; } else { die "Cannot find the iteration in\n$_"; } my $off = /offset=(\d+)/ ? $1 : 0; printf "Iteration%8d: Offset%6d\n", $i, $off; }

Replies are listed 'Best First'.
Re: Re (tilly) 3: alternating row colors
by jeroenes (Priest) on May 18, 2001 at 20:00 UTC
    I appreciate tilly's posting so much, that I just had to make figures from it. I modified it a bit to find the maximum offset for various array_lengths. Here are the graphs:
    Simple: max_offset 1600 ++----------+----------+-----------+-----------+----------+----- +-----++ + + + + +'/tmp/out.txt'** +***** + 1400 ++ * + ++ | * + | | * + | 1200 ++ * + ++ | * + | 1000 ++ * + ++ | * + | 800 ++ * + ++ | ****** * + | | * ********** * + | 600 ++ * *********** * + ++ | * ****** + | 400 ++ * * + ++ | *********** * + | | * ****** + | 200 ++ ********* + ++ + * **** + * + + + + + + 0 *****-------+----------+-----------+-----------+----------+----- +-----++ 0 100 200 300 400 500 + 600 array_length Better axes: max_offset / array_length 9 ++------+-------+-------+-------+--------+-------+-------+-------+- +-----++ + + + + + + +'/tmp/out2.txt'+* +***** + 8 ++ * + ++ | * * + | | * * + | 7 ++ * * + ++ | * * + | 6 +* * + ++ |* * + | 5 *+ * + ++ | * + | | * + | 4 ++ * + ++ | ** + | 3 ++ * * * ** * * * + ++ | ** ** ** *** ** ** ** + | | * * * * ** * ** **** **** * + | 2 ++ * * ** * *** * ** * ** * *** * + ++ + + + ** *+ ** * ****+ ****+ ** * **** + + 1 ++------+-------+-----**+------**------**+-----**+------**------**- +-----++ 1 2 3 4 5 6 7 8 9 + 10 log2( array_length)
    That's clear: a jump at every 2**(1..$n) length of the array. The jump is to 3 times the array_length at the jump. So now we can deduct the perl source code from this {grin}.

    There are small deviations due to the poor representation of lines on the ASCII field. For smaller array lengths, this relation does not hold.

    Jeroen
    "We are not alone"(FZ)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://81413]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-23 15:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found