Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Show different text based on random number

by kevbot (Vicar)
on Apr 22, 2016 at 04:25 UTC ( [id://1161178]=note: print w/replies, xml ) Need Help??


in reply to Show different text based on random number

You could put your text into an array, and then use the method shown in How do I pick a random element from an array?. For example,
#!/usr/bin/env perl use strict; use warnings; my @choices = ( "Choice 1", "Choice 2", "Choice 3", "Choice 4" ); my $random_choice = $choices[rand @choices]; print "The text is: $random_choice\n"; exit;

Replies are listed 'Best First'.
Re^2: Show different text based on random number
by htmanning (Friar) on Apr 22, 2016 at 04:43 UTC
    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?
      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";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1161178]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2024-04-19 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found