2.1 Lists and Arrays 2.1. 2.2 Summary of 1 st lesson Single quoted and double quoted strings Backslash ( \ ) – the escape character: \t \n Operators:

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Perl Arrays and Lists Software Tools. Slide 2 Lists l A list is an ordered collection of scalar data. l A list begins and ends with parentheses, with.
Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
Perl Arrays and Lists Learning Objectives: 1. To understand the format and the declaration of Arrays & Lists in Perl 2. To distinguish the difference between.
CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
4.1 Controls: Ifs and Loops. 4.2 Controls: if ? Controls allow non-sequential execution of commands, and responding to different conditions else { print.
4ex.1 More loops. 4ex.2 Loops Commands inside a loop are executed repeatedly (iteratively): my $num=0; print "Guess a number.\n"; while ($num != 31) {
1 Controlling Script Flow! David Lash Chapter 3 Conditional Statements.
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.
Practical Extraction & Report Language Picture taken from
CSC3530 Software Technology Tutorial Two PERL Basics.
4.1 Revision. 4.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n"; my $number.
CS 898N – Advanced World Wide Web Technologies Lecture 7: PERL Chin-Chih Chang
5.1 Revision: Ifs and Loops. 5.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n";
3.1 Ifs and Loops. 3.2 Revision: variables Scalar variables can store scalar values: Variable declaration my ($priority); Numerical assignment $priority.
4.1 More loops. 4.2 Loops Commands inside a loop are executed repeatedly (iteratively): my $num=0; print "Guess a number.\n"; while ($num != 31) { $num.
2ex.1 Lists and Arrays. 2ex.2 Comments on exercises Always run your script with “ perl -w ” and take care of all warnings  submitted scripts should not.
3ex.1 Note: use strict on the first line Because of a bug in the Perl Express debugger you have to put “use strict;” on the first line of your scripts.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Perl Practical(?)‏ Extraction and Report Language.
Slide 3-1 CHAPTER 3 Conditional Statements Objectives To learn to use conditional test statements to compare numerical and string data values To learn.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Introduction to Perl Giorgos Georgakilas Graduated from C.E.I.D.Graduated from C.E.I.D. M.Sc. degree in ITMBM.Sc. degree in ITMB Ph.D. student in DIANA-LabPh.D.
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: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.
Introduction to Unix – CS 21
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
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.
Getting started in Perl: Intro to Perl for programmers Matthew Heusser – xndev.com - Presented to the West Michigan Perl User’s Group.
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)
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.
5.1 Revision: Ifs and Loops. 5.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n";
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.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Working with Loops, Conditional Statements, and Arrays.
31/01/ Selection If selection construct.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
2.1 Lesson 2: Scalar Functions and Arrays “Perl programming is an empirical science!” - Larry Wall.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
Perl Scripting II Conditionals, Logical operators, Loops, and File handles Suzi Lewis Genome Informatics.
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.
The Scripting Programming Language
Copyright © 2003 Pearson Education, Inc. Slide 3-1 The Web Wizard’s Guide to PHP by David A. Lash.
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 8.
Chapter 17 Arrays Perl to denote an array, for = (10, 20, 30, 50); Array subscripts are number from 0. Array elements require scalar.
Lesson 05: Iterations Class Chat: Attendance: Participation
ITM 352 Flow-Control: Loops
3rd prep. – 2nd Term MOE Book Questions.
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.
Chapter 3: Selection Structures: Making Decisions
Chapter 3: Selection Structures: Making Decisions
The Selection Structure
Presentation transcript:

2.1 Lists and Arrays 2.1

2.2 Summary of 1 st lesson Single quoted and double quoted strings Backslash ( \ ) – the escape character: \t \n Operators: numbers: + - * / ** strings:. x Variables: my $name; Reading input: $name = ; Functions: length($name); substr($name,2,2);

2.3 Undefined variables my $a; print($a+3); Use of uninitialized value in addition (+) 3 print("a is :$a:"); Use of uninitialized value in concatenation (.) or string a is ::

2.4 Lists and arrays A list is an ordered set of scalar values: (1,2,3,"fred") An array is a variable that holds a list: = (1,2,3,"fred"); You can access an individual array element: print $a[1];2 $a[0] = "*"; scalar 4scalar 3scalar 2scalar 1

2.5 Lists and arrays You can easily get a sub-array: = (1,2,3,"fred","bob"); print $a[1];2 You can extend an array as much as you like: = (1,2,3) $b[5] = is now (1,2,3,undef,undef,6) scalar 4scalar 3scalar 2scalar 1

2.6 Lists and arrays Assigning to arrays: = (3..6); (3,4,5,6) = qw(a b cat d); ("a","b","cat","d") my = (1..5); $a=1; Counting array elements: print 4 2.6

2.7 Reading and printing arrays You can read lines from the standard input in list context: = will store all the lines entered until the user hits ctrl-z. You can interpolate arrays and array elements into strings: abcatd print a b cat d print "$b[2] is the third element of cat is the third element 2.7

2.8 Manipulating arrays – push & pop = (1,2,3,4,5); = (1,2,3,4,5); my $x = print $x;5 2.8

2.9 shift & unshift = (1,2,3); = (1,2,3); my $x = print $x;1 2.9

2.10 split & join = split(",", "hello,how are you?,goodbye"); print "$a[1]\n"; how are you? holds the list: ("hello","how are you?","goodbye") my $str = print "$str\n"; hello:how are you?:goodbye 2.10

2.11 Reversing lists = ("yossi","bracha","moshe"); print join(";", moshe;bracha;yossi (You can also reverse strings…) 2.11

2.12 Sorting lists Default sorting is alphabetical: = sort("yossi","bracha","moshe"); is ("bracha","moshe","yossi") = sort(1,3,9,81,243); is (1,243,81,9) Other forms of sorting require subroutine definition: = sort(compare_sub 1,3,9,81,243); We’ll get to that latter… 2.12

2.13 The Debugger 2.13

2.14 Debugging A complex program will never work correctly the first time you run it! So: Write the program one stage at a time and check that it works Use a debugger to execute the program step by step Next line that will be executed

2.15 Next line that will be executed Start debuggerStep one line Run continuously Add breakpoint – to run until this point

2.16 Choose “ i/o ” for interactive input 1 View printed output 2 Enter input 3

2.17 You can “ watch ” your variables as they change their values using the “ Watch List ” window Mark a variable name 1 Click “ Add Watch ” 2

2.18 Enter expression In order to “ watch ” arrays and more complex data use the “ Evaluate Watch ” button

2.19 Controls: Ifs and Loops

2.20 Controls: if ? Controls allow non-sequential execution of commands, and responding to different conditions. else { print "Are you doing anything tomorrow night?\n"; } print "How old are you?\n"; my $age = ; if ($age < 18) { print "Sorry, I’m not allowed to chat with minors\n"; }

2.21 if, elsif, else It’s convenient to test several conditions in one if structure: if ($age < 18) { print "Sorry, I’m not allowed to chat with minors\n"; } elsif ($age < 25) { print "Are you doing anything tomorrow night?\n"; } elsif ($age < 35) { print "Are you married?\n"; } else { print "Do you need help crossing the street?\n"; }

2.22 Comparison operators StringNumericComparison eq==Equal ne!=Not equal lt<Less than gt>Greater than le<= Less than or equal to ge>= Greater than or equal to if ($age == 18)... if ($name eq "Yossi")... if ($name ne "Yossi")... if ($name lt "n")...

2.23 Boolean operators if (($age==18) && ($name eq "Yossi"))... if (!($name ne "Yossi"))... if (!($name eq "Yossi:" && $age==18))... And && Or || Not !

2.24 Controls: Loops Commands inside a loop are executed repeatedly (iteratively): while ($name ne "Yossi") { chomp($name = ); print "Hello $name!\n"; } foreach $name { print "Hello $name!\n"; } * There are also until, do-while and do-until loops

2.25 Loops $i=0; while { $name = $names[$i]; print "Hello $name!\n"; $i++; } for ($i=0; $i++) { $name = $names[$i]; print "Hello $name!\n"; } A for loop is controlled by three statements: 1 st is executed before the first iteration 2 nd is the stop condition 3 rd is executed before every re-iteration These are equivalent

2.26 Breaking out of loops next – skip to the next iteration last – skip out of the loop = ; foreach $line { if (substr($line,0,1) ne ">") { next; } print(substr($line,1)); if (substr($line,0,4) eq ">ehd") { last; } }

2.27 Breaking out of loops die – end the program and print an error message to the standard error if ($score<0) { die "score must be positive"; } score must be positive at test.pl line 8. Note: if you end the string with a "\n" then only your message will be printed * warn does the same thing as die without ending the program