Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 15th, 2012, 05:43 PM
LAD13 LAD13 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 1 LAD13 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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>

Reply With Quote
  #2  
Old November 16th, 2012, 01:03 AM
Laurent_R Laurent_R is online now
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 546 Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 5 Days 2 h 42 m 48 sec
Reputation Power: 406
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > The Ingredients and Instructions arent printing out.. Help Please

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap