push -- weird problem

B

butt-fuzz

i got a weird problem with push.


for ($x=1;$x<=12;$x++){
$sysDateLoop=`/usr/bin/cal $x 2003`;
push @sysDate, $sysDateLoop;
}
$sysDate=join '##--Month-Break--##', @sysDate;
this will produce jan, feb...dec. cals.


I tried a more lazy approach and I get weridness.
for ($x=1;$x<=12;$x++){
push @sysDate `/usr/bin/cal $x 2003`;
}
$sysDate=join '##--Month-Break--##', @sysDate;

look at this:
January 2003
##--Month-Break--##Su Mo Tu We Th Fr Sa
##--Month-Break--## 1 2 3 4
##--Month-Break--## 5 6 7 8 9 10 11
##--Month-Break--##12 13 14 15 16 17 18
##--Month-Break--##19 20 21 22 23 24 25
##--Month-Break--##26 27 28 29 30 31
##--Month-Break--##
##--Month-Break--## February 2003
##--Month-Break--##Su Mo Tu We Th Fr Sa
##--Month-Break--## 1
##--Month-Break--## 2 3 4 5 6 7 8
##--Month-Break--## 9 10 11 12 13 14 15
##--Month-Break--##16 17 18 19 20 21 22
##--Month-Break--##23 24 25 26 27 28
##--Month-Break--##
##--Month-Break--## March 2003
##--Month-Break--##Su Mo Tu We Th Fr Sa
##--Month-Break--## 1
##--Month-Break--## 2 3 4 5 6 7 8
##--Month-Break--## 9 10 11 12 13 14 15
##--Month-Break--##16 17 18 19 20 21 22
##--Month-Break--##23 24 25 26 27 28 29
##--Month-Break--##30 31
##--Month-Break--## April 2003
##--Month-Break--##Su Mo Tu We Th Fr Sa
##--Month-Break--## 1 2 3 4 5
##--Month-Break--## 6 7 8 9 10 11 12
##--Month-Break--##13 14 15 16 17 18 19
##--Month-Break--##20 21 22 23 24 25 26
##--Month-Break--##27 28 29 30
##--Month-Break--##
##--Month-Break--## May 2003
##--Month-Break--##Su Mo Tu We Th Fr Sa
##--Month-Break--## 1 2 3
##--Month-Break--## 4 5 6 7 8 9 10
##--Month-Break--##11 12 13 14 15 16 17
##--Month-Break--##18 19 20 21 22 23 24
##--Month-Break--##25 26 27 28 29 30 31
##--Month-Break--##
##--Month-Break--## June 2003
##--Month-Break--##Su Mo Tu We Th Fr Sa
##--Month-Break--## 1 2 3 4 5 6 7
##--Month-Break--## 8 9 10 11 12 13 14
##--Month-Break--##15 16 17 18 19 20 21
##--Month-Break--##22 23 24 25 26 27 28
##--Month-Break--##29 30
##--Month-Break--##
##--Month-Break--## July 2003
##--Month-Break--##Su Mo Tu We Th Fr Sa
##--Month-Break--## 1 2 3 4 5
##--Month-Break--## 6 7 8 9 10 11 12
##--Month-Break--##13 14 15 16 17 18 19
##--Month-Break--##20 21 22 23 24 25 26
##--Month-Break--##27 28 29 30 31
##--Month-Break--##
##--Month-Break--## August 2003
##--Month-Break--##Su Mo Tu We Th Fr Sa
##--Month-Break--## 1 2
##--Month-Break--## 3 4 5 6 7 8 9
##--Month-Break--##10 11 12 13 14 15 16
##--Month-Break--##17 18 19 20 21 22 23
##--Month-Break--##24 25 26 27 28 29 30
##--Month-Break--##31
##--Month-Break--## September 2003
##--Month-Break--##Su Mo Tu We Th Fr Sa
##--Month-Break--## 1 2 3 4 5 6
##--Month-Break--## 7 8 9 10 11 12 13
##--Month-Break--##14 15 16 17 18 19 20
##--Month-Break--#21 22 23 24 25 26 27
##--Month-Break--##28 29 30
##--Month-Break--##
##--Month-Break--## October 2003
##--Month-Break--##Su Mo Tu We Th Fr Sa
##--Month-Break--## 1 2 3 4
##--Month-Break--## 5 6 7 8 9 10 11
##--Month-Break--##12 13 14 15 16 17 18
##--Month-Break--##19 20 21 22 23 24 25
##--Month-Break--##26 27 28 29 30 31
##--Month-Break--##
##--Month-Break--## November 2003
##--Month-Break--##Su Mo Tu We Th Fr Sa
##--Month-Break--## 1
##--Month-Break--## 2 3 4 5 6 7 8
##--Month-Break--## 9 10 11 12 13 14 15
##--Month-Break--##16 17 18 19 20 21 22
##--Month-Break--##23 24 25 26 27 28 29
##--Month-Break--##30
##--Month-Break--## December 2003
##--Month-Break--##Su Mo Tu We Th Fr Sa
##--Month-Break--## 1 2 3 4 5 6
##--Month-Break--## 7 8 9 10 11 12 13
##--Month-Break--##14 15 16 17 18 19 20
##--Month-Break--#21 22 23 24 25 26 27
##--Month-Break--##28 29 30 31
##--Month-Break--##


--




If it doesn't work, plug it in.
If you're still having trouble, switch it on.
 
D

Domenico Discepola

If I interpret the goal of your program correctly & at the risk of not
answering your question ;-), have you looked at the Date::pCalc module?
 
E

Eric Schwartz

Domenico Discepola said:
If I interpret the goal of your program correctly & at the risk of not
answering your question ;-), have you looked at the Date::pCalc module?

Was it really necessary to quote all 123 lines of the original post to
ask that question?

-=Eric
 
B

butt-fuzz

I just want what comes out of 'cal' and mess around with it like
highlighing dates and arranging it in an HTML table.
I dont want to load another module. I just want something simple.
thanks.
 
D

David K. Wall

butt-fuzz said:
i got a weird problem with push.


for ($x=1;$x<=12;$x++){

C-style loops are usually not needed in Perl....
$sysDateLoop=`/usr/bin/cal $x 2003`;

The output of cal is in scalar context here.
push @sysDate, $sysDateLoop;
}
$sysDate=join '##--Month-Break--##', @sysDate;
this will produce jan, feb...dec. cals.

Yes, because each *month* is one array element.
I tried a more lazy approach and I get weridness.
for ($x=1;$x<=12;$x++){
push @sysDate `/usr/bin/cal $x 2003`;
^
You're missing a comma in the push().

But assuming that's a typo (you should copy and paste working code),
the problem is context.

push @sysDate, `/usr/bin/cal $x 2003`;

has `/usr/bin/cal $x 2003` in list context, so it puts each *line* of
the cal output into a separate array element. January 2003 (quoted
text below) will be at least 7 array elements.
}
$sysDate=join '##--Month-Break--##', @sysDate;

look at this:
January 2003
##--Month-Break--##Su Mo Tu We Th Fr Sa
##--Month-Break--## 1 2 3 4
##--Month-Break--## 5 6 7 8 9 10 11
##--Month-Break--##12 13 14 15 16 17 18
##--Month-Break--##19 20 21 22 23 24 25
##--Month-Break--##26 27 28 29 30 31


Force scalar context:

push @sysDate, scalar `/usr/bin/cal $x 2003`;


More Perlishly, you can replace the entire loop with one line:

push @sysDate, scalar `/usr/bin/cal $_ 2003` for (1..12);

Or if you prefer:

for (1..12) {
push @sysDate, scalar `/usr/bin/cal $_ 2003`;
}
 
D

David K. Wall

butt-fuzz said:
sorry, I'm not a real programmer.
lots of stuff I don't understand even if I read the docs a hundred
times.
(only wrote code for less than one year)

Then learn. :) I'm not primarily a programmer either, have little formal
training in it (obviously to some, I suppose), but I've learned a
considerable amount by reading posts here in comp.lang.perl.misc, reading
various recommended books (see learn.perl.org), and writing my own
programs and having to revise them when my first (or second or third...)
attempt turned out to be less-than-good. Just remember that Perl is not
the only language worth writing programs in, and other languages may do
things differently. That doesn't mean they're better or worse -- although
it can, for a particular problem domain -- just different. I need to
learn more languages and more theory and get more practice, but that's all
part of the fun, right?

Oh, and I for one will take you more seriously if you use a name other
than "butt-fuzz".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,058
Members
48,769
Latest member
Clifft

Latest Threads

Top