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

Stuck on something here, and I can't figure out how to do this properly... Given:
@array = ('name0','name1','name2','name3','name4');
How would I be able to knock off $array[0], so that the array will look like:
@array = ('name1','name2','name3','name4');
after X seconds?

This program is run through a consol (program doesn't die unless the user kills it), if that helps.

Any advice on this? Thanks.

Replies are listed 'Best First'.
Re: time and arrays
by robartes (Priest) on Jan 31, 2003 at 20:48 UTC
    If you don't need to do anything else while you are waiting your X seconds, you simply use sleep before shifting the array:
    my $seconds=10; sleep $seconds; my $element=shift @array;
    If, however, you want to spend your waiting time usefully, set an alarm:
    $SIG{ALRM}= sub {shift @array}; my $seconds=10; alarm $seconds; do_useful_stuff(); # after 10 seconds, @array gets shifted, no matter what you are doing +at that point.
    Note that this code is untested, but should give you an idea on how to proceed.

    Update: First I define $seconds, than I don't use it. Fixed that :).

    CU
    Robartes-

      Thanks all =) Robartes, that code is exactally what I needed (I needed to do "useful stuff" while it was counting). Thanks again =)
Re: time and arrays
by Hofmator (Curate) on Jan 31, 2003 at 20:40 UTC
Re: time and arrays
by dempa (Friar) on Jan 31, 2003 at 20:47 UTC
    Well, you can use shift to knock off the first element in the array. It's difficult to say how you should do it after X seconds without seeing the rest of the code. I guess sleep could be used but as I said it's difficult to know if it's possible without the rest of the code. (I mean, sleep will cause your program to "halt" (bad word, I know) for X seconds. Maybe use SIGALRM and alarm()?)

    -- 
    dempa

Re: time and arrays
by boo_radley (Parson) on Jan 31, 2003 at 21:18 UTC
    well, yes, but what are you doing?
    Sometimes it's helpful to say "my goal is to process the 0th element of @array every 10 seconds because I've got this time sensitive process going on in the real world, and it relies on my script feeding it $array[0]." rather than giving a specific example of an ambiguously purposed algorithm.