Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Reference to a single array item in an array of hashes?

by tedrek (Pilgrim)
on Apr 05, 2004 at 22:48 UTC ( #342781=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://342781]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2021-09-19 15:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?