1 / 13

File Handle and conditional

File Handle and conditional. Lecture 2. File Handling. The Files associated with Perl are often text files: e.g. text1.txt Files need to be “opened for reading; writing or appending data to the file and finally close it”. The default type of “opening” is to open the file for reading

talib
Download Presentation

File Handle and conditional

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. File Handle and conditional Lecture 2

  2. File Handling • The Files associated with Perl are often text files: e.g. text1.txt • Files need to be “opened for reading; writing or appending data to the file and finally close it”. • The default type of “opening” is to open the file for reading • open MYFILE, ‘text1.txt’ or die “could not open file aborting…\n”; • MYFILE is called a file pointer (handler) and essentially is an alias for the files location • ‘text1.txt’ refers to the file : note the name is case sensitive • The above example requires the file to be in the same folder/directory as the perl program if it is not you have to insert the path (location) as well: e.g. c:\perltest\data.txt’

  3. File Handling: Reading • A file can also be opened for writing (to allow you to write data to it). • open MYFILE, ‘>text2.txt’ or die “could not open file aborting…\n”(open file for writing) • Note the > symbol before the file name tells the program to open it for writing and of course reading. • The file does not have to exist in order to use this function • However, every time you open a file for writing it over-writes the previous version • A file can be open for appending (add data to existing file) • open MYFILE, ‘>>text3.txt’ or die “could not open file aborting…\n” • It this case the file does not have to exist (an empty one is created) and if it does exist the new information is added to the end of the file (appended)

  4. File Handling: Write and Append • read data from a file • $line = <MYFILE > ; #read one line from file and assigns to variable $line • Chomp $line; • Write/append data to a file • print MYFILE “Do you like computers….\n”; # write out to file • Print MYFILE “$line\n”; # write the contents of variable • Close the file : • close MYFILE; • read_write_append_file.pl

  5. Conditional Statements: if • The if statement is the most basic conditional statement: if the expression is true you execute the line(s) between the brackets {….}; if the statements is false the program executes the line after the if {….} statement. • print “Enter your age: ”; • $age = <>; • if ($age <= 5) #the conditional expression • { • print “You are probable too young to be using a computer.\n”; • } • print “continue with rest of program…..”

  6. Conditional expression: Operators • The following are used for numeric operators: • == Equality $a == $b • != Not equal $a != $b • < Less than $a < $b • > Greater than $a > $b • <= Less than or equal to $a <= $b • >= Greater than or equal to $a >= $b

  7. String conditional operator • eq Equality $a eq $b • ne Not equal $a ne $b • lt Less than $a lt $b • gt Greater than $a gt $b • le Less than or equal to $a le $b • ge Greater than or equal to $a ge $b • Less than or greater than in string operators is where you would sort alphabetically.

  8. Boolean operators • If the condition uses more 2 conditional expression they are connected via a Boolean operator: there are 3: • Logical AND if ($x ==$y) && (&x == ‘Y’) • logical OR if ($x = $y) || (&x = ‘Y’) • Logical NOT if ( $ = !$b)

  9. Conditional Statements: if{ }else{ } • This is used if there are two possible outcomes: if the conditional statement (condition) is true perform statement after if it is false the statement after the else • #!/usr/bin/perl • use strict; • use warnings; • print “Enter your age: ”; #prompt user to input age • my $age = <>; • if ($age <= 5) # the conditional statement • { • print “You should not be allowed use computers ”; • } • else • { • print “You can use a computer\n”; • } • Print “ continue with the rest of the program…..”;

  10. Conditional statements: if elseif else • If , elsif , else [nested if statements]: Note it is elsif and not elseif • #!/usr/bin/perl • Use strict; • Use warnings; • print “Enter your age: ”; • $age = <>; • if ($age <= 0) { • print “You do not exist so you can not use a computer.\n”; • } • elsif ($age <= 5) • { • print “you are a little to young to use a computer \n”; • } else • { • print “You are not to young to use a computer\n”; • } • Else_If.pl

  11. Conditional statements: Boolean operators • The If , elsif , else [nested if statements] can be written as a set of if statements with 2 conditional expression and a booolean operator. • #!/usr/bin/perl • use strict; • use warnings • print “Enter your age: ”; • my $age = <>; • if ($age <= 0) • { • print “You do not exist so you can not use a computer.\n”; • } • if ($age >0) && ($age <=5) # the use of a boolean operator • { • print “you are a little to young to use a computer \n”; • } • If ($age >5) { • print “Your are not to young to use a computer\n”; • }

  12. Exercise 1: file handling • Using this fasta file called: “text1.txt” • Create and test the following: • Part 1 • Open the file for reading and print the contents to the screen • Part 2 • Open a file called ‘text2.txt’ for writing: write your name and address to the file • Confirms it works by writing a program to open and read the file • Part 3 • Open the file ‘test2.txt’ for appending and write the any data you want to the file; open the file to confirm it has worked.

  13. Exercise 2: Tryptophan operon program • Write a program that can simulate the • Following “expression” rule: • (tryptophan is present then gene is not expressed • tryptophan is not present then gene is expression) • Ask user if there is or there is not tryptophan • Validate the input: ensure only (y or n) are input by the user • Use a conditional statement to determine if the geneis expressedor not expressed.

More Related