# Always start your scripts with use strict and use warnings. use strict; use warnings; $| = 1; # autoflush your print statements # This subroutine slurps a text file into a string. # Note the use of a lexical file handle and three-argument open # and localization of the $/ global variable. # Note that the lexical $fh is automatically closed at end # of scope, i.e. when the subroutine exits. sub slurp_file { my $file = shift; open( my $fh, '<', $file ) or die "error: open '$file': $!"; local $/ = undef; return <$fh>; } # Note: you can use forward slashes on Windows (saves you escaping backslashes). my $file = 'c:/Users/yashu/Desktop/SNLP/EX4/test.txt'; my $mytext = slurp_file($file); $mytext = lc $mytext; # $mytext =~ s/[^a-z|]/ /g; # your original replaces with space $mytext =~ s/[^a-z|]//g; # this deletes [^a-z|] instead my @wordarray = split //, $mytext; my $iter = 0; print "@wordarray\n"; # Note with a for loop you don't need the ugly array indexes. for my $wordchar (@wordarray) { while (1) { print " \n Enter the character then press ENTER : "; my $char = ; chomp $char; if ( length($char) != 1 ) { print "you must enter exactly one character (not $char)\n"; next; } print "\n inputted character is : $char "; if ( $char eq $wordchar ) { print "Correct !!"; last; } print "\n Wrong please try again "; $iter++; print "\n Number of attempts : $iter "; } } print "\nNumber of failed attempts: $iter\n";