perl question my @guesses=() difference between @guesses;


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl question my @guesses=() difference between @guesses;
# 1  
Old 07-15-2007
perl question my @guesses=() difference between @guesses;

what is the point of ?

my @guesses=();


why can't I do

my @guessses;

Is there any difference?
# 2  
Old 07-15-2007
In fact, I am gonna extend this question even longer,

Below is where this question came from... it is hangman perl program.

Code:
   #!/usr/bin/perl -w

   @words=qw( internet answers printer program );
   @guesses=();
   $wrong=0;

   $choice=$words[rand @words];
   $hangman="0-|--<";

  @letters=split(//, $choice);
  @hangman=split(//, $hangman);
  @blankword=(0) x scalar(@letters);
  OUTER: 
      while ($wrong<@hangman) {
          foreach $i (0..$#letters) {
                  if ($blankword[$i]) {
                          print $blankword[$i];
                  } else {
                          print "-";
                  }
          }
          print "\n";
          if ($wrong) {
                  print @hangman[0..$wrong-1]
          }
          print "\n Your Guess: ";
          $guess=<STDIN>;  chomp $guess;
          foreach(@guesses) {
                  next OUTER if ($_ eq $guess);
          }
          $guesses[@guesses]=$guess;
          $right=0;
          for ($i=0; $i<@letters; $i++) {
                  if ($letters[$i] eq $guess) {
                          $blankword[$i]=$guess;
                          $right=1;
                  }
          }
          $wrong++ if (not $right);
          if (join('', @blankword) eq $choice) {
                  print "You got it right!\n";
                  exit;
          }
  }
  print "$hangman\nSorry, the word was $choice.\n";


where I really don't understand is below,

Say @letters = n o w,

and iterating over 0..$#letters would assign i to
$letters[0]=n
$letters[1]=o
$letters[2]=w

and @blankword was 0 0 0,

how is $blankword[$i] is one of n o w ???

Code:
      while ($wrong<@hangman) {
          foreach $i (0..$#letters) {
                  if ($blankword[$i]) {
                          print $blankword[$i];
                  } else {
                          print "-";
                  }
          }

# 3  
Old 07-15-2007
@blankword is an array of all matched entries so far. Elements are set one by one here if the character the user types matches one of the letters of the picked word:

Code:
          for ($i=0; $i<@letters; $i++) {
                  if ($letters[$i] eq $guess) {
                          $blankword[$i]=$guess;
                          $right=1;
                  }
          }

# 4  
Old 07-15-2007
Quote:
Originally Posted by hankooknara
what is the point of ?

my @guesses=();


why can't I do

my @guessses;

Is there any difference?
They are the same. But

my @guesses = ();

makes your intent a little bit more explicit, but not strictly necessary.
# 5  
Old 07-16-2007
why not use a hash for each letter in the answer?

Code:
$word = "hello";

map $H{$_}++, split //, $word;

while (($k,$v) = each %H) {

    print "$k = $v\n";
}

Code:
l = 2
e = 1
h = 1
o = 1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date difference using perl

Hi All, I am using the below code to get the days difference between 2 dates ddate="05/31/2018" bdate="06/10/2018" diff=$(perl -e 'use Time::Local; my ($month1,$day1,$year1)=split /\W+/, $ENV{'ddate'}; my ($month2,$day2,$year2)=split /\W+/, $ENV{'bdate'}; my $time1 =... (5 Replies)
Discussion started by: jayadanabalan
5 Replies

2. Shell Programming and Scripting

Perl : difference between two dates in years

hello folks, I have a requirement in which I have to calculate the difference of localdate(today's date) and the given(earlier) date and to check whether the difference is exactly a year or more than that(can be 1 year or 2 years or 3 years.. ) . Could anyone please let me know the logic... (2 Replies)
Discussion started by: scriptscript
2 Replies

3. Shell Programming and Scripting

days difference perl

Hi I have the following issue. the headeer in the file contains as follows IMAHDR tsmdsl01 EMBS_DAT 20120911 20120911 233656S000000000000000 001 So the fifth field in the header is a string that represents the file arrival date.(20120911) yyyyMMDD I just need to compare... (1 Reply)
Discussion started by: ptappeta
1 Replies

4. UNIX for Dummies Questions & Answers

What difference does * make here ? (ls command question)

Solaris 10 (korn shell) I use -d option with ls command , when I want to suppress contents of the subdirectories being listed when listing all the directories and files in a directory. This is what man page says about -d option in ls command. -d If an argument is a directory,... (3 Replies)
Discussion started by: kraljic
3 Replies

5. Shell Programming and Scripting

Calc Time Difference in Perl

Hello, I am having hard time calculating the time differnce in the below sequence. I tried nested for loops but I can't get to work. Algorithm: find time difference between the first AVAIL and the next event just before AVAIL. 0 05/17/2010 09:33 AVAIL <-- 1 1 05/17/2010 09:32 UM ... (2 Replies)
Discussion started by: bataf
2 Replies

6. Shell Programming and Scripting

Difference between 2 arrays in perl

Hi, I have 2 arrays: @a=qw(19190289 18381856 12780546 10626296 9337410 8850557 7740161 8101063); @b=qw(18309897 17612870 10626296 16871843 7740161 19947571 18062861); $len=@a; print "<br> length of array1: $len<br>"; $len1=@b; print "<br> length of array2: $len1<br>"; The output... (3 Replies)
Discussion started by: vanitham
3 Replies

7. Shell Programming and Scripting

Perl Time Difference

I having probelm in time difference output using Delta_YMDHMS, using below start date and enddate I get -30days. Any idea how to fix this issue. output : 0,1,-30, 0,0,0 Thanks, Bataf use POSIX qw/strftime/; use Date::Calc qw(Delta_YMDHMS); use Time::Local; $start_date =... (1 Reply)
Discussion started by: bataf
1 Replies

8. Shell Programming and Scripting

Perl difference between dates

Hi, Is there any way I can get the difference between two dates in terms of days? I have used this method so far, but I cant format it in terms of days. @a=&DateCalc($date1,$date2,0); The o/p that I am getting is sort of like this: +0:0:0:4:0:0:0 I just want to get 4 days as an o/p.... (1 Reply)
Discussion started by: King Nothing
1 Replies

9. Shell Programming and Scripting

Compute difference between 2 arrays perl

Hi, I have 2 arrays. I have to compute symmetric difference! @arr=qw(19205134 18630215 18453487 18416242 18338715 18227590 17698645); @arr1=qw(18227590 18053561 17698645 16966777); #The code which i used is this! @union = @isect = @diff = (); %union = %isect = (); %count =... (3 Replies)
Discussion started by: vanitham
3 Replies

10. Shell Programming and Scripting

Date Difference in PERL

Hi I have a file which has start time as my first line and End time in last line start time: 23 May 2008 03:30:33 End time : 23 May 2008 04:30:00 I have to extract these two lines and find the time of execution for the script in PERl. Any help appreciated Thanks (1 Reply)
Discussion started by: usshell
1 Replies
Login or Register to Ask a Question