How to generate an array with random values, without using a loop?

后端 未结 24 2163
逝去的感伤
逝去的感伤 2020-12-13 19:23

How can I generate an array in Perl with 100 random values, without using a loop?

I have to avoid all kind of loops, like \"for\", foreach\", while. This is my exerc

24条回答
  •  情话喂你
    2020-12-13 20:03

    its ugly, but it works. the foreach is just to show that it does.

    #!/usr/bin/perl
    
    rand1();
    
    $idx = 1;
    
    foreach $item (@array) {
        print "$idx - $item\n";
        $idx++;
    }
    
    exit;
    
    
    
    sub rand1() {
        rand2();
        rand2();
        rand2();
        rand2();
    }
    
    sub rand2() {
        rand3();
        rand3();
        rand3();
        rand3();
        rand3();
    }
    
    sub rand3() {
        push @array, rand;
        push @array, rand;
        push @array, rand;
        push @array, rand;
        push @array, rand;
    }
    

提交回复
热议问题