Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.

Slides:



Advertisements
Similar presentations
Arrays A list is an ordered collection of scalars. An array is a variable that holds a list. Arrays have a minimum size of 0 and a very large maximum size.
Advertisements

Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
PL/SQL.
Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
Scalar Variables Start the file with: #! /usr/bin/perl –w No spaces or newlines before the the #! “#!” is sometimes called a “shebang”. It is a signal.
Program Design and Development
Perl Lecture #1 Scripting Languages Fall Perl Practical Extraction and Report Language -created by Larry Wall -- mid – 1980’s –needed a quick language.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Prog.Perl Most operators have numeric and string version –remember Perl will convert variable.
CS 898N – Advanced World Wide Web Technologies Lecture 7: PERL Chin-Chih Chang
Perl File I/O and Arrays. File I/O Perl allows to open a file to read, write, or append As well as pipe input or output to another program. —We get to.
Introduction to C Programming
2.1 Lists and Arrays Summary of 1 st lesson Single quoted and double quoted strings Backslash ( \ ) – the escape character: \t \n Operators:
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Prof. Alfred J Bird, Ph.D., NBCT Office – Wheatly 2nd floor Office Hours – MW 3:00PM to 4:00PM.
4. Python - Basic Operators
Practical Extraction & Report Language PERL Joseph Beltran.
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormack 3rd floor 607.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Control Structures. Important Semantic Difference In all of these loops we are going to discuss, the braces are ALWAYS REQUIRED. Even if your loop/block.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students – * Office – Wheatly.
Perl By Gabe and Ted. History Perl was created by Larry Wall while working at NASA’s Jet Propulsion Labs. Larry Wall also created patch which is in widespread.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Prof. Alfred J Bird, Ph.D., NBCT -bird.wikispaces.umb.edu/ Office – McCormick 3rd floor.
Books. Perl Perl (Practical Extraction and Report Language) by Larry Wall Perl 1.0 was released to usenet's alt.comp.sources in 1987 Perl 5 was released.
Perl Language Yize Chen CS354. History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes.
Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
CS 105 Perl: Data Types Nathan Clement 15 May 2014.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students.
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
CSI605 perl. Perl Facts Perl = Pathologically Eclectic Rubbish Lister Perl is highly portable across many different platforms and operating systems Perl.
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.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Camel –perldoc perlop Many operators have numeric and string version –remember Perl will.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 8.
IT441 Network Services Administration
IT441 Network Services Administration
Chapter 5 - Control Structures: Part 2
An Introduction to Perl – Part I
Control Structures: if Conditional
Control Structures: for & while Loops
Programming Perls* Objective: To introduce students to the perl language. Perl is a language for getting your job done. Making Easy Things Easy & Hard.
PHP.
The Selection Structure
Presentation transcript:

Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and Thursday 4:00PM to 5:15PM

 What is a Scalar?  Types of Scalars:  Numbers  Strings  Logical or Boolean  Automatic conversion between types

 Decimal  Binary  Octal  Hexadecimal

 What is a constant?  Why do we use a constant?  Write a simple program to print out a few constants.  Single quoted vs. Double quoted strings

 What is a variable?  How do we name variables?  Starts with $  Next either letter or _  Rest can be letters or numbers  You should develop a pattern so you are consistent within your programs.  Make the name mean something!!!

 + Addition  - Subtraction  * Multiplication  / Division  ** Exponentiation  % Modulo (Remainder)

 && And  || Or  ! Not

 == Equality  < Less Than  <= Less Than or Equal To  > Greater Than  >= Greater Than or Equal To

. Concatenation  x Repetition  ord()ASCII Value of a character  lt Less Than  gt Greater Than  eq Equal To

 $x gt $y  $x lt $y  $x ge $y  $x le $y  $x eq $y  $x ne $y

 $a++ Autoincrement  $b-- Autodecrement  Scoping: Blocks of code limit the range of a variables definition $numDef=25; print $numDef; { my $numDef=1; print $numDef;} print $numDef;

 exit (0);  die $string;

 A very helpful construct is the increment/decrement statement  $i++ is equivalent to $i = $i+1  $j = $i++  $j = ++$i  $j = $i - -  $j = - - $i

 $x and &y  $x && $y  $x or $y  $x || $y  not $x  !$x

 What is a control statement?  Types of control statements:  if  while  for

 if  if ( condition ) { action }  if else  if (condition ) { action }  else {another action }  if elsif else  if ( condition ) { action }  elsif (another condition ) { another action }  …  else { a further action }

 unless statement unless (condition) {action};  Reversed order print “Hello Al\n” if($inputName = “Al”); die “Can’t divide by zero\n:” if ($num4 == 0);

 while loops  while ( condition ) { action }  $i=1;  while ($i<=5) {  print $i, “\n”;  $i++;  }  until loops  Same form but opposite action of the while loop

 $_ is the default variable for many functions  while ( $line = ) {  chomp ($line);  …  }  while ( ) {  chomp;  …  }

 There are three ways to modify the execution of the loop:  last;  next;  redo;  Statement labels  Labels a location in the program  Best to use all uppercase letters OUTER: while(…) { … }

 do { action } while ( condition );  do { action } until ( condition );

 for loop  for ( init_exp ; test_exp; step_exp) { action }  for ($i=1; $i<5; $i++) {print $i, “\n”;}

 foreach my $number (1..10) {  print “The number is: $number \n”;  }

 q// single quoted string  qq// double quoted string  In qq// the // can be replaced with any other non-alphanumeric character provided you use the same character on both ends of the string

 $a = “123”  $b = “456”  What do we get if we write this line of code,  print $a + $b;  How about this line of code,  print $a. $b;

 ** Exponentiation  -Unitary Negation  * Multiplication  /Division  %Modulo (Remainder)  +Addition  -Subtraction

 Use the file handle  Try this out print “Input something:”; my $newInput= ; print $newInput;  chomp ($newInput);  chop($newInput);

 We want to know for sure that we were successful opening the file so we include a test:  open (OUT1, “>>test.txt”) or die $!;

 To use a filehandle you wrap it in angle brackets.  print OUT1 “Hello World!\n”;  chomp ($in = );

 Remember what the redirectors do:  >  >>  <

 Perl normally treats lines beginning with a # as a comment.  Get in the habit of including comments with your code.  Put a comment block at the beginning of your code which includes your name, the name of the module, date written and the purpose of the code.

#!/usr/bin/perl -w # #Module Name: helloWorld.pl # #Written by Alfred J Bird, Ph.D., NBCT #Date Written – 21 September 2011 #Purpose: To print out the string “Hello world!” # #Date Modified – 25 September, 2011 #Purpose of modification: To fix spelling errors. #Modified by: Al Bird # print “Hello world! \n”;

 Remember there are three basic data types in Perl  Numeric  String  Boolean (Logical)  I differentiate between data types and data structures. Not every author or teacher does. Some books use the terms interchangeably so watch out!

 In PERL there are three types of data structures:  Scalars  Arrays  Hashes  Each structure has it own naming syntax.  $scalar  %hash

 I do not consider a list a data structure but some authors, teachers and CS pros do so be careful.  A list is defined as an ordered set of scalar values.  Lists are delimited by parentheses such as  ()  (1)  (“a”)  (1, 2, 3, 4, 5)  (“a”, “b”, “c”, “d”, “e”)  (‘e’, ‘d’, ‘c’, ‘b’, ‘a’)  Remember that a list is ordered!

 You have already been using lists without knowing it.  When you type the following statement print (“Hello ”, “world”, “! “, “\n”); You are passing a list to the print function.  I have just used a new Perl term, function.  A function is subroutine (a free standing piece of code) or an operator that returns a value and/or does something

 Given a list we created this way: (‘Hello’, ‘world.’, ‘I’, ‘am’, Al’)  We can use another method to create it: qw/Hello world I am Al/  As with earlier similar operators we can use any nonalphanumeric character as a separator: qw#Hello world I am Al# qw&Hello world I am Al& qw{Hello world I am Al}

 We can create a list by using a range.  This list (1, 2, 3, 4, 5, 6)  Is the same as this list (1..6)  But this will not work:  (6..1) does not give (6, 5, 4, 3, 2, 1)  because the left hand side must be less than the rhs  To get the list (6, 5, 4, 3, 2, 1) using a range we need to type reverse (1..6)  Try these using a print statement!

 Remember that a list is ordered!  The elements have a location that can be counted  The counting starts with 0 (the 1 st element is number 0)  How do we print a list?  What is the result of the following statements? print (qw/a b c d e f g/);  How about this statement? print qw/a b c d e f g/;  First predict the results and then try them and see what happens.

 We can refer to individual elements in a list by using a number in brackets [] after the list.  What is the result of the following statement? print ((qw/a b c d e f g/)[2]);  How about this statement: print ((‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’) [3]);  First predict the results and then try them and see what happens.  You can put a scalar variable into the braces $i = 3; print ((qw/a b c d e f g/)[$i]);

 We can refer to a range inside the braces.  What do we get when we run the following statement: print ((qw/a b c d e f g/)[2..4]);  First predict the results and then run the statement.  What about this statement: print ((qw/a b c d e f g/)[3..1]);

 What do you think will happen if you enter the following code: print ((‘z’, ‘x’, ‘c’, ‘v’, ’b’, ‘n’, ‘m’)[-1]);  First make a prediction and then run the code.  How about this code $i=2.9; print ((‘z’, ‘x’, ‘c’, ‘v’, ’b’, ‘n’, ‘m’)[$i]);  First make a prediction and then run the code.

The problem with a list is that it cannot be named! We need to retype the list every time we want to use it. To solve this difficulty we have a data structure called an array. We can give an array a name that starts with

 An array is a data structure that has the characteristics of a list but can be named!  To store a scalar into a variable we use an assignment statement $a = 1;  To store a list into an array we do the same thing: = (1,2,3,4,5); = (‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’); = qw ;

How do we access an individual element in an array? Just like we did in a list.  Using a list if we code: print ((‘now’, ‘is’, ‘the’, ‘time’)[2]);  It will print out the  Likewise if we define an = (‘now’, ‘is’, ‘the’, ‘time’);  Will also print out the  What about print $s[2];? What will it print out?

 Why does the statement print $s[2]; work?  Use the prefix for what you want not what you have.  This is referred to as list vs. scalar context.  It can become a very important concept later.

 How do we add data to an array? = $scalar); #is one way!  But there is a better way!!  $scalar; #will do the same thing!  push will append the value in $scalar to the top  Likewise pop will take the last value in an array and do something with it.  $scalar =

 push() and pop() act on the top of an array (the highest indexed end)  shift() and unshift() act on the bottom of an array and perform the same function.  We already know what reverse() does.

 Another function is sort().  What do you think it does?  Write a simple program to try it with your array of months.  Predict the output before you try it.  What happened?  Now write a simple program to try it with your array of number of days.  Predict the output before you try it.  What happened?????  Why?????