#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Perl making a word from a random string


    Hello

    I am way to new to perl and programming in general. I have just picked perl in my studies this year. And I have problems with an assignment. I am doing very good so far the problem is my lack of knowledge of it. I want to check if it is possible to create a word from a random generated string I produce. The word is in putted from the user. So I have both the string and the word but how can I check if the letters in the word are in the string ?


    Code:
    #!/usr/bin/perl
    print "Want to play a game ";		# Ask for input
    $a = <STDIN>;			# Get input
    chop $a;			# Remove the newline at end
    while ($a ne "STOP")		# While input is wrong...
    {
    
    
    sub generate_random_string
    {
            my $length_of_randomstring= 8;# the length of
                             # the random string to generate
    
            my @chars=('a'..'z');
            my $random_string;
            foreach (1..$length_of_randomstring)
            {
                    # rand @chars will generate a random
                    # number between 0 and scalar @chars
                    $random_string.=$chars[rand @chars];
            }
            return $random_string;
    }
    
    #Generate the random string
    my $random_string=&generate_random_string(11);
    
    print "Random string: ".$random_string."\n";
    #asking for the user to input a word
    print "Please enter a word\n";
    chomp($user_input_word = <>);
    print "\n";
    if ($user_input_word =~ /^[a-z]+$/) {
    }
    else
    {
    print "Sorry it seems you do not want to play a game with me.\n";
    print "You have entered a word that coints numbers\n";
    print "There is no word that coints numbers\n";
    exit;
    }
    print "you have entered $user_input_word\n";
    
        print "Again? ";	# Ask again
        $a = <STDIN>;		# Get input again
        chop $a;			# Chop off newline again
    }
    Thank you for having a look. Please do mind that this is my work in progress and there are mistakes as far the code I am ok. Few tips on how to do the asked will be create
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    Originally Posted by zamboz0
    Hello

    I am way to new to perl and programming in general. I have just picked perl in my studies this year. And I have problems with an assignment. I am doing very good so far the problem is my lack of knowledge of it. I want to check if it is possible to create a word from a random generated string I produce. The word is in putted from the user. So I have both the string and the word but how can I check if the letters in the word are in the string ?


    Code:
    #!/usr/bin/perl
    print "Want to play a game ";		# Ask for input
    $a = <STDIN>;			# Get input
    chop $a;			# Remove the newline at end
    while ($a ne "STOP")		# While input is wrong...
    {
    
    
    sub generate_random_string
    {
            my $length_of_randomstring= 8;# the length of
                             # the random string to generate
    
            my @chars=('a'..'z');
            my $random_string;
            foreach (1..$length_of_randomstring)
            {
                    # rand @chars will generate a random
                    # number between 0 and scalar @chars
                    $random_string.=$chars[rand @chars];
            }
            return $random_string;
    }
    
    #Generate the random string
    my $random_string=&generate_random_string(11);
    
    print "Random string: ".$random_string."\n";
    #asking for the user to input a word
    print "Please enter a word\n";
    chomp($user_input_word = <>);
    print "\n";
    if ($user_input_word =~ /^[a-z]+$/) {
    }
    else
    {
    print "Sorry it seems you do not want to play a game with me.\n";
    print "You have entered a word that coints numbers\n";
    print "There is no word that coints numbers\n";
    exit;
    }
    print "you have entered $user_input_word\n";
    
        print "Again? ";	# Ask again
        $a = <STDIN>;		# Get input again
        chop $a;			# Chop off newline again
    }
    Thank you for having a look. Please do mind that this is my work in progress and there are mistakes as far the code I am ok. Few tips on how to do the asked will be create
    If I was doing this I would create a hash which keeps all the random letters and the number of occurrences....

    Code:
    #!/usr/bin/perl
    
    use warnings;
    use strict;
    
    my $r = "lksdjglkrsajflsjfslwefhnsnohdqj";#my random example
    
    my %h = ();
    
    foreach (split('', $r))
    {
    	$h{$_}++;
    }
    
    foreach (sort(keys(%h)))
    {
    	print "$_ has a value->$h{$_}\n";
    }
    
    __END__
    Now you can look for letters in the hash, remembering to count down for each letter found in the hash.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    I don't know what you are trying to accomplish, but this is a simple example of checking if each character of one variable exists in another variable:
    Code:
    #!perl -w
    # Script: test.pl
    
    $rs  =  "aeecgcga";      # random string
    $ui  =  "age";           # user input value
    #$ui  =  "bef";
    
    @rs_a = split( (''), $rs );
    @ui_a = split( (''), $ui );
    
    print "@rs_a\n";
    foreach $ui_a (@ui_a) {
      print "$ui_a\n";
      if ( $ui_a ~~ @rs_a ) {
        $f = "Y"
      } else {
        $f = "N"
      }
    }
    
    if ( $f eq "N" ) {
      print "Some or all user input characters do not exist in random string\n";
    } else {
      print "User input characters exists in random string\n";
    }
    
    $ test.pl
    a e e c g c g a
    a
    g
    e
    User input characters exists in random string
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    Originally Posted by spacebar208
    I don't know what you are trying to accomplish, but this is a simple example of checking if each character of one variable exists in another variable:
    Code:
    #!perl -w
    # Script: test.pl
    
    $rs  =  "aeecgcga";      # random string
    $ui  =  "age";           # user input value
    #$ui  =  "bef";
    
    @rs_a = split( (''), $rs );
    @ui_a = split( (''), $ui );
    
    print "@rs_a\n";
    foreach $ui_a (@ui_a) {
      print "$ui_a\n";
      if ( $ui_a ~~ @rs_a ) {
        $f = "Y"
      } else {
        $f = "N"
      }
    }
    
    if ( $f eq "N" ) {
      print "Some or all user input characters do not exist in random string\n";
    } else {
      print "User input characters exists in random string\n";
    }
    
    $ test.pl
    a e e c g c g a
    a
    g
    e
    User input characters exists in random string
    One thing bothers me about this posting. Do you have to account for multi occurrences of a letter?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    I did not fully understand what you want to do, but is you have a list of existing words and want to check whether a given input may match one on the words, one possible way is to store your words in a normalized way in a hash. For example, you first put the letters of each word in alphabetical order and store un a hash the sorted letters (as the key) and the actual word (the value).

    Then, when given an new input, put it in alphabetical order and check whether the word exists.

    You'll have to take into accound that two distincts words may end up in the same normalized way, so you have to manage such collisions (possibly with a hash or arrays).

    This might help you for your problemů, or this may have nothing to do with what you want to do. Please fogive if this is the latter case.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by G4143
    If I was doing this I would create a hash which keeps all the random letters and the number of occurrences....

    Code:
    #!/usr/bin/perl
    
    use warnings;
    use strict;
    
    my $r = "lksdjglkrsajflsjfslwefhnsnohdqj";#my random example
    
    my %h = ();
    
    foreach (split('', $r))
    {
    	$h{$_}++;
    }
    
    foreach (sort(keys(%h)))
    {
    	print "$_ has a value->$h{$_}\n";
    }
    
    __END__
    Now you can look for letters in the hash, remembering to count down for each letter found in the hash.
    Hashes sound very interesting I had the same idea but with arrays and i was trying to search an array. But hashes with the keys will be much better.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by Laurent_R
    I did not fully understand what you want to do, but is you have a list of existing words and want to check whether a given input may match one on the words, one possible way is to store your words in a normalized way in a hash. For example, you first put the letters of each word in alphabetical order and store un a hash the sorted letters (as the key) and the actual word (the value).

    Then, when given an new input, put it in alphabetical order and check whether the word exists.

    You'll have to take into accound that two distincts words may end up in the same normalized way, so you have to manage such collisions (possibly with a hash or arrays).

    This might help you for your problemů, or this may have nothing to do with what you want to do. Please fogive if this is the latter case.
    I have a 8 letter string produced in my subroutine and I have input from the user. I want to see if the letters can be made from the 8 random letters.

IMN logo majestic logo threadwatch logo seochat tools logo