CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [iThunder] Code wont work for names other than last two in the list!!: Edit Log



Zhris
Enthusiast

Oct 18, 2013, 1:07 PM


Views: 6083
Re: [iThunder] Code wont work for names other than last two in the list!!

Hi,

Its always best to attach the actual input file because I can't guarantee that my input file will be identical to yours after copying and pasting. For example, there is an extra whitespace on the end of each word which I have removed.


Quote
The main goal is to split the above list into two sublists i.e. words1 = [bubble pencil] and words2 = [john catherine]. User is asked for input i.e. name and a secret word. If name matches any of names in words2, the program should print test and exit.


Based on your description above, for now you do not make use of the secret word. Here is my substantially modified version I am testing against your data using http://www.compileonline.com/execute_perl_online.php. NOTE that its been modified to work with compileonline only and will not work in your environment. I have switched the arrays and do not experience any issues either way (using @words1 or @words2), so long as I choose an appropriate name...


Code
init_words();  
print "What is your name Mr. \n";
$name = <STDIN>;
chomp ($name);
if ($name =~ /^randal\b/i){
print "Hello, Randal, How are you doing \n";
}
else{
print "Hello, $name!\n";
print "Tell the secret word\n";
$guess = <STDIN>;
chomp ($guess);
print good_word($name,$guess);
}


sub init_words {
local $/ = "\r\n";
open (WORDSLIST, "input.txt") || die "can't open wordslist: $!";
$k = 1;
$a = 0;
$b = 0;
while (defined ($name = <WORDSLIST>)) {


if ($k % 2 == 0){

chomp ($name);

@words1[$a] = $name;
++$k;
++$a; }

else{
chomp ($name);
@words2[$b] = $name;
++$k;
++$b;

}

}

close (WORDSLIST) || die "couldn't close wordlist: $!";
}

sub good_word {
my ($somename, $someguess) = @_;
$somename =~ s/\W.*//;
$somename =~ tr/A-Z/a-z/;
if ($somename eq "randal"){
return 1;}
else {
#$n = 0;
$t = scalar @words1;
$u = scalar @words2;
print "the words2 array is @words1\n\n";
for ($d = 0; $d < $t; $d++)
{
print "currently name in array is [@words1[$d]]\n";
print "The value of somename is [$somename] \n\n";
if ("@words1[$d]" eq "$somename"){
print "test";
return 1;}
}
#print "The final value of d is $d";
#print " The final value of array is @words1[$d]";
#if ("groucho" eq $someguess){
#return 1;}
#else{
#while ($n < $t){
#if (@words1[$n] eq $someguess) {
#return 1;}
#else { ++$n};
}

}
# }
#}


Since I don't understand the problem, I can't be of much more help.

Here is my rework of your script. You can switch whether you use odd or even words by negating the (++$i % 2) i.e. !(++$i % 2)...


Code
#!/usr/bin/perl 
use strict;
use warnings;

chomp(my $name = <STDIN>);

my $words = init_words('wordslist.txt');
my $response = good_word($words, $name);

print $response;

sub init_words
{
my $filename = shift;

open my $fh, '<', $filename or die "cannot open $filename: $!";
my @words = <$fh>;
close $fh;

chomp @words;

return \@words;
}

sub good_word
{
my ($words, $name) = @_;

#$name =~ s/\W.*//;
$name = lc $name;

my $i = 0;
return 1 if (($name eq 'randal') || (grep { (++$i % 2) && ($_ eq $name) } @$words));

return 0;
}


or...


Code
#!/usr/bin/perl 
use strict;
use warnings;

chomp(my $name = <STDIN>);

my ($odd_words, $even_words) = init_words('wordslist.txt');
my $response = good_word($even_words, $name);

print $response;

sub init_words
{
my $filename = shift;

open my $fh, '<', $filename or die "cannot open $filename: $!";
my @words = <$fh>;
close $fh;

chomp @words;

my ($i, @odd_words, @even_words);
push @{++$i % 2 ? \@odd_words : \@even_words}, $_ for (@words);

return (\@odd_words, \@even_words);
}

sub good_word
{
my ($words, $name) = @_;

#$name =~ s/\W.*//;
$name = lc $name;

return 1 if (($name eq 'randal') || (grep { $_ eq $name } @$words));

return 0;
}


Chris


(This post was edited by Zhris on Oct 18, 2013, 2:24 PM)


Edit Log:
Post edited by Zhris (Enthusiast) on Oct 18, 2013, 1:07 PM
Post edited by Zhris (Enthusiast) on Oct 18, 2013, 1:19 PM
Post edited by Zhris (Enthusiast) on Oct 18, 2013, 1:56 PM
Post edited by Zhris (Enthusiast) on Oct 18, 2013, 2:24 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives