Prof. Alfred J Bird, Ph.D., NBCT -bird.wikispaces.umb.edu/ Office – McCormick 3rd floor.

Slides:



Advertisements
Similar presentations
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Advertisements

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.
References and Data Structures. References Just as in C, you can create a variable that is a reference (or pointer) to another variable. That is, it contains.
Lecture 2 Introduction to C Programming
Guide to Programming with Python
Python Dictionary.
Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
Programming and Perl for Bioinformatics Part III.
Scripting Languages Chapter 5 Hashes. Hash Data structure, not unlike an array – it will hold any number of values It indexes values by name – not by.
Hashes a “hash” is another fundamental data structure, like scalars and arrays. Hashes are sometimes called “associative arrays”. Basically, a hash associates.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Programming with Perl CSCE 330 Group presentation by: Robert Shannon Robert Shannon Ryan Mullaney Ryan Mullaney Anthony So Anthony So.
8.1 Hashes (associative arrays). 8.2 Let's say we want to create a phone book... Enter a name that will be added to the phone book: Dudi Enter a phone.
10.1 Variable types in PERL ScalarArrayHash $number $string %hash => $array[0] $hash{key}
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
9.1 Hashes. 9.2 Let's say we want to create a phone book... Enter a name that will be added to the phone book: Ofir Enter a phone number: Enter.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
Computing with Strings CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
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.
The Ruby Programming Language
Prof. Alfred J Bird, Ph.D., NBCT Office – Wheatly 2nd floor Office Hours – MW 3:00PM.
1 An Introduction to Perl Part 2 CSC8304 – Computing Environments for Bioinformatics - Lecture 8.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormack 3rd floor 607.
PHP meets MySQL.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students – * Office – Wheatly.
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
Prof. Alfred J Bird, Ph.D., NBCT Door Code: * Office – McCormick 3rd floor 607 Office.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
11 1 Cookies CGI/Perl Programming By Diane Zak Objectives In this chapter, you will: Learn the difference between temporary and persistent cookies.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Built-in Data Structures in Python An Introduction.
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students.
An Intro to Perl, Pt. 2 Hashes, Foreach Control, and the Split Function.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Perl Chapter 5 Hashes. Outside of world of Perl, know as associative arrays Also called hash tables Perl one of few languages that has hashes built-in.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students.
A Few More Functions. One more quoting operator qw// Takes a space separated sequence of words, and returns a list of single-quoted words. –no interpolation.
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
8.1 Common Errors – Exercise #3 Assuming something on the variable part of the input file. When parsing a format file (genebank, fasta or any other format),
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607.
Perl Tutorial - 2 林光龍. Memory Address (1/2) A number that is assigned to each byte in a computer’s memory that the CPU uses to track where data and instructions.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students.
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
IT443 – Network Security Administration Week 2 – Wireshark Instructor: Alfred J Bird, Ph.D., NBCT
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3nd floor, room 607 Office Hours –Tuesday.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 8.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Monday 3:00 to 4:00 and.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607.
10 - Python Dictionary John R. Woodward.
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
IT441 Network Services Administration
IT441 Network Services Administration
PERL: part II hashes, foreach control statements, and the split function By: Kevin Walton.
Lists.
Dictionary.
Presentation transcript:

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

 Study and try to understand the program filetest.pl on pages See if you can enter it and make it work.

 This string operator resembles the substitute operator from a regex : s/// but it functions very differently  tr/old/new/ will not replace occurrences of the string old with the string new. It will replace any occurrence of the letter o with the letter n, any occurrence of the letter l with the letter e and any occurrence of the letter d with the letter w.

 How can we use the transliteration operator to change every occurrence of a comma into a period?  How can we use the transliteration operator to change every occurrence of the letter a to the number 1?  How can we use the transliteration operator to change the case of all letters in a string?

 Write a small program to:  Input the file string.txt into your program  Change all occurrences of small letters into capital letters  Print the resulting string to the screen

 How can we use the transliteration operator to change the case of all letters in a string?  Write a small program to:  Input the file string.txt into your program  Change all occurrences of small letters into capital letters  Print the resulting string to the screen See transtest.pl in my home directory

 We have already studied two (2) ways to store data in a Perl program:  Scalars  Arrays  Each has its own strengths and weaknesses  In Perl (and many other programming environments) there is a third way to store data. It is referred to as a HASH  Hashes are very useful and very powerful!

 Remember that a scalar variable always starts with a $, for example $scalar  Remember that an array structure always starts with for  Well a hash always starts with a %, for example %hash

 A hash is a data structure that consists of pairs of datum, one called the key and one called the value.  Some people call a hash an associative array.  A hash is stored in no particular order.  In a hash the keys must be unique but the values have no such restrictions.

 A classic example of a use for a hash in Systems Administration is the MAC  IP pairing  To create this type of hash we would enter: my %ipMac = ( ‘ ’, ‘2a:09:4e:3c:31:42’ ‘ ’, ‘0e:88:4e:2a:56:07’ ‘ ’, ‘1a:32:6f:5c:6b:1a’ );  This hash uses the IP as the key and the MAC as the value.

 We could also enter the previous hash as my %ipMac = ( => ‘2a:09:4e:3c:31:42’ => ‘0e:88:4e:2a:56:07’ => ‘1a:32:6f:5c:6b:1a’ );  We call the symbol => the quoting comma because it acts as a comma and quotes the string to the left of it.

 Another possible use for a hash is using student numbers as the key and student names as the value.  Why could we not reverse the key, value pairs in this case?  Can you think of some other examples where a hash would be a good choice for a data structure?

 Since hashes and arrays have a lot in common we can change back and forth between them.  Given the following = qw( Thao Saigon Hoang Boston Kevin Dorchester Gary Houston Susan Manchester Allison Chicago)  We can create a hash directly by an assignment statement: %where  What are the pairs in this hash?

 This way of defining an hash is equivalent to the following: %where = ( Thao => ‘Saigon’ Hoang => ‘Boston’ Kevin => ‘Dorchester’ Gary => ‘Houston’ Susan => ‘Manchester’ Allison => ‘Chicago’ );

 To look up a value in a hash we use something similar to the index notation for an array. However there are two (2) differences:  Instead of locating the value by number, we locate it by the key  Instead of using square brackets [ ] we use curly braces{ }   Here is an example using the hash %where:  print “Hoang lives in $where{Hoang}”;

 Adding a new element to the hash is very simple. All that is needed is to type an assignment statement: $where{Al} = “Quincy”;  Changing an element in the hash is just as simple: $where{Kevin} = “Quincy”;  Remember, Kevin used to live in Dorchester but he now lives in Quincy  Or we can remove an element from the hash using the delete function: delete $where{Gary}  Will remove Gary and Houston from the hash

 There are a few functions that operate on hashes.  keys(%hash) returns a list of all keys in %hash  values(%hash) returns a list of all values in %hash  each(%hash) returns each key/value pair in %hash  delete $hash{key} deletes the key/value pair in %hash  exists $hash{key} returns true if a entry with key exits in %hash

 Do you remember where in the Ubuntu directory tree structure a file is located and what it is named that stores the IP address and the MAC information for computers on the networks we set up in IT341? (I hope so)  Write a small program that will:  Read in the file  Extract the IP addresses and the corresponding MAC addresses  Create a hash named %ipMac containing that information  Print out a table containing the information from the hash

 Read pages 115 to 129 in the textbook.  Try exercises 1-3 on pages  Skim pages