Re: Show different text based on random number
by kevbot (Vicar) on Apr 22, 2016 at 04:25 UTC
|
#!/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;
| [reply] [d/l] |
|
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?
| [reply] |
|
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;
| [reply] [d/l] |
|
|
|
Re: Show different text based on random number
by Your Mother (Archbishop) on Apr 22, 2016 at 04:20 UTC
|
my $rand = int(rand(4));
if ( 0 == $rand ) {} elsif ( 1 == $rand ) {} … etc
Don’t use perl4 style unless you know why you’re doing it, c.f. &subname (carries @_ context) instead of subname(). Always use strict and warnings. This seems XY and maybe solved elsewhere: Data::Random. | [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
|
| [reply] |
|
|
Re: Show different text based on random number
by golux (Chaplain) on Apr 22, 2016 at 04:36 UTC
|
Hi htmanning,
Even more generally, here's a subroutine that gives back a random integer in the range X to Y (inclusive):
sub random_number_from_X_to_Y {
my ($x, $y) = @_;
my $random = $x + int(rand($y - $x + 1));
return $random;
}
say
substr+lc crypt(qw $i3 SI$),4,5
| [reply] [d/l] |
Re: Show different text based on random number
by Your Mother (Archbishop) on Apr 22, 2016 at 16:52 UTC
|
Everyone's problems are more interesting than mine. As long as I can write idiopathically. Idiomatically, I mean. I meant idiomatically.
use strict; # DO NOT OMIT.
use warnings; # DITTO.
my $dir = shift || die "Gimme a dir full of text files!\n";
my $random_texter = build_texter(dir => $dir);
print $random_texter->();
exit;
sub build_texter {
require List::Util;
require Path::Tiny;
require Carp;
my %arg = ( dir => "YOU FORGOT TO SET THE DIR!",
suffix => "", # e.g., .txt
@_ );
my $dir = Path::Tiny::path($arg{dir});
-d $dir && -r _ || Carp::croak("$dir is not a readable directory")
+;
my @files = grep /\Q$arg{suffix}\E\z/, grep -f, $dir->children;
Carp::croak("There are no matching files in $dir") unless @files;
sub { [ List::Util::shuffle( @files ) ]->[0]->slurp };
}
| [reply] [d/l] |
|
I like your version, since you don't have to list the text files in the script...and you can add a choice simply by adding a file to the directory. It inspired me to update my version:
#!/usr/bin/env perl
use strict;
use warnings;
use Path::Tiny;
my $dir_path = path('text_files');
my @choices = $dir_path->children;
unless( scalar @choices > 0 ){
die "No text files found in $dir_path";
}
my $random_path = $choices[rand scalar @choices];
my $text = $random_path->slurp;
print "The text is: $text\n";
exit;
| [reply] [d/l] |