beginner wrote:
> Hi,

Hello,

> I am trying to create an array of references but am getting stuck at 
> how/when to push the reference into the array
> 
> Below is some sample data and a snip of what I have been trying. What 
> is happening is that the reference $times is getting pushed into the 
> array before all the keys are defined so I am getting
> 
> [snip]
> 
> what I'd like is 
> 
> $VAR11 = {
>             'dom' => '09',
>             'day' => 'Fri' '
>             'morning' => '09:24',
>             'out' => '13:10',
>             'back' => '14:17',
>             'home' => '16:09'
>           };
> 
> Can someone point out where I am going wrong please.
> 
> 
> ======== My effort ===============
> use strict;
> use warnings;
> 
> ...snip
> 
>  my @times;
>  my ($i,$key,$day,$dom,$mon,$time,$hour,$week_starting);
>  my $times;
>  while (defined($i = <FH>)) {
>          next if ($i !~ /^(x|j|k|z)/);
>          chomp($i);
> 
>  #                                                       day    dom   
> mon         time           hour
>          ($key,$day,$dom,$mon,$time,$hour) = ($i =~ 
> /^(\w)\s+(\w+)\s+(\d+)-(\w+)-\d+\s+(\d+:\d+):.*(\d+:\d+|-\d+:-\d+)/);
>          my $colour = 'black';
> 
>          if ($key =~ /x/i ) {
>                  $times = {
>                          day     => $day,
>                          dom     => $dom,
>                          morning => $time,
>                  };
>          }
>          elsif ($key =~ /j/ ) {
>                  $times = { out => $time};
>          }
>          elsif ($key =~ /k/) {
>                  $times = { back => $time};
>          }
>          elsif ($key =~ /z/) {
>                  $times = { home => $time};
>          }
>          push(@times,$times);
>  }

It looks like you need something like:

        if ( 'x' eq lc $key ) {
            push @times, {
                day     => $day,
                dom     => $dom,
                morning => $time,
                };
        }
        elsif ( 'j' eq $key ) {
            $times[ -1 ]{ out } = $time;
        }
        elsif ( 'k' eq $key ) {
            $times[ -1 ]{ back } = $time;
        }
        elsif ( 'z' eq $key ) {
            $times[ -1 ]{ home } = $time;
        }
}



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to