![]() |
|
|
| 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";
} | |
| |
| |
|
|
| Subject:
Re: Perl: Perl Cookbook question for example
Answered By: googleexpert-ga on 04 Mar 2003 12:37 PST Rated: ![]() |
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:
cool that works googexpert. Great response |
|
| 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. |
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 Home - Answers FAQ - Terms of Service - Privacy Policy |