in reply to Reference to a single array item in an array of hashes?

You can just use the built in aliasing of for loops, (I'm assuming this loop is initializing the array):

#!/usr/bin/perl use strict; use warnings; my $numTimes = 2; my $i = 0; my @jobTimes = ('') x $numTimes; # Make the array big enough foreach my $job (@jobTimes) { $job = { start => param("JobStart$i"), }; $i++; }

Update: made it compile and do what I mean.

I tried to swear at perl and it compiled!

Replies are listed 'Best First'.
Re: Re: Reference to a single array item in an array of hashes?
by ogxela (Novice) on Apr 05, 2004 at 22:55 UTC
    Using that code gives an error "Global symbol "$job" requires explicit package name".

    I tried changing it to this:

    foreach my %job (@jobTimes)
    but it didn't like that either. Syntax error.

      I fixed a few things. But you shouldn't be getting that error unless you are trying to use $job outside of the loop.

      I tried to swear at perl and it compiled!

        Teach me not to list enough code. It's actually erroring a little later in the loop. Here's the full loop:

        my $i = 0; my @jobTimes = ('') x $numTimes; #initialize array foreach my $job (@jobTimes) { $job = { start => param("JobStart$i"), end => param("JobEnd$i"), }; $job{'start'} =~ s/^0+//; $job{'end'} =~ s/^0+//; $i++; }

        It's giving the "Global Symbol "%job" requires explicit package" error on the regexpen.

        --Alex