The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Perl Programming
|
The Ingredients and Instructions arent printing out.. Help Please
Discuss The Ingredients and Instructions arent printing out.. Help Please in the Perl Programming forum on Dev Shed. The Ingredients and Instructions arent printing out.. Help Please Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 15th, 2012, 05:43 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 1
Time spent in forums: 19 m 44 sec
Reputation Power: 0
|
|
|
The Ingredients and Instructions arent printing out.. Help Please
I am supposed to create a server and client that talk to each other and take in information from a recipe text file. I can get the names of the recipes in the file to print but not the instructions or ingredients. Any help would be greatly appreciated.
#!/usr/bin/perl
#Luke Davis
#Server
use IO::Socket;
#Get Port Number
$portnumber = 1313;
$file = $ARGV[0];
#Connect With Server
$hostname=`hostname`;
$local = IO::Socket::INET->new(
Proto => 'tcp',
LocalPort => $portnumber,
Reuse => 1,
Listen => 1
) or die "$!";
#Listen
$local->listen();
$local->autoflush(1);
#Send Response
print "Starting Up... Waiting For $hostname At Port Number: $portnumber\n";
my $address;
#Get Request And Display Connections
while ($address = $local->accept() ) {
print "Connected From Host: ", $address->peerhost();
print " On Port: ", $address->peerport(), "\n";
#Result Variable
my $result;
while(<$address>)
{
#Get Input
chomp $_;
$recipenames = " ";
$choice = $_;
#Declare Arrays
@recipenames = ();
@ingredients = ();
@instructions = ();
#Counting Variable
$count = 0;
#Open File Selected From Server
open(FILE,$file);
#Seperates Recipe Name, Ingredients, and Instructions And Places Them Into The Corresponding Array
while(<FILE>)
{
next if m/^#/; # Comment Line
if ( m/^<Recipe/i )
{
#If No Name Is Entered Display Error
if (not m/name/i)
{
print "ERROR, Incorrect Recipe\n";
exit(1);
}
#Read Recipe Name
$recipeline = $_;
$recipeline =~ s/^<recipe.*name=['"]//gi;
$recipeline =~ s/['"] *>$//gi;
push(@recipenames, $recipeline);
}
#If Line Is An Ingredient, Place In Corresponding Array
if ( m/^<ingredients>$/i )
{
push(@ingredients, "");
while (<FILE>)
{
last if (m/^<\/ingredients>$/i);
$ingredients[$#ingredients] .= $_;
}
}
#If Line Is An Instruction, Place In Corresponding Array
if ( m/^<instructions>$/i )
{
push(@instructions, "");
while (<FILE>)
{
last if m/^<\/instructions>$/i;
$instructions[$#instructions] .= $_;
}
}
}
close(FILE);
#Sends Back The Chosen Recipe
if($choice eq "Recipes()")
{
#Print Each Recipe Name In The Array
foreach(@recipenames)
{
$chosenrecipename .= $_;
}
#Print Recipe Name
print $address "$chosenrecipename";
}
#Sends Back The Chosen Recipes Ingredients
elsif($choice eq "GrabIngandIns()")
{
($option,$entered)=split(/\|/, $choice);
print $entered;
$entered = $entered - 1;
$outgoing = "Ingredients: \n".@ingredients[$entered]."\nInstructions: \n".@instructions[$entered];
print $outgoing;
print @ingredients[$entered];
print $address "$outgoing";
}
last;
}
chomp;
#Reset
reset('r');
$recipenames = " ";
#Declare Arrays
@recipenames = ();
@ingredients = ();
@instructions = ();
#Close Connection
print "Closing Connection\n";
close $address;
#Wait For Next Recipe Request
print "Ready For Next Recipe Request...\n";
}
#!/usr/bin/perl
#Luke Davis
#Client
use IO::Socket;
#Get Input From User
$clienthost = $ARGV[0];
chomp($clienthost);
$portnumber = 1313;
#Connects To The Address and Port of the Server
$remote = IO::Socket::INET->new(
Proto => 'tcp',
PeerAddr=> $clienthost,
PeerPort=> $portnumber,
Reuse => 1,
) or die "$!";
#Displays Which Port The User Is Connected To
print "Successfully Connected To ", $remote->peerhost," On Port Number: ", $remote->peerport, "\n";
#Get The Recipes From The Server
print $remote "Recipes()\n";
$i=0;
#Declare Array 1 And Get Contents From The Server
my @serve = ();
@serve = <$remote>;
#Display The Recipes In The Server
print "Recipe Names in Server:\n";
#Declare Number To Get All Items From Server
$num=1;
#Loop To Display All Items In Server
foreach $item (@serve)
{
print $num.") ".$item;
$num++;
}
print "Enter 'Quit' or 'quit' To Exit Program\n";
#Decrement Counter
$num = $num-1;
#Close
close $remote;
#Get Input From User
$clienthost = $ARGV[0];
chomp($clienthost);
$portnumber = 1313;
#Connect To The Address And Port Of The Server
$remote = IO::Socket::INET->new(
Proto => 'tcp',
PeerAddr=> $clienthost,
PeerPort=> $portnumber,
Reuse => 1,
) or die "$!";
#Request User Input
print "Enter Number Of Recipe You Would Like: \n";
while(<STDIN>)
{
#Recieve Users Choice
$option = $_;
chomp($option);
#'Quit' Or 'quit' Is Entered If The User Wants To Quit
if (( $option eq 'quit') || ($option eq 'Quit' ))
{
#Exit Program
exit 1;
last;
}
#Makes Sure The User Entered A Correct Recipe Number
elsif (($option >= 1 ) && ( $option <= $num ))
{
#Get Recipe From Server
print"HERE";
print $remote "GrabIngandIns()".$option."\n";
last;
}
#The User Entered An Invalid Recipe Option
else
{
#Let User Know Their Input Was Incorrect
print "ERROR, Not An Option. Please Try Again\n";
}
}
#Declare Array 2 And Get Contents From The Server
my @secondserve = ();
@secondserve = <$remote>;
#Print Each Item In The Array
foreach $item1 (@secondserve)
{
print $item1;
}
#End Client
print "End of Client\n";
#Close
close $remote;
Text File:
## Example Recipe File ###
# Comment lines start with #
<Recipe name='Mexican Layered Dip'>
<ingredients>
1 bag tortilla chips
1 lg. can refried beans
1 (10-oz.) jar taco sauce
1 lb. hamburger
1 pkt. taco seasoning
6 cups Mexican shreeded cheese
</ingredients>
<instructions>
Brown hamburger, drain. Stir in taco seasoning. Set aside. In medium bowl, combine refried beans and taco sauce. Spread small amount of bean mixture in bottom of a 13x9-inch baking dish. Layer hamburger then cheese. Repeat with bean mixture, hamburger and cheese. Bake at 350 degrees until hot, about 30 to 40 minutes. Serve with tortilla chips. Can also top with black olives and sour cream, if desired.
</instructions>
</Recipe>
<Recipe name='Guacamole'>
<ingredients>
4 fresh, ripe avocados
2 cloves garlic, minced
Juice of 1/2 lime
Juice of 1/2 lemon
1 tomato, diced
Salt and pepper to taste
</ingredients>
<instructions>
Smash peeled and seeded avocadoes with fork or potato-masher. Add remaining ingredients and smash until slightly smooth.
</instructions>
</Recipe>
<Recipe name='Holiday Dipped Chocolate Chip Cookies'>
<ingredients>
Your favorite chocolate chip Toll House recipe or 1 (18-oz.) pkg refrigerated chocolate chip cookies
1 cup semi-sweet chocolate chips
1 tbs shortening
</ingredients>
<instructions>
Heat oven to 350 degrees. Bake cookies as directed on package. Cool completely. Melt chocolate chips and shortening over very low heat or at 50% power in the microwave. Stir well. Dip half of each cookie into melted chocolate. Sprinkle with decorations before chocolate hardens. Place on wax paper to coll. Maked 3 dozen cookies.
</instructions>
</Recipe>
|

November 16th, 2012, 01:03 AM
|
|
|
|
please add the appropriate code tags to your source code, so that code formatting (identation) is preserved. Such a long piece of code without any identation is hardly readable.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|