DOC PREVIEW
UCSD CSE 182 - Scripting Language Basics

This preview shows page 1-2-3-4-5-6 out of 19 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Scripting Language BasicsCSE/BENG/BIMM 182September 28, 2009Scripting Languages●Examples: Perl (Documentation: http://www.perl.org/docs.html) and Python (Documentation: http://docs.python.org/)●Advantages:●Easy to code●Tons of prewritten scripts/modules developed for the bioinformatics community (e.g. BioPerl & BioPython)●Not required for this class, but they are convenient for many applications discussed●The best way to learn any language is to write some small scripts in itPerl Basics: Command Line Input●To invoke a Perl script from command line, type the following:perl PATH/FILE_NAME [ARG1 ARG2 …]●ARGs are optional parameters which are fed as an array of inputs into the program (more later)Perl Basics: Syntax and Headers●All variables should start with identifiers●$ for scalars●@ for arrays●% for hashes●All lines should end with a semicolon (;)●A script should begin with:#!PATHwhere PATH is the location of the Perl interpreter files– This is usually /usr/bin/perl●To use premade modules, type use MODULE_NAMEPerl Basics: Variables●Variables can be assigned values using the assignment operator (=)●$i = 3;●$j = 4;●$str = “This is a Perl string”;●Standard operations can be performed on integers●$k = $i + $j; #$k = 7Perl Basics: Strings●Single quotes mean string is taken literally, double quotes mean string is interpreted●Useful functions●Size–length($str)●Substring–substr($str, 3, 5)#takes the substring starting at index 3 with length 5●Concatenation–$str = "con"."cat".”enate”; #$str = “concatenate”●Splitting a string up–my @line = split(/a/, $str);#@line is an array containing 3 parts: “conc”, “ten”, and “te”–Any regular expression can fit in between the slashes in the split functionPerl Basics: Arrays●Can be instantiated during use●Array labeled with '@', while elements in the array labeled with '$' (e.g. $row_line[$0] is the 1st element in @row_line, which is “conc”)●Maximum Index: $#array●Size: scalar(@array)Perl Basics: Conditionals and Loops●Conditional:if(BOOLEAN){STATEMENTS;}elsif(BOOLEAN){STATEMENTS;}else{STATEMENTS;}●For Loops:for($i = 1; $i <= $size; $i++){print “$i\n”;}ORfor $i (1 .. $size){print “$i\n”;}Other types of loops occur too (e.g. while)Perl Basics: File I/O●To open a stream:open(F0, STRING_LOCATION);●Start the STRING_LOCATION with '>' for write/overwrite, '>>' for write/append, or '<' for read (default – if nothing is placed before the file name)●To read all at once into an array:my(@lines) = <F0>;●To read line-by-line:●while(<F0>){$line = $_;chomp($line);#Process as requiredprint “$line\n”;}●To write to a file, simply insert the stream between print and the text(e.g. print F1 “$line\n”; #F1 is a write stream opened similarly to above)Sample ScriptCalled from command line with:perl ./PerlTest.pl TestArraySizes.txtPython Basics: Syntax and Headers●Spacing is important – off by one can throw off the whole program●Should begin with:#!<PATH>where <PATH> is the location of the Python interpreter files (usually /usr/bin/env python)●To use premade modules, type import <MODULE_NAME>Python Basics: Command Line Input●To invoke a Python script from command line, type the following:python <PATH/FILE_NAME> [ARG1 ARG2 …]●ARGs are optional parameters which are fed as an array of inputs into the program (more later)Python Basics: Variables●Variables can be assigned values using the assignment operator (=)●i = 3●j = k = 4●str = “This is a Python string”●Standard operations can be performed on integers●l = i + j; #l = 7Python Basics: Strings●Immutable (can't be changed directly, but can be modified and stored)●Useful functions●Size–len(str)●Substring–str[3:7] #takes the substring starting at index 3 with length 5●Concatenation–str = "con" + "cat" + ”enate” #str = “concatenate”●Splitting a string up–line = str.split(“a”);#line is an array containing 3 parts: “conc”, “ten”, and “te”Python Basics: Arrays●Should be instantiated prior to usage●Access: array[i] is the ith element in array●Size: len(array)Python Basics: Conditionals and Loops●Conditional:if(BOOLEAN):STATEMENTSelif(BOOLEAN):STATEMENTSelse:STATEMENTS●For Loops:for i in range(1, size):print '%d' % i●Other types of loops occur too (e.g. while)Python Basics: File I/O●To open a stream:F0 = open(STRING_LOCATION)●Have a second parameter with 'w' for write/overwrite, 'a' for write/append, or 'r' for read (default – if nothing is placed before the file name)●To read all at once into an array:lines = F0.readlines()●To read line-by-line:●line = F0.readline()●for line in F0:#Process as requiredprint line●To write to a file,print >>F1 “line” #F1 is a write stream opened similarly to aboveOR●F1.write(“line”)Sample ScriptCalled from command line with:python ./PythonTest.pl


View Full Document

UCSD CSE 182 - Scripting Language Basics

Download Scripting Language Basics
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Scripting Language Basics and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Scripting Language Basics 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?