On Thu, Mar 12, 2009 at 03:40, Kammen van, Marco, Springer SBM NL
<marco.vankam...@springer.com> wrote:
> Hi All,
>
>
>
> I'm trying to generate random sentences using a few words, but can't get
> the thing to work......
>
> I know I'm doing something wrong but what? :-D
>
>
>
> Any help is appreciated!
>
>
>
>
>
> #!/usr/bin/perl -w
>
> #
>
>
>
> my $count = "0";
>
>
>
> my $word0 = "aap";
> my $word1 = "nood";
> my $word2 = "mies";
> my $word3 = "boot";
> my $word4 = "pet";
> my $word5 = "boom";
> my $word6 = "klok";
> my $word7 = "bel";
> my $word8 = "fiets";
> my $word9 = "toeter";
snip

This is a bunch of scalars, not an array.


>
>
> while ($count < 5) {
>
>  ++$count;

This is better done as a for loop with a range operator:

for my $count (0 .. 4) {


snip
> sub wordgen {
>
>  my $number = int(rand(10));
>
>  $firstword = $word{$number};
snip

There is no word array, and even if there was $word{$number} gets the
value associated with the key $number from the hash named %word.


#!/usr/bin/perl

use strict;
use warnings;


my @words = qw(
        aap   nood   mies boot
        pet   boom   klok bel
        fiets toetor
);

for my $i (0 .. 5) {
        print "$words[int rand @words] ";
}
print "\n";



-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to