Perl Variables: Array Web Programming.

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

Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Dynamic Arrays Lecture 4. Arrays In many languages the size of the array is fixed however in perl an array is considered to be dynamic: its size can be.
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.
Perl I/O Learning Objectives: 1. To understand how to perform input from standard Input & how to process the input 2. To understand how to perform input.
Bioinformatics Lecture 7: Introduction to Perl. Introduction Basic concepts in Perl syntax: – variables, strings, input and output – Conditional and iteration.
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.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Practical Extraction & Report Language Picture taken from
Introduction to Perl. How to run perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Your program/script.
I/O while ($line= ){ #remove new line char \n chomp($line); if($line eq “quit”){ exit(1); } while ( ){ #remove new line char \n chomp($_); if($_ eq “quit”){
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.
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.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)
Practical Extraction & Report Language PERL Joseph Beltran.
CPTG286K Programming - Perl Chapter 3: Arrays and List Data.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
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.
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Introduction to Perl Part III By: Bridget Thomson McInnes 6 Feburary 2004.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
5 1 Data Files CGI/Perl Programming By Diane Zak.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Perl COEN 351  Thomas Schwarz, S.J Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles.
– Introduction to Perl 12/13/ Introduction to Perl - Strings, Truth and Regex Introduction to Perl Session 2 · manipulating.
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
Basic Variables & Operators Web Programming1. Review: Perl Basics Syntax ► Comments: start with # (ignored by Perl) ► Statements: ends with ; (performed.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Perl Scripting III Arrays and Hashes (Also known as Data Structures) Ed Lee & Suzi Lewis Genome Informatics.
Working with Loops, Conditional Statements, and Arrays.
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.
Programming Perl in UNIX Course Number : CIT 370 Week 2 Prof. Daniel Chen.
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
The Scripting Programming Language
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
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.
Computer Science 101 Python Lists. Literals, Assignment, Comparisons, Concatenation Similar to the behavior of strings so far a = [1, 2, 3] b = range(1,
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
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.
String and Lists Dr. José M. Reyes Álamo.
CSc 120 Introduction to Computer Programing II
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Control Structures: if Conditional
Introduction to Perl Jarrad Battaglia.
Perl Variables: Hash Web Programming.
An Introduction to Perl
Basic Input/Output Web Programming.
Control Structures: for & while Loops
Context.
Pemrosesan Teks Herika Hayurani Sept 19, 2006
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: part II hashes, foreach control statements, and the split function By: Kevin Walton.
Perl I/O Learning Objectives:
JavaScript CS 4640 Programming Languages for Web Applications
JavaScript: Introduction to Scripting
Useful functions.
JavaScript CS 4640 Programming Languages for Web Applications
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Presentation transcript:

Perl Variables: Array Web Programming

Review: Perl Variables Scalar e.g. $var1 = “Mary”; $var2= 1; holds number, character, string Array e.g. @array1 = (“Mary”,”Tom”); holds a list of scalars $array1[0]=“Mary”; $array1[1]=“Tom”; Associative array (i.e. hash) e.g. %hash1 = (“Mary”,”F”,”Tom”,”M”); holds key-value scalar pairs $hash{“Mary”}=“F”; $hash{“Tom”}=“M”; Web Programming

Array and List List Array a sequence of scalar values (i.e. elements) e.g. (1, 2, 3), (“a”, ”b”, ”c”), (1, ”a”, 2.3, 4) Array a Perl variable that holds a list data e.g. @array = (1, 2, 3); an array element is a scalar variable e.g. $array[0]; Web Programming

Array Syntax Array Name Array Elements array length first character must be @. second character must be a letter. can consist of only letters, digits, or underscore case-sensitive can be as long as you want (within reason) Array Elements $array_name[$index] array index goes from 0 to (array length -1) e.g. @array = (“cat”, ”dog”, “pig”); $array[0] is “cat”, $array[1] is “dog”, $array[2] is “pig” array length number of elements in an array $array_length = @array; Web Programming

Array: Value Assignment Assigning values to an array variable @array = (element0, element1, ..); an element can be a constant, a variable, an expression, or a list range e.g. 1..5, “a”..“z” e.g. @array = (“one”, 2, “three”, “four”, 5) @array = (1, $a, “two”, @b) @array = (1, 1+1, $a.$b, 1..5) @array = (1) @array = ( ) Array values in a conditional expression if (@array): false if a null array, true otherwise Example script Web Programming

Array Elements array element index of the last element in @array $array[index] index of the last element in @array 1 less than array length $#array last element in @array $array[-1] $array[$#array] $array[@array-1] Web Programming

Printing Arrays print @array; print “@array”; prints all elements concatenated together. print “@array”; prints elements separated by a space (default list separator). the list separator ($”) can be set to a different value. foreach $item (@array) { print “$item\n”; } For ($i=0; $i <=$#array; $i++) { print “$array[$i]\n”; } Example script Web Programming

String to Array Conversion $string = join ($separator, @array); concatenates elements using $separator $string = “@array”; concatenates elements using “ ” @array = split (/$separator/, $string); splits $string by $separator into @array Example script Web Programming

Array Functions @array2 = sort (@array); @array2 = reverse (@array); sorts alphabetically @array2 = reverse (@array); reverses the order of array elements $first = shift (@array); remove and return the first element of @array $last = pop (@array); remove and return the last element of @array unshift (@array, $item); add $item to the start of @array push (@array, $item); add $item to the end of @array chop (@array); chops the last character of each element chomp (@array); removes the last character of each element if it is a newline character Example script Web Programming

Array Functions Insert elements Replace elements Remove elements @array2 = splice (@array, offset, length); Remove elements (length from offset) @array2 holds the removed elements $array2 = splice (@array, offset, length); $array2 holds the last removed element @array2 = splice (@array, offset); Remove elements (from offset to end of the array) Insert elements splice (@array, offset, 0, LIST); Insert LIST at offset No return value Replace elements @array2 = splice (@array, offset, length, LIST); Remove elements (length from offset) & Insert LIST at offset Example script Web Programming

Reading Input into Arrays Reading from STDIN @array = <STDIN>; chomp @array;  Example script Reading from a file reading line by line $line = <FILE>; reading all at once @lines = <FILE>; Example script Web Programming

Advanced I/O Command line arguments Working with a directory @ARGV holds command line arguments e.g. prog1.pl arg1 arg2 @ARGV holds (arg1, arg2)  Example script Working with a directory Open a directory opendir (IND, $directory); Read the contents. @files = readdir (IND); Close the directory closedir (IND); Example script Web Programming