It's good that you try to figure out the workings of the code by inserting print statements in it. It's always a good start.

As to your third question:

say "month are @month"; month are ARRAY(0x1fa34e0) ARRAY(0x1fa3678) ARRAY(0x1fa41d0) ARRAY(0x1 +edd548) ARRAY(0x1edd5f0) ARRAY(0x1edd698)

You were expecting the elements of the array, and here they are, still, they look a bit strange. Thing is, @month is an array of arrays (to be more specific, an array of array references). Data structures such as these are best viewed with Data::Dumper.

use Data::Dumper; say Dumper \@month;

You will see, that your array looks like this:

$VAR1 = [ [ undef, undef, undef, undef, undef, undef, 1 ], [ 2, 3, 4, 5, 6, 7, 8 ], [ 9, 10, 11, 12, 13, 14, 15 ], [ 16, 17, 18, 19, 20, 21, 22 ], [ 23, 24, 25, 26, 27, 28, 29 ], [ 30, undef, undef, undef, undef, undef, undef ] ];

Now, you could just print it as is, with

foreach (@month) { say join " ", @$_; }

The code above means "iterate over the weeks of @month, treat each week as an array ( that's what @$_ does ), and print this array with one space between each element".

It you try it, it will look like a mess, because each element will occupy zero, one, or two characters, and the days will not align under each other.

The problem you are facing is this. When you are printing, you have an array that looks like this:

( undef, 2, 3, 10 )

and you want an array like this:

( ' ', ' 2 ', ' 3 ', '10 ')

with every element taking up exactly three spaces.

It's a good rule: when you want to transform one array into another array, you should be thinking about map. map iterates over the array, and maps the value of the original element ( $_ ) to the value you want in your target array. You can read about map in Map: The Basics, or here.

map, as used in your example, does exactly this:

foreach (@month) { say map { if ($_) { # if the element is not undef sprintf "%2d ", $_; # format it so it takes 2 spaces, and add one space to it } else { # if the element is undef ' '; # replace it with 3 spaces } } @$_; }

It looks different in your example, because it uses the ternary operator as a shorthand for the if ... else expression.

Now, as to your first question, the line:

my $font = $pdf->ttfont('DejaVuSans.ttf');

assumes the font file is in the same directory. The argument for ttfonf is the path to the file. You can of course use the core fonts, but I'm afraid the Russian characters will not work (this was the reason I used ttfont in my example).

- Luke


In reply to Re^3: using perl to print out tomorrow's list by blindluke
in thread using perl to print out tomorrow's list by Aldebaran

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.