in reply to Re^2: using perl to print out tomorrow's list
in thread using perl to print out tomorrow's list
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
|
|---|