Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3697936
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:55:14+00:00 2026-05-19T04:55:14+00:00

#!/usr/bin/perl use strict; use warnings; sub paragraph { open my $file, <, dict.txt or

  • 0
#!/usr/bin/perl
use strict;
use warnings;

sub paragraph
{
open my $file, "<", "dict.txt" or die "$!";
my @words = <$file>;
close $file;

print "Number of lines:";
my $lines = <>;

print "Max words per line:";
my $range = <>;

for(my $i = 0; $i<$lines; $i++){
    my $wordcount = int(rand($range));

        for(my $s = 0; $s<=$wordcount; $s++){
            my $range2 = scalar(@words);
            my $word = int(rand($range2));
            print $words[$word]." ";

                if($s==$wordcount){
                    print "\n";}
                    }
                    }
                    }
paragraph;

I’m trying to learn programming, so I just wrote this simple script.

When running this code, I am getting use of uninitialized value errors… I can’t figure out why, but I sure I am just overlooking something.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:55:14+00:00Added an answer on May 19, 2026 at 4:55 am

    These two lines open the dict.txt file for writing and then try to read from it.

    open FILE, ">dict.txt" or die $!;
    my @words = <FILE>;
    

    Since you can’t read from a write-only file, it fails. If the file was writable, then it is empty now – sorry about your nice word list. Suggestion:

    open my $file, "<", "dict.txt" or die "$!";
    my @words = <$file>;
    close $file;
    

    Also, please learn to indent your braces in an orthodox fashion, such as:

    sub go
    {
        print "Number of lines:";
        my $lines = <>;
        print "Max words per line:";
        my $range = <>;
        for (my $i = 0; $i<$lines; $i++){
            my $wordcount = int(rand($range));
            for (my $s = 0; $s<$wordcount; $s++){
                my $range2 = 23496;
                my $word = int(rand($range2));
                my $chosen = @words[$word];
                print "$chosen ";
                if ($s=$wordcount){
                    print "\n";
                }
            }
        }
    }
    

    Also leave a space between ‘if’ or ‘for’ and the open parenthesis.

    Your assignment if ($s = $wordcount) probably isn’t what you intended; however, the condition if ($s == $wordcount) will always be false since it is in the scope of a loop with the condition $s < $wordcount. You need to rethink that part of your logic.

    On average, you should choose a better name for your function than go. Also, it is probably better to invoke it as go();.


    When I test compile your script, Perl warns about:

    Scalar value @words[$word] better written as $words[$word] at xx.pl line 19.
    

    You should fix such errors before posting.

    You have:

    my $range2 = 23496;
    my $word = int(rand($range2));
    

    Unless you have more than 23,496 words in your dictionary, you will likely be accessing an uninitialized word. You should probably use:

    my $range2 = scalar(@words);
    

    That then just leaves you with some logic problems to resolve.


    Given ‘dict.txt’ containing:

    word1
    word2
    word3
    word4
    nibelung
    abyssinia
    tirade
    pearl
    

    And ‘xx.pl’ containing:

    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    
    open my $file, "<", "dict.txt" or die $!;
    my @words = <$file>;
    close $file;
    
    sub go
    {
        print "Number of lines: ";
        my $lines = <>;
        print "Max words per line: ";
        my $range = <>;
        my $range2 = scalar(@words);
        for (1..$lines)
        {
            for (1..$range)
            {
                my $index = int(rand($range2));
                my $chosen = $words[$index];
                chomp $chosen;
                print "$chosen ";
            }
            print "\n";
        }
    }
    
    go();
    

    When I run it, I get:

    $ perl xx.pl
    Number of lines: 3
    Max words per line: 4
    word4 word3 word4 nibelung 
    abyssinia pearl word1 tirade 
    word3 word1 word3 word2 
    $
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider: #!/usr/bin/perl use strict; use warnings; my %hash; foreach (1 .. 10) { $hash{$_}
i have something like: #!/usr/bin/perl use strict; use warnings; use CGI::Simple; use DBI; my
When I do this #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $s =
This code triggers the complaint below: #!/usr/bin/perl use strict; use warnings; my $s =
!/usr/bin/env perl use warnings; use strict; my $text = 'hello ' x 30; printf
Which version would you prefer? #!/usr/bin/env perl use warnings; use strict; use 5.010; my
with #!/usr/bin/perl use warnings; use strict; use 5.010; use SQL::Parser; my $parser = SQL::Parser->new(
the following code #!/usr/bin/env perl use strict; use warnings; my @foo = (0,1,2,3,4); foreach
I have this script #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my %acc =
I have this script #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my %x1 =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.