Perl: infinite loop array?

I need an infinite loop into finite array...

Example:

@name = ('John', 'Helen', 'Dave');

1=John
2=Helen
3=Dave 
4=John
5=Helen
6=Dave 
7=John
8=Helen
9=Dave 
...etc

Is it possible? Thanks

Answers


It sounds as if you need to index the finite array with the index number modulo the size of the array:

foreach my $i (1..100) { printf "%d=%s\n", $i, $name[$i % scalar(@name)]; }

my @dow = ( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' );
print $dow[7 % scalar(@dow)], "\n";
print $dow[125 % scalar(@dow)], "\n";

Etc.


I don't know why anyone would want to do that, but this will do what you want:

my $i=0;
my $cnt=1;
my @name = ('John', 'Helen', 'Dave');

    while($i<scalar (@name )){

        print $cnt."".$name[$i];
        $i++;
        $cnt++;
        $i=$i==scalar(@name)?0:$i;


}

Need Your Help

grizzly jaxws schemaLocation

jax-ws grizzly

i'm publishing a soap ws api from within my java app.

java replaceAll(regex, replacement) regex command

java regex replaceall

Hello I would like to know if anyone knows the regex command to remove the following

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.