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

In the attached code, I am trying to reference a hash contained in an array. However, each time I try to reference the hash, it says it is not a Hash. I'm following the examples with no luck. Can you help? Specifically with the ftp_sessions() array. Thanks This code is being put together to monitor multiple, long-running FTP jobs. I don't have that code in here ... basically stubbing it out with sleep statements.
#!/usr/local/bin/perl -w ###################################################################### +#################################### # + # # Program: GetDCBillingsFiles + # # + # # Description: Programs collects DC billings files from all the serv +ers located at the DC. This # # script is NOT intended to be a production application +. # # + # # Date: 12/27/03 + # # + # ###################################################################### +#################################### use strict; use threads; # Define global variables for program. my @locations = (); my @ftp_sessions = (); my $INTERVAL = 5; my $ACTIVE = 1; my $INACTIVE = 0; ###################################################################### +#################################### # + # # Subroutine: monitor_ftp + # # + # # Description: Subroutine collects information about active FTP sess +ions. # # + # ###################################################################### +#################################### sub monitor_ftp() { my $nbr = 0; my $status = 'status'; my $active_sessions = 0; sleep 1; print "Monitoring FTP sessions ... \n\n\n"; #printf "Sessions status <%d> and location <%s>\n", $ftp_sessions[ +0]->{status}, $ftp_sessions[0]->{loc}; do { my $session; $active_sessions = 0; for $session (@ftp_sessions) { $active_sessions += 1; $nbr++; } print "Active Sessions: $active_sessions\n"; # Check the list size and exit if all threads are complete. if ($active_sessions > 0) { # Monitor the sessions at fixed intervals. sleep $INTERVAL; } } until $active_sessions == 0; } ###################################################################### +#################################### # + # # Subroutine: start_ftp + # # + # # Description: Subroutine starts FTP sessions to collect data from l +ocations. # # + # ###################################################################### +#################################### sub start_ftp($$) { my ($nbr, $location) = @_; my $id = threads->self->tid; printf "FTP session: <%d> <%s>\n", $id, $location; $ftp_sessions[$nbr] = { status => 1, loc => $location }; ############# Insert FTP commands here ######################## my $time_delay = $id*5; sleep $time_delay; ############# Insert FTP commands here ######################## # Session complete. Reset the status and exit the session. $ftp_sessions[$nbr]{status} = 0; printf "Session <%d> complete.\n", $id; } ###################################################################### +#################################### # + # # Main Routine + # # + # ###################################################################### +#################################### $locations[0] = "awd1"; # Loop through the locations, starting a process to extract the data r +equired from each DC. my $loc = 0; my $nbr = 0; foreach $loc (@locations) { # Use threads to manage the FTP sessions. $ftp_sessions[$nbr] = threads->create("start_ftp", $nbr, $loc); $nbr++; } monitor_ftp(); sleep 40;

Replies are listed 'Best First'.
Re: data structure question
by jbware (Chaplain) on Dec 28, 2003 at 04:47 UTC
    I'm not sure exactly where you're trying to reference the array value as a hash, but I suspect you're referencing it outside start_ftp. In start_ftp you set the array value to a hash:
    $ftp_sessions[$nbr] = { status => 1, loc => $location };
    But the following statement sets the same array value to a scalar reference:
    $ftp_sessions[$nbr] = threads->create("start_ftp", $nbr, $loc);
    Looks like a problem of overwriting your hash value from inside the function w/ a scalar ref when the function is returned.
Re: data structure question
by pg (Canon) on Dec 28, 2003 at 04:56 UTC
    $ftp_sessions[$nbr] = threads->create("start_ftp", $nbr, $loc);

    That line pointed out by jbware is not needed actually, and he is right. As you do not join your threads later, there is no point to keep thread id.