Google Answers Logo
View Question
 
Q: Perl: Perl Cookbook question for example ( Answered 5 out of 5 stars,   3 Comments )
Question  
Subject: Perl: Perl Cookbook question for example
Category: Computers > Programming
Asked by: derekwtp-ga
List Price: $4.00
Posted: 04 Mar 2003 06:14 PST
Expires: 03 Apr 2003 06:14 PST
Question ID: 170438
Looking on the Perl cookbook on page 152 there is a script that looks
like the one below. I am in need of something similar, however when I
try to even use the example given it errors on the while (<>). I have
4 perl boooks and I still can find out what is wrong w/ the syntax.
Hopefully this is a quickie. Anyone??

 
#!/usr/bin/perl -w


%father = (
Cain  => "Adam",
Abel  => "Adam",
Seth  => "Adam",
Enoch => "Cain",
Irad  => "Enoch",
Mahujael  => "Irad",
Methusael => "Mehujael",
Lamech    => "Methusael",
Jabal => "Lamech",
Jubal => "Lamech",
Tubalcain => "Lamech",
Enos  => "Seth" );


 while ( ($k,$v) = each %father) {
push( @{$children{$v} }, $k );
}

#$_ = ',';
while (<>) {
chomp;
if ($children{$_}) {
@children=@{$children{$_}};
} else {
@children = "nobody";
}
print "$_ begat @children.\n";
}

Request for Question Clarification by googleexpert-ga on 04 Mar 2003 07:31 PST
I have a problem with the while(<>) statement, I think that's used for
filehandling.  anyway, I modified two lines of code and received the
following output:

Lamech begat Tubalcain Jabal Jubal.
Methusael begat Lamech.
Seth begat Enos.
Adam begat Seth Cain Abel.
Cain begat Enoch.
Enoch begat Irad.
Irad begat Mahujael.
Mehujael begat Methusael.

I wanted to know if the output is what you are looking for.

Clarification of Question by derekwtp-ga on 04 Mar 2003 11:59 PST
I looked up while <> and I found that it is used for filehandleing,
but a) why would it be used in this example and b) how can you
traverse the hash using it?

Clarification of Question by derekwtp-ga on 04 Mar 2003 12:01 PST
yeah that is the output I was looking for. What did you modify?
Answer  
Subject: Re: Perl: Perl Cookbook question for example
Answered By: googleexpert-ga on 04 Mar 2003 12:37 PST
Rated:5 out of 5 stars
 
Hi derekwtp, 
I have commented what I changed in the original code:

#!/usr/bin/perl -w 
   
   
  %father = ( 
  Cain  => "Adam", 
  Abel  => "Adam", 
  Seth  => "Adam", 
  Enoch => "Cain", 
  Irad  => "Enoch", 
  Mahujael  => "Irad", 
  Methusael => "Mehujael", 
  Lamech    => "Methusael", 
  Jabal => "Lamech", 
  Jubal => "Lamech", 
  Tubalcain => "Lamech", 
  Enos  => "Seth" ); 
   
   
   while ( ($k,$v) = each %father) { 
  push( @{$children{$v} }, $k ); 

#added -->
push(@justfathers,$k);
#<--
  } 
   
  #$_ = ','; 
  #while (<>) {   

#added -->
foreach(@justfathers) {
#<--

  chomp; 
  if ($children{$_}) { 
  @children=@{$children{$_}}; 
  } else { 
  @children = "nobody"; 
  } 
  print "$_ begat @children.\n"; 
  }
derekwtp-ga rated this answer:5 out of 5 stars
cool that works googexpert. Great response

Comments  
Subject: Re: Perl: Perl Cookbook question for example
From: yatha-ga on 04 Mar 2003 10:12 PST
 
Hi,

While(<>) syntax means it is expecting input from STDIN. When you run
the script, it stops and waits, you enter something and then it moves
further.
It is like @ARGS. 
This is the perl way of expecting input form STDIN.
Subject: Re: Perl: Perl Cookbook question for example
From: derekwtp-ga on 04 Mar 2003 12:03 PST
 
yeah, but how to do traverse the hash using it?
Subject: Re: Perl: Perl Cookbook question for example
From: googleexpert-ga on 04 Mar 2003 13:40 PST
 
hi derekwtp,
I appreciate your rating.
thanks.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy