while loop in perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting while loop in perl script
# 1  
Old 04-23-2010
Question while loop in perl script

Quote:
hey guys im writing my first perl script i have a question about looping in script...if i try to loop by testing numbers(1,2.3) it works however if i use characters it failsSmilie...here is the code please let me know what i am doing wrong
thanks guys!Smilie
Code:
#! /usr/bin/perl

$exp = "y";
while ($exp !="n") {
system "clear";         # clear the window

print "\nEnter the number of Widgets ordered: ";
$widgetcount = <STDIN>; #vairable to save the number of total widgerts ordered
chop $widgetcount;

print "\nEnter the number of Gidgets ordered: ";
$gidgetcount = <STDIN>; #vairable to save total number of gidget ordered
chop $gidgetcount;

print "\nEnter the number of Gadgets ordered: ";
$gadgetcount = <STDIN>; #variablet to save total number of gadget ordered
chop $gadgetcount;

print "\nEnter the number of Doodats ordered: ";
$doodatcount = <STDIN>; #vatiable to save total number of doodats ordered
chop $doodatcount;

$widgetsub = $widgetcount * 10.25; #subtotal of widgets
$widgetsub = sprintf("%0.2f", $widgetsub); #setprecesion two decimal places
print ("\nThe total number of Widgets ordered is: $widgetcount");
print ("\nThe total value of Widgets ordered is \$$widgetsub\n");

$gidgetsub = $gidgetcount * 5.75; #subtotal of gidgets
$gidgetsub = sprintf("%0.2f", $gidgetsub); #setprecesion two decimal places
print ("\nThe total number of Gidgets ordered is: $gidgetcount");
print ("\nThe total value of Gidgets ordered is \$$gidgetsub\n");

$gadgetsub = $gadgetcount * 11.33; #subtoal of gadgets
$gadgetsub = sprintf("%0.2f",$gadgetsub); #setprecesion two decimal places
print ("\nThe total number of Gadgets ordered is: $gadgetcount");
print ("\nThe total value of Gadgets ordered is \$$gadgetsub\n");

$doodatsub = $doodatcount * 44.75; #subtotal of doodats
$doodatsub =sprintf("%0.2f",$doodatsub); #setprecesion two decimal places
print ("\nThe total number of doodats ordered is: $doodatcount");
print ("\nThe total value of doodats ordered is \$$doodatsub\n");

#total of all subtotals without taxes and shipping charges
$total=($widgetsub + $gidgetsub + $gadgetsub + $doodatsub);
$total = sprintf("%0.2f", $total);

print ("\nTotal of all products subtotal is \$$total ");

$tax = $total*.10;      #total tax on total amount
$tax =sprintf("%0.2f", $tax);
print ("\nThe total of all products tax is \$$tax");

$shipping = $total*.085;         #total shipping charges
$shipping = sprintf("%0.2f", $shipping);
print ("\nThe total of all products shipping charges is \$$shipping");

$grandtotal = $total + $tax + $shipping;        #grandtotal
$grandtotal = sprintf("%0.2f", $grandtotal);

print("\nThe grandtotal is \$$grandtotal \n");


print "Please enter 0 to quit: ";
$exp = <STDIN>;
chop $ex;
}

# 2  
Old 04-23-2010
Try this,

Code:
while ($exp ne "n") {
system "clear";        ..............
..............
}

Try to read string comparison in Perl.
URL : String Comparison Operators
# 3  
Old 04-23-2010
Problem with Loop

Use "ne" instead of "!=" in the condition. "!=" works out only for numbers.

Code:
 
#!/usr/bin/perl
 
$exp = "y";
 
while ($exp ne "n") { 
 
system "clear"; # clear the window 
 
print "\nEnter the number of Widgets ordered: "; 
$widgetcount = <STDIN>; #vairable to save the number of total widgerts ordered 
chop $widgetcount; 
 
print "\nEnter the number of Gidgets ordered: "; 
$gidgetcount = <STDIN>; #vairable to save total number of gidget ordered 
chop $gidgetcount; 
 
print "\nEnter the number of Gadgets ordered: "; 
$gadgetcount = <STDIN>; #variablet to save total number of gadget ordered 
chop $gadgetcount; 
 
print "\nEnter the number of Doodats ordered: "; 
$doodatcount = <STDIN>; #vatiable to save total number of doodats ordered 
chop $doodatcount; 
 
$widgetsub = $widgetcount * 10.25; #subtotal of widgets 
$widgetsub = sprintf("%0.2f", $widgetsub); #setprecesion two decimal places 
print ("\nThe total number of Widgets ordered is: $widgetcount"); 
print ("\nThe total value of Widgets ordered is \$$widgetsub\n"); 
 
$gidgetsub = $gidgetcount * 5.75; #subtotal of gidgets 
$gidgetsub = sprintf("%0.2f", $gidgetsub); #setprecesion two decimal places 
print ("\nThe total number of Gidgets ordered is: $gidgetcount"); 
print ("\nThe total value of Gidgets ordered is \$$gidgetsub\n"); 
 
$gadgetsub = $gadgetcount * 11.33; #subtoal of gadgets 
$gadgetsub = sprintf("%0.2f",$gadgetsub); #setprecesion two decimal places 
print ("\nThe total number of Gadgets ordered is: $gadgetcount"); 
print ("\nThe total value of Gadgets ordered is \$$gadgetsub\n"); 
 
$doodatsub = $doodatcount * 44.75; #subtotal of doodats 
$doodatsub =sprintf("%0.2f",$doodatsub); #setprecesion two decimal places 
print ("\nThe total number of doodats ordered is: $doodatcount"); 
print ("\nThe total value of doodats ordered is \$$doodatsub\n"); 
 
#total of all subtotals without taxes and shipping charges 
$total=($widgetsub + $gidgetsub + $gadgetsub + $doodatsub);
$total = sprintf("%0.2f", $total); 
print ("\nTotal of all products subtotal is \$$total "); 
 
$tax = $total*.10; #total tax on total amount 
$tax =sprintf("%0.2f", $tax); 
print ("\nThe total of all products tax is \$$tax"); 
 
$shipping = $total*.085; #total shipping charges 
$shipping = sprintf("%0.2f", $shipping); 
print ("\nThe total of all products shipping charges is \$$shipping"); 
 
$grandtotal = $total + $tax + $shipping; #grandtotal 
$grandtotal = sprintf("%0.2f", $grandtotal); 
print("\nThe grandtotal is \$$grandtotal \n"); 
 
print "Do you want to Continue(y/n): "; 
$exp = <STDIN>;
chop $exp; 
$exp = lc($exp);
 
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open()) I would like to run another PERL-script from first one, not... (1 Reply)
Discussion started by: alex_5161
1 Replies

2. Shell Programming and Scripting

PERL script loop problem

I have written the below PERL script to reprocess messages from a failure queue. It basically browses all the messages in the failure queue to individual files in a directory and then scans those files to determine the originating queue. The script will then move each message in turn from the... (0 Replies)
Discussion started by: chris01010
0 Replies

3. Shell Programming and Scripting

Perl loop until

I have a script that needs to wait on another script to finish. I created a sub routine to check the file for the number 0 but my until statement keeps on going. I tried eq, == and =~ but same thing. my $CHECKING_FILE = 1; do { sleep(5); $CHECKING_FILE = check_file(); ... (2 Replies)
Discussion started by: numele
2 Replies

4. Shell Programming and Scripting

Calling an interactive perl script from within a while-read loop

Hi, I have a perl script that prompts for a user to enter a password before doing what it does. This works well if I call it directly from a bash script #!/bin/bash /path/to/perl/script $arg1 $arg2 But, when I try to enclose this within a while read loop, the perl script is called but... (1 Reply)
Discussion started by: prafulnama
1 Replies

5. Shell Programming and Scripting

Need help with perl script with a while loop reading file

Good morning, I appreciate any assistance that I can get from the monks out there. I am able to get this to work for me so that I can do a hostname lookup if I only specify one hostname in the script. What I want to do is have a file with hostnames and do lookups for each name in the file. Here is... (1 Reply)
Discussion started by: brianjb
1 Replies

6. Shell Programming and Scripting

until loop Perl

I am trying to print out a section of a file begining at the start and printng until a character is found. My code and input file are below. This code is printing out every line except for the line with the character which is not what I want the out put should be a file with numbers 1-4. ... (3 Replies)
Discussion started by: cold_Que
3 Replies

7. Shell Programming and Scripting

Perl - pass shell-vars into perl for input loop

I need to process a file line-by-line using some value from a shell variable Something like:perl -p -e 's/$shell_srch/$shell_replace/g' input.txt I can't make the '-s' work in the '-p' or '-n' input loop (or couldn't find a syntaxis.) I have searched and found... (4 Replies)
Discussion started by: alex_5161
4 Replies

8. UNIX for Dummies Questions & Answers

Foreach loop to run a perl script on multiple files

Hi, I have thousands of files in a directory that have the following 2 formats: 289620178.aln 289620179.aln 289620180.aln 289620183.aln 289620184.aln 289620185.aln 289620186.aln 289620187.aln 289620188.aln 289620189.aln 289620190.aln 289620192.aln.... and: alnCDS_1.fasta (1 Reply)
Discussion started by: greptastic
1 Replies

9. Shell Programming and Scripting

Creating loop for a script -Perl

Hi Guyz I designed a script that can compare 2 columns(values) of single file and gives the closest numbers to the first column by comparing the numbers in first column with second and it works in a single file. Now I'm trying to design a new script with 2 objectives for 2 files (not a single... (4 Replies)
Discussion started by: repinementer
4 Replies

10. Shell Programming and Scripting

help with perl while loop

Can anyone tell me why this program won't kick out when the time gets beyond time in the loop? sub showtime { local($format,$military)=@_; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); if ((! $military) &amp;&amp; ($hour &gt; 12)) {$hour-=12;} ... (2 Replies)
Discussion started by: methos
2 Replies
Login or Register to Ask a Question