in reply to Re: Show different text based on random number
in thread Show different text based on random number

Thanks. I tried this but couldn't get it to work. The text is a rather large block so I put each of them in separate text files. I tried to open each file and assign it to a variable which I then stuff into the array, but it didn't work. I wonder if there is a better way since its such a large amount of text?
  • Comment on Re^2: Show different text based on random number

Replies are listed 'Best First'.
Re^3: Show different text based on random number
by kevbot (Vicar) on Apr 22, 2016 at 04:53 UTC
    If you have your text in individual files, you could do something like this (with the help of Path::Tiny):
    #!/usr/bin/env perl use strict; use warnings; use Path::Tiny; my @choices = ( path('c1.txt'), path('c2.txt'), path('c3.txt'), path('c4.txt'), ); my $random_path = $choices[rand scalar @choices]; my $text = $random_path->slurp; print "The text is: $text\n"; exit;
      Thanks. Do I have something wrong? This doesn't work:
      use Path::Tiny; my @choices = ( path('/usr/home/test/public_html/front-one.txt'), path('/usr/home/test/public_html/front-two.txt'), path('/usr/home/test/public_html/front-three.txt'), ); my $random_path = $choices[rand scalar @choices]; my $text = $random_path->slurp; print "$text";
        What do you mean by "doesn't work"? Are you getting any output at all, such as an error message? As far as I can tell, your code should work. Check the following:
        • Is Path::Tiny installed?
        • Check your file permissions. Do you have read access to those files?