Posts tagged: perl

1234567890

Hopefully I’m within liberty to reproduce this information here.  comp.lang.perl.misc user, Jean-Luc, has this to say about a certain timestamp (as copied from my newsreader):

Hi,

  As some of you know, Unix epoch time will reach the number
1,234,567,890
today at 23:31:30 UTC.

  If you don't know, epoch time is a measure of time in number of
(non-leap) seconds since Midnight UTC on January 1, 1970.  (That means
over
a billion seconds have passed since then.)

  Nostradamus once cryptically said, "The world will end when all
numbers
are visible."  I wonder if he was talking about this epoch time stuff.
(Disclaimer:  I just made that quote up.)

  You can view the epoch time ticking away by typing the following at
any
DOS/Unix prompt (provided you have Perl installed):

     perl -le "print time and sleep 1  while 1"

(You may want to synchronize your computer's clock by going to
http://www.time.gov/ .)

  You can see the exact time of 1234567890 for your timezone by
typing:

     perl -le "print scalar localtime 1234567890"

  Have a happy 1234567890 day!

  -- Jean-Luc

Only 1 hour 25 minutes to go.

Edit:

Dan C helpfully adds (yes, credit where credit is due)

watch -n 1 date +%s

as a handy way to watch the timer tick up to the timestamp.

34 minutes left.

Piksahl, just a little more

Working on Piksahl just a little bit more. Finally labelling all of the columns and rows.

Another sneak peek.

Another sneak peek.

Since the beginning I’ve had the functionality to label the columns/rows, or to at least build a string like “2,1,1” and output the column/row total as “4”. So today I finally got around to parsing the original string and outputting as “2\n1\n1\n” with a little chomp(stirng) for good measure, in the case of a column. Rows are simpler, needing only to s/,/  /g the string.

To do, align the row labels to the right, align those columns to the bottom. For both columns and rows I need to get rid of all that wasted space where there is no text. That will be easily done by calculating the maximum number of characters per row and column individually.

Piksahl, sneak peek.

Here's a look into the current state of Piksahl.

Here's a look into the current state of Piksahl.

I did have working images (photos as they are called in Perl-TK), but as a sacrifice for the photos working it ended up breaking something else. In the meantime, solid colours are being used with the canvas object createRectangle. But this is just a first look into Piksahl. I’ll get more in-depth later.

PerlTK and “activating” multiple canvas items

What I was looking for was every row and every column to be “lit up” wherever I put my mouse cursor. I started with the base code snippet:

#!/usr/bin/perl -w

use strict;
use Tk;

my ($x, $y);
my $iconSize = 20;
my ($width, $height) = (3, 3);
my ($canvasWidth, $canvasHeight) = ($iconSize+$width*$iconSize,
                                    $iconSize+$height*$iconSize);

my $MW = MainWindow->new;
my $MF = $MW->Frame->pack;
my $c = $MF->Canvas( -width => $canvasWidth,
                -height => $canvasHeight )->pack;

for ($y = $iconSize; $y < $canvasHeight; $y+=$iconSize)
{
    for ($x = $iconSize; $x < $canvasWidth; $x+=$iconSize)
    {
        $c->createRectangle ($x, $y,
                $x+$iconSize, $y+$iconSize,
                -fill => '#AFAFAF',
                -activefill => '#CFCFCF' );
    }
}

MainLoop

And posted to comp.lang.perl.tk for help. I received two solutions.

Zentara was first to reply, who wrote:

The secret to using the Canvas is tags. Read the perldoc Tk::Canvas
for everything on tags, and search groups.google.com for "Perl Tk canvas
tags" for many examples.
It's kind of an art, to see which tag juggling technique to use, but
generally you bind to motion or a tag, then find the current item, then
addtags or deltags, etc.  You can get very clever and make things very
efficient. For instance, in the following script, I do it the clunky
way, by itemconfiguring a bunch of items returned by find. But you
could also define a tag called "lit' (or something), and addtag lit
to all rows and cols on enter, then deltags lit on leaving. 

Second to reply was Jack D. He altered Zentara’s solution making it more compact.

#!/usr/bin/perl -w

use strict;
use Tk;

my ($x, $y);
my $iconSize = 20;
my ($width, $height) = (10, 10);
my ($canvasWidth, $canvasHeight) = ($iconSize+$width*$iconSize,
                                    $iconSize+$height*$iconSize);

my $MW = MainWindow->new;
my $MF = $MW->Frame->pack;
my $c = $MF->Canvas( -width => $canvasWidth,
                -height => $canvasHeight )->pack;

for ($y = $iconSize; $y < $canvasHeight; $y+=$iconSize)
{
    for ($x = $iconSize; $x < $canvasWidth; $x+=$iconSize)
    {
        $c->createRectangle ($x, $y,
            $x+$iconSize, $y+$iconSize,
            -fill => '#AFAFAF',
            -activefill => '#CFCFCF',
            -tags=>['rect',"row.$y", "col.$x"] );
    }
}

$c->bind('rect', '<Enter>', \&enter );
$c->bind("rect", "<Leave>", \&leave );

MainLoop;

sub findtag {
    my ($canv) = @_;
    my $id = $canv->find('withtag', 'current');
    my @tags = $canv->gettags($id);
    my ($row) = ( grep /^row\d*/, @tags );
    my ($col) = ( grep /^col\d*/, @tags );
    return ($row,$col);
}

sub enter {
    my ($canv) = @_;
    my ($r,$c) = findtag($canv);
    $canv->itemconfigure($r, -fill=>$canv->itemcget($r,-activefill));
    $canv->itemconfigure($c, -fill=>$canv->itemcget($c,-activefill));
}

sub leave{
    my ($canv) = @_;
    $canv->itemconfigure('rect', -fill=>'#AFAFAF');
}

__END__

And it certainly gets the job done. I’m interested in that particular trick for a game I’m currently programming. I call it Piksahl and I’ll release more details as the game sees more progress.

2-Dimensional Array in Perl

This example starts with the usual declarations.

#!/usr/bin/perl -w

use strict;

my ($width, $height) = (3, 4);
my ($x, $y);
my (@array2d, @rows);

Now we create the “Y-Axis” array which will give us “height”. Here I set the value to “0”.

for ($y = 0; $y < $height; $y++)
{
	push @rows, "0";
}

Then we stuff the “rows” array into the main array. We’re creating an array of arrays.

for ($x = 0; $x < $width; $x++)
{
	push @array2d, [ @rows ];
}

That covers creating a two-dimensional array, so let’s test the output to make sure it’s working as expected.

$array2d[1][3] = "1";
print "Just set position (1,3) to value '1'\n";

for ($y = 0; $y < $height; $y++)
{
	for ($x = 0; $x < $width; $x++)
	{
		print "($x,$y) => $array2d[$x][$y]\n";
	}
}

Output:

Just set position (1,3) to value '1'
(0,0) => 0
(1,0) => 0
(2,0) => 0
(0,1) => 0
(1,1) => 0
(2,1) => 0
(0,2) => 0
(1,2) => 0
(2,2) => 0
(0,3) => 0
(1,3) => 1
(2,3) => 0

Staypressed theme by Themocracy