Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm doing something stupid thats causing this code not to work properly.

use Date::Calc::Iterator; # This puts all the dates from Dec 1, 2003 to Dec 10, 2003 in @dates +1 # @dates1 will contain ([2003,12,1],[2003,12,2] ... [2003,12,10]) ; my $i1 = Date::Calc::Iterator->new(from => [2003,12,1], to => [2003, +12,10]) ; my @dates1 ; push @dates1,$_ while $_ = $i1->next ; foreach my $v (@dates1) { print $v; }
This code is basically from the Date::Calc::Iterator Docs, but I added the foreach to print out the values of @dates1

The code above prints out:
ARRAY(0x1a2f2a4)ARRAY(0x17cb864)ARRAY(0x17cb7bc)ARRAY(0x17cb810)ARRAY( +0x17cb594)ARRAY(0x1a619e0)ARRAY(0x1a619a4)ARRAY(0x1a619d4)ARRAY(0x1a6 +1a1c)ARRAY(0x1a61aa0)
What am I doing wrong? How come I can't print the values of @dates1 correctly.

Thanks, Erold

Replies are listed 'Best First'.
Re: Date::Calc::Iterator
by prasadbabu (Prior) on Jul 14, 2005 at 06:28 UTC

    The return value is array reference. So you have to dereference the array reference as shown below.

    print "@$v\n";

    prints the output:

    2003 12 1 2003 12 2 2003 12 3 2003 12 4 2003 12 5 2003 12 6 2003 12 7 2003 12 8 2003 12 9 2003 12 10

    updated:

    Prasad

Re: Date::Calc::Iterator
by gopalr (Priest) on Jul 14, 2005 at 07:00 UTC
      Why would you want to use Data::Dumper? The problem of the OP is very obvious and a simple statement about Data::Dumper without explanation doesn't add anything to the understanding of the problem (which is: $v is a array reference and should be dereferenced via @$v).

      Paul

      Hi,

      Is there any other way to carry out this in Perl w/out using this module? I got it printing out the values correctly thanks to both your replies. But now I realized I can't specify the date range values.

      For example if I altered the code like this, it makes an error:
      use Date::Calc::Iterator; # This puts all the dates from Dec 1, 2003 to Dec 10, 2003 in @dates +1 # @dates1 will contain ([2003,12,1],[2003,12,2] ... [2003,12,10]) ; my $sr = "2005,07,1"; my $i1 = Date::Calc::Iterator->new(from => [$sr], to => [2005,10,1]) + ; my @dates1 ; push @dates1,$_ while $_ = $i1->next ; foreach my $v (@dates1) { print "@$v<br>"; }
      Since I used the scalar inside the from brackets, it prints an error saying this: Invalid start date at C:/Perl/site/lib/Date/Calc/Iterator.pm line 25.

      Thank you lots, Erold

        You cannot asign a scalar value as a input for that object and also not posible to convert the string to an array by giving "[ ]" symbols. But you can give array as a input as shown below:

        use Date::Calc::Iterator; # This puts all the dates from Dec 1, 2003 to Dec 10, 2003 in @dates +1 # @dates1 will contain ([2003,12,1],[2003,12,2] ... [2003,12,10]) ; my @sr = (2005,07,1); my $i1 = Date::Calc::Iterator->new(from => [@sr], to => [2005,10,1]) +; my @dates1 ; push @dates1, $_ while $_ = $i1->next ; foreach my $v (@dates1) { print "@$v\n"; }

        Regards,
        Velusamy R.