Indian Institute of Technology Kharagpur. PERL Part II. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. I.I.T.

Similar documents
(Refer Slide Time: 01:12)

Indian Institute of Technology Kharagpur. PERL Part III. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. I.I.T.

Practical Report and Extraction Language (PERL)

A control expression must evaluate to a value that can be interpreted as true or false.

Introduction to Perl. Perl Background. Sept 24, 2007 Class Meeting 6

COMS 3101 Programming Languages: Perl. Lecture 2

Programming Perls* Objective: To introduce students to the perl language.

CS 230 Programming Languages

Perl. Interview Questions and Answers

T-( )-MALV, Natural Language Processing The programming language Perl

Scripting Languages Perl Basics. Course: Hebrew University

Perl. Many of these conflict with design principles of languages for teaching.

Perl. Perl. Perl. Which Perl

IT441. Network Services Administration. Perl: File Handles

CSCI 4152/6509 Natural Language Processing. Perl Tutorial CSCI 4152/6509. CSCI 4152/6509, Perl Tutorial 1

They grow as needed, and may be made to shrink. Officially, a Perl array is a variable whose value is a list.

SEER AKADEMI LINUX PROGRAMMING AND SCRIPTINGPERL 7

Bourne Shell Reference

Examples of Using the ARGV Array. Loop Control Operators. Next Operator. Last Operator. Perl has three loop control operators.

Conditional Control Structures. Dr.T.Logeswari

Control Structures. CIS 118 Intro to LINUX

Pathologically Eclectic Rubbish Lister

The Power of Perl. Perl. Perl. Change all gopher to World Wide Web in a single command

Learning Perl 6. brian d foy, Version 0.6, Nordic Perl Workshop 2007

Regular expressions and case insensitivity

Essentials for Scientific Computing: Bash Shell Scripting Day 3

8/13/ /printqp.php?heading=II BSc [ ], Semester III, Allied: COMPUTER PROGRAMMING-PERL -309C&qname=309C

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.

Outline. CS3157: Advanced Programming. Feedback from last class. Last plug

Introduction to Perl. c Sanjiv K. Bhatia. Department of Mathematics & Computer Science University of Missouri St. Louis St.

COMS 3101 Programming Languages: Perl. Lecture 1

Introduc)on to Unix and Perl programming

Shell Start-up and Configuration Files

Classnote for COMS6100

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Bash scripting Tutorial. Hello World Bash Shell Script. Super User Programming & Scripting 22 March 2013

Introduc)on to Unix and Perl programming

Bashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

A shell can be used in one of two ways:

What is PERL?

CSC105, Introduction to Computer Science I. Introduction. Perl Directions NOTE : It is also a good idea to

Scripting. More Shell Scripts. Adapted from Practical Unix and Programming Hunter College

print STDERR "This is a debugging message.\n";

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

1. Introduction. 2. Scalar Data

Outline. Introduction to Perl. Why use scripting languages? What is expressiveness. Why use Java over C

Useful Unix Commands Cheat Sheet

Introduction. C provides two styles of flow control:

Lecture 02 The Shell and Shell Scripting

Control Structures. Important Semantic Difference

The PHP language. Teaching you everything about PHP? Not exactly Goal: teach you how to interact with a database via web

Regular expressions and case insensitivity

Modularity and Reusability I. Functions and code reuse

Welcome to Research Computing Services training week! November 14-17, 2011

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

Week January 27 January. From last week Arrays. Reading for this week Hashes. Files. 24 H: Hour 4 PP Ch 6:29-34, Ch7:51-52

Bash scripting basics

CMSC 331 Final Exam Fall 2013

PHP by Pearson Education, Inc. All Rights Reserved.

Beginning Perl. Mark Senn. September 11, 2007

PERL Scripting - Course Contents

1. Hello World Bash Shell Script. Last Updated on Wednesday, 13 April :03

bash Execution Control COMP2101 Winter 2019

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Basic Linux (Bash) Commands

GNU ccscript Scripting Guide IV

Perl Scripting. Students Will Learn. Course Description. Duration: 4 Days. Price: $2295

Shell programming. Introduction to Operating Systems

Programming introduction part I:

(Refer Slide Time: 01:40)

There are some string operators that can be used in the test statement to perform string comparison.

COMP284 Scripting Languages Lecture 2: Perl (Part 1) Handouts

What is PHP? [1] Figure 1 [1]

CS313D: ADVANCED PROGRAMMING LANGUAGE

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

Title:[ Variables Comparison Operators If Else Statements ]

COMP284 Scripting Languages Lecture 11: PHP (Part 3) Handouts

Answers to AWK problems. Shell-Programming. Future: Using loops to automate tasks. Download and Install: Python (Windows only.) R

COMP 4/6262: Programming UNIX

COP4020 Programming Assignment 1 - Spring 2011

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

DEMO A Language for Practice Implementation Comp 506, Spring 2018

2. λ is a regular expression and denotes the set {λ} 4. If r and s are regular expressions denoting the languages R and S, respectively

Hands-On Perl Scripting and CGI Programming

Indian Institute of Technology Kharagpur. Javascript Part III. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. I.I.T.

Shells and Shell Programming

Introductory Perl. What is Perl?

Shell Programming (bash)

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

function [s p] = sumprod (f, g)

8. Control statements

Decision Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

More Perl. CS174 Chris Pollett Oct 25, 2006.

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

CSCI-GA Scripting Languages

Transcription:

Indian Institute of Technology Kharagpur PERL Part II Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. I.I.T. Kharagpur, INDIA Lecture 22: PERL Part II On completion, the student will be able to: Define the file handling functions, and illustrate their use. Define the control structures in Perl, with illustrative examples. Define the relational operators and conditional statements. Illustrate the features using examples. 1

Sort the Elements of an Array Using the sort keyword, by default we can sort the elements of an array lexicographically. Elements considered as strings. @colors = qw (red blue green black); @sort_col = sort @colors # Array @sort_col is (black blue green red) Another example: @num = qw (10 2 5 22 7 15); @new = sort @num; # @new will contain (10 15 2 22 5 7) How do sort numerically? @num = qw (10 2 5 22 7 15); @new = sort {$a <=> $b @num; # @new will contain (2 5 7 10 15 22) 2

The splice function Arguments to the splice function: The first argument is an array. The second argument is an offset (index number of the list element to begin splicing at). Third argument is the number of elements to remove. @colors = ( red, green, blue, black ); @middle = splice (@colors, 1, 2); # @middle contains the elements removed File Handling 3

Interacting with the user Read from the keyboard (standard input). Use the file handle <STDIN>. Very simple to use. print Enter your name: ; $name = <STDIN>; # Read from keyboard print Good morning, $name. \n ; $name also contains the newline character. Need to chop it off. The chop Function The chop function removes the last character of whatever it is given to chop. In the following example, it chops the newline. print Enter your name: ; chop ($name = <STDIN>); # Read from keyboard and chop newline print Good morning, $name. \n ; chop removes the last character irrespective of whether it is a newline or not. Sometimes dangerous. 4

Safe chopping: chomp The chomp function works similar to chop, with the difference that it chops off the last character only if it is a newline. print Enter your name: ; chomp ($name = <STDIN>); # Read from keyboard and chomp newline print Good morning, $name. \n ; File Operations Opening a file The open command opens a file and returns a file handle. For standard input, we have a predefined handle <STDIN>. $fname = /home/isg/report.txt ; open XYZ, $fname; while (<XYZ>) { print Line number $. : $_ ; 5

Checking the error code: $fname = /home/isg/report.txt ; open XYZ, $fname or die Error in open: $! ; while (<XYZ>) { print Line number $. : $_ ; $. returns the line number (starting at 1) $_ returns the contents of last match $i returns the error code/message Reading from a file: The last example also illustrates file reading. The angle brackets (< >) are the line input operators. The data read goes into $_ 6

Writing into a file: $out = /home/isg/out.txt ; open XYZ, >$out or die Error in write: $! ; for $i (1..20) { print XYZ $i :: Hello, the time is, scalar(localtime), \n ; Appending to a file: $out = /home/isg/out.txt ; open XYZ, >>$out or die Error in write: $! ; for $i (1..20) { print XYZ $i :: Hello, the time is, scalar(localtime), \n ; 7

Closing a file: close XYZ; where XYZ is the file handle of the file being closed. Printing a file: This is very easy to do in Perl. $input = /home/isg/report.txt ; open IN, $input or die Error in open: $! ; while (<IN>) { print; close IN; 8

Command Line Arguments Perl uses a special array called @ARGV. List of arguments passed along with the script name on the command line. Example: if you invoke Perl as: perl test.pl red blue green then @ARGV will be (red blue green). Printing the command line arguments: foreach (@ARGV) { print $_ \n ; Standard File Handles <STDIN> Read from standard input (keyboard). <STDOUT> Print to standard output (screen). <STDERR> For outputting error messages. <ARGV> Reads the names of the files from the command line and opens them all. 9

@ARGV array contains the text after the program s name in command line. <ARGV> takes each file in turn. If there is nothing specified on the command line, it reads from the standard input. Since this is very commonly used, Perl provides an abbreviation for <ARGV>, namely, < > An example is shown. $lineno = 1; while (< >) { print $lineno ++; print $lineno: $_ ; In this program, the name of the file has to be given on the command line. perl list_lines.pl file1.txt perl list_lines.pl a.txt b.txt c.txt 10

Control Structures Introduction There are many control constructs in Perl. Similar to those in C. Would be illustrated through examples. The available constructs: for foreach if/elseif/else while do, etc. 11

Concept of Block A statement block is a sequence of statements enclosed in matching pair of { and. if (year == 2000) { print You have entered new millenium.\n ; Blocks may be nested within other blocks. Definition of TRUE in Perl In Perl, only three things are considered as FALSE: The value 0 The empty string ( ) undef Everything else in Perl is TRUE. 12

if.. else General syntax: if (test expression) { # if TRUE, do this else { # if FALSE, do this Examples: if ($name eq isg ) { print Welcome Indranil. \n ; else { print You are somebody else. \n ; if ($flag == 1) { print There has been an error. \n ; # The else block is optional 13

elseif Example: print Enter your id: ; chomp ($name = <STDIN>); if ($name eq isg ) { print Welcome Indranil. \n ; elseif ($name eq bkd ) { print Welcome Bimal. \n ; elseif ($name eq akm ) { print Welcome Arun. \n ; else { print Sorry, I do not know you. \n ; while Example: (Guessing the correct word) $your_choice = ; $secret_word = India ; while ($your_choice ne $secret_word) { print Enter your guess: \n ; chomp ($your_choice = <STDIN>); print Congratulations! Mera Bharat Mahan. 14

for Syntax same as in C. Example: for ($i=1; $i<10; $i++) { print Iteration number $i \n ; foreach Very commonly used function that iterates over a list. Example: @colors = qw (red blue green); foreach $name (@colors) { print Color is $name. \n ; We can use for in place of foreach. 15

Example: Counting odd numbers in a list @xyz = qw (10 15 17 28 12 77 56); $count = 0; foreach $number (@xyz) { if (($number % 2) == 1) { print $number is odd. \n ; $count ++; print Number of odd numbers is $count. \n ; Breaking out of a loop The statement last, if it appears in the body of a loop, will cause Perl to immediately exit the loop. Used with a conditional. last if (i > 10); 16

Skipping to end of loop For this we use the statement next. When executed, the remaining statements in the loop will be skipped, and the next iteration will begin. Also used with a conditional. Relational Operators 17

The Operators Listed Comparison Equal Not equal Greater than Less than Greater or equal Less or equal Numeric ==!= > < >= <= String eq ne gt lt ge le Logical Connectives If $a and $b are logical expressions, then the following conjunctions are supported by Perl: $a and $b $a && $b $a or $b $a $b not $a! $a Both the above alternatives are equivalent; first one is more readable. 18

SOLUTIONS TO QUIZ QUESTIONS ON LECTURE 21 19

Quiz Solutions on Lecture 21 1. Do you need to compile a Perl program? No, Perl works in interpretive mode. You just need a Perl interpreter. 2. When you are writing a Perl program for a Unix platform, what do the first line #!/usr/bin/perl indicate? The first line indicates the full path name of the Perl interpreter. 3. Why is Perl called a loosely typed language? Because by default data types are not assigned to variables. Quiz Solutions on Lecture 21 4. A string can be enclosed within single quotes or double quotes. What is the difference? If it is enclosed within double quotes, it means that variable interpolation is in effect. 5. How do you concatenate two strings? Give an example. By using the dot(.) operator. $newstring = $a. $b. $c; 20

Quiz Solutions on Lecture 21 6. What is the meaning of adding a number to a string? The number gets added to the ASCII value. 7. What is the convenient construct to print a number of fixed strings? Using line-oriented quoting (print << somestring). 8. How do you add a scalar at the beginning of an array? @xyz = (10, @xyz); Quiz Solutions on Lecture 21 9. How do you concatenate two arrays and form a third array? @third = (@first, @second); 10. How do you reverse an array? @xyz = reverse @original; 11. How do you print the elements of an array? print @arrayname ; 21

QUIZ QUESTIONS ON LECTURE 22 Quiz Questions on Lecture 21 1. How to sort the elements of an array in the numerical order? 2. Write a Perl program segment to sort an array in the descending order. 3. What is the difference between the functions chop and chomp? 4. Write a Perl program segment to read a text file input.txt, and generate as output another file out.txt, where a line number precedes all the lines. 5. How does Perl check if the result of a relational expression is TRUE of FALSE. 22

Quiz Questions on Lecture 21 6. For comparison, what is the difference between lt and <? 7. What is the significance of the file handle <ARGV>? 8. How can you exit a loop in Perl based on some condition? 23