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

If I have several arrays.
@name = ("tim", "tom", "jon"); @time = ("5:00", "6:00", "7:00") ; @duty = ("floors", "urinals", "windows");
The amount contained in each array for my application is unknown, but there is alway a matching time and duty for each name.
What I need to do I use a loop to sequencially print each set like:
tim is on floor duty at 6:00 tom is on urinals duty at 7:00 jon is on windows duty at 8:00 (windows duty..harsh, i know)
I say I need to use a loop to do this because that is the only solution that I see. Perhaps an eagle eye can point out some other possibilities. Not too much to it. Just something that I know I should learn how to do. Thanks

Replies are listed 'Best First'.
Re: Arrays and Loops
by screamingeagle (Curate) on Feb 15, 2002 at 17:33 UTC
    how about :
    use strict; my @name = ("tim", "tom", "jon"); my @time = ("5:00", "6:00", "7:00") ; my @duty = ("floors", "urinals", "windows"); for my $i (0..$#name) { print "$name[$i] is on $duty[$i] duty at $time[$i]\n" }
Re: Arrays and Loops
by dragonchild (Archbishop) on Feb 15, 2002 at 17:57 UTC
    I'm shocked no-one's suggested the optimal solution.
    my @stuff = ( { name => 'tim', time => '5:00', duty => 'floors', }, # Fill in the others just as above ); for my $thing (@stuff) { print "$thing->{name} is on $thing->{duty} duty at $thing->{time}\ +n"; }
    Obviously, @stuff and $thing are bad variable names. I have no idea what better names would be in your context. In addition, these sound like they should be objects of some class. That way, you can deal with each as a give thing. For example, you could have a Jobs class. The attributes would be Who, What-to-do, When, as well as anything else you need to track. Maybe something like Who-manages or What-department or Salary.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: Arrays and Loops
by Masem (Monsignor) on Feb 15, 2002 at 17:59 UTC
    Use mapcar:
    use mapcar; my @data = mapcar { "$_[0] is on $_[2] duty at $_[1]" } ( \@name, \@ti +me, \@duty ); print join "\n", @data;

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

Re: Arrays and Loops
by derby (Abbot) on Feb 15, 2002 at 17:46 UTC
    nlafferty,

    Ohh just to show off ...

    #!/usr/local/bin/perl -w use strict; use Data::Dumper; my @name = ("tim", "tom", "jon"); my @time = ("5:00", "6:00", "7:00") ; my @duty = ("floors", "urinals", "windows"); my $x = 0; my %sked = map { $_,{ time => $time[$x], duty => $duty[$x++] } } @name +; print Dumper( \%sked );

    -derby

Re: Arrays and Loops
by ignatz (Vicar) on Feb 15, 2002 at 17:34 UTC
    #!/usr/bin/perl use strict; my @name = ("tim", "tom", "jon"); my @time = ("5:00", "6:00", "7:00") ; my @duty = ("floors", "urinals", "windows"); for (my $x = 0; $x < scalar(@name); $x++) { print @name[$x] . " is on " . @duty[$x] . " duty at " . @time[$x] +. "\n"; }

    UPDATE: /me lowers head in shame for having written the least pearly answer.
    ()-()
     \"/
      `
Re: Arrays and Loops
by nlafferty (Scribe) on Feb 15, 2002 at 23:15 UTC
    Thank you for you prompt reply everyone. I actually used the very first reply in my application because of the ease of maintaining the code. That's not to say that none of the other suggestions when insufficient. That's quite the opposite. It's nice to know that perl is so versitile that you can do what you want very many different ways. Depending on the purpose the other examples might fit a little better than the one that I used.