Perl Reference Code

Discussion in 'Software' started by Wookie, Sep 27, 2004.

  1. Wookie

    Wookie Sergeant Major

    Well If you saw my C Reference code you prob wondering why I started Perl


    Cause I already know C and its syntax, I convinced my teacher to allow me to learn a language of my choice and I couldn't think of anything but perl since I don't know many scripting languages.

    I must say I was completely in awe at how simple it was to write this program compared to C.

    I love declaring Variables on the go and not having to worry about what type of variable they are.

    OK heres my first Perl program that calculates the BMI for a person

    For a compiler I used activestate activeperl a free download with lots of documentation
    http://www.activestate.com/



    Heres the code

    Code:
    #Assignment 3 for Intro to C,  but I am doing Perl
    #By WookMaster
    
    print "Please Input Your Height in Feet: "; 
    chop ($height = <STDIN>);       #Get Height and store in variable
    print "Please Enter the remaining inches: ";
    chop ($inches = <STDIN>);
    print "Please Enter your weight in pounds: ";
    chop ($weight = <STDIN>);        #Get Weight and store in a variable
    
    # Here comes the math for the BMI,  formula = Weight DIV Height DIV Height * 703
    #This converts the height in feet to inches for the math
    $totalh = ($height * 12) + $inches;
    $BMI = (($weight / $totalh) / $totalh) * 703;  # The math for calculating BMI
    
    print "Your BMI is: ";
    printf ("%.0f", $BMI); #Used printf for the rounding function to round to the nearest whole number
    
     
  2. Wookie

    Wookie Sergeant Major

    and FYI I will be keeping all this code in a sticky thread Here
     
  3. Robster12

    Robster12 The Horse Whisperer

    So, you are "switching gears" and going with the PERL for the class?

    Everybody says that PERL is such a good scripting language. You know,
    I'm trying to learn BASH right now. As a matter of fact, I have just written my first script that will "parse" data from a website.

    It is what it is, a newbie's attempt.
    If you want to check it out, it is attached to this post:

    http://forums.majorgeeks.com/showpost.php?p=443043&postcount=72

    Do you ever find yourself getting confused between languages, Wookie?

    You know, like you use a syntax of one language in another one, by accident?

    I wonder if that is a common problem in programming.
     
  4. Wookie

    Wookie Sergeant Major

    Yeah LOL. Sometimes ill throw in a ; in a vb program or start declaring a Dim in C.

    Not to often but it has happend lol. Usually when I switch in the same day between languages.

    Ill check out your bash stuff, thats another one on my list.
     
  5. Wookie

    Wookie Sergeant Major

    Assingment 4

    Code:
    #Assignment 4 by WookMaster
    
    $redo = 1; #use this to decide if need to ask again
    
    do { 
    	$yesno = "n";
    	print "Please Input Your Height in Feet: ";
    	$height = <STDIN>;       #Get Height and store in variable
    	print "Please Enter the remaining inches: ";
    	$inches = <STDIN>;
    
    	$totalh = ($height * 12) + $inches; # Calculates the height in inches
    
    	if ($totalh < 58 or $totalh > 80)
    		{
    		print "If the height is less than 4 ft 10 In or greater than 6 Ft 8 In it may not be meaningful.\n";
    		print "Would you like to Re Enter the value? enter y for yes n for no\nEnter Value: ";
    		$yesno = <STDIN>;
    		print $yesno;
    		if ($yesno =~ /y/ or $yesno =~ /Y/){  #If yes do nothing leave redo at 1
    		$redo = 1;	}
    		else
    		{ 
    		$redo = 2; 
    		}    # anything else we break the loop
    	}
    	else { $redo = 2; } # setting this to 2 breaks the loop conditional
    
    }while ($redo eq 1);    # loop goes while redo = 1
    
    
    $redo = 1;
    
    
    do {
    $yesno = /n/;
    print "Please Enter your weight in pounds: ";
    $weight = <STDIN>;        #Get Weight and store in a variable
      # Initilize the Question as no
    if ($weight < 75 or $weight > 250) {
    	
    	print "\nHaving a weight under 75 or above 250 may be meaningless, would you like to reenter?\n";
    	print "Enter y for yes or n for no\nEnter Value: ";
    	$yesno = <STDIN>;
    
    	if ($yesno =~ /Y/ or $yesno =~ /y/){
    		$redo = 1;
    	}
    	else    # If its not yes we want to break the loop
    	{
    		$redo = 2;
    	}
    
    }
    else { $redo = 2; }  # If its not  < 75 or > 250 we want to break the loop
    
    } while ($redo eq 1);   # loops until redo != 1
    
    # Here comes the math for the BMI,  formula = Weight DIV Height DIV Height * 703
    #This converts the height in feet to inches for the math
    
    $BMI = ((($weight / $totalh) / $totalh) * 703) + .5;  # The math for calculating BMI
    
    
    print "Your BMI is: ";
    printf ("%.0f", $BMI); #Used printf for the rounding function to round to the nearest whole number
    if ($BMI > 25 or $BMI < 18){ # Check to see if BMI meets these requirements for the message Below
    print "\nPlease Note that having a BMI under 18 is Considered Underweight while over 25 is considered overweight";
    }
     
  6. eric06

    eric06 Sergeant Major

    is that perl? thats looks like the do while loops we are doing in my java class.

    eric
     
  7. QuickSilver

    QuickSilver Corporal

    you'd be amazed that a lot of languages share a lot in common. Quite often the if/else/loop/case etc constructs are extremely similar and are applied the same across multiple languages.
    That said there can be huge differences between languages too. ;-)

    But that is definitely perl up there - also Java isnt quite as free with the variable usage as perl is. You need an integer in perl? Use a scalar. You need a string in perl? Use a scalar. Making conversions between different data types is really quite easy... :)

    And lets not forget the power of the mighty regular expression hehe
     
  8. Wookie

    Wookie Sergeant Major

    ive fallen in love with perl because the variables. ITs great. C takes so much more work to do string manipulation and variable stuff.

    Never done Java tho.
     
  9. QuickSilver

    QuickSilver Corporal

    I enjoy Java for creating Windows apps - designing GUI's is fun and easy and then interfacing that to some more powerful functionality is also easy and intuitive once you get used to the fact that Java is PURE Object Oriented. That was the hardest thing for me to get my head around using Java when I first started learning.
    But its well documented and easy to find out what you want to do.

    But I've come to use Linux and UNIX based OS's much more and I love Perl for them. So easy to use, so easy to implement. String manipulation is so straight forwards and regular expressions just turn massive segments of C code into a single line. Amazing!

    Every language has its place. Perl is definitely one of my favourites at the moment though :D
     
  10. Wookie

    Wookie Sergeant Major

    Code:
    #Assignment 6 in Perl By WookMaster
    [url]http://www.techdefine.com[/url]
    # Write your program to pick a random number between 1 and 10. Use a while loop to allow the user to guess the number until they guess correctly.
    # Tell the user when their guess is correct or incorrect.
    # For extra credit give the user feedback hints about whether their guess is too high or too low.
    
    #Begin Code
    
    $somenum=int rand(9)+1 ; #Generates a Random number between 1 and 10  I used +1 so that if its between 0 and 1 it will make it over 1 as the requirements ask
    #If it reaches 9 it adds one to make it 10
    
    $answer = 100; #initializes the answer to a value that will not be and answer
    print "Please Enter your guess: ";
    $answer = <stdin>;
    
    #Initiates a while loop that continues until the correct answer is guessed
    
    while ($answer != $somenum){
    	#The next two if statements check to see if the answer is lower or higher than the number and give a little hint
    	if ($answer < $somenum){
    		print "\nIncorrect guess,  maybe try higher?";
    	}
    	if ($answer > $somenum){
    		print "\nIncorrect guess,  maybe try lower?";
    	}
    	#User input for the answer
    	$answer = <stdin>;
    }
    #The user is correct the loop has to break and we print that they are correct
    print "\nYou are Correct!";
    
     
  11. Wookie

    Wookie Sergeant Major

    Code:
    #Assignment 7 by WookMaster
    
    $hourcounter = 20;  #start this at 20 because we know at 15 inches and 6 gallons it takes 20 hours to burn the fuel
    print "Hours Left     Inches Remaining\n"; # Prints the header
    print " _______\n"; # Prints the top of the ruler
    
    #The For loop goes through each hour subtracts .3 gallons
    for ($gallons=6; $gallons>0 && $hourcounter > 0; $gallons = $gallons - 0.3)
    {
    $inchesremain = $gallons * 2.50; # calculates remaining inches
    print "|  $hourcounter  ";
    
    #this fixes a spacing issue when the hours goes down to one digit
    if($hourcounter < 10){
     print " ";
    }
    print "|      ";
    printf("%0.3f",$inchesremain);
    print "\n";
    $hourcounter--;
    }
    print " _______"; # Finishes the bottom of the ruler
    
     
  12. Wookie

    Wookie Sergeant Major

    Ive gotten into php and I have to say I love php, its much like perl but even easier! if anyone here likes perl definately check out php.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds