Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year 2014-2015.

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

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
Guide to Programming with Python
Fundamentals of Python: From First Programs Through Data Structures
Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
ITC 240: Web Application Programming
Perl Hashes Software Tools. Slide 2 “bill” “cheap” What is a Hash? l A hash (or associative array) is like an array, where the index can be any scalar.
Stacks.
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.
7ex.1 Hashes. 7ex.2 Let's say we want to create a phone book... Enter a name that will be added to the phone book: Eyal Enter a phone number:
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
8.1 References and complex data structures. 8.2 An associative array (or simply – a hash) is an unordered set of key=>value pairs. Each key is associated.
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}
8ex.1 References and complex data structures. 8ex.2 An associative array (or simply – a hash) is an unordered set of key=>value pairs. Each key is associated.
Guide To UNIX Using Linux Third Edition
Chapter 8 Arrays and Strings
Programming Logic and Design Fourth Edition, Comprehensive
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.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 8: Perl Basics Fundamentals of Web Programming.
Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)
1 Microsoft Access 2002 Tutorial 5 – Enhancing a Table’s Design, and Creating Advanced Queries and Custom Forms.
1 An Introduction to Perl Part 2 CSC8304 – Computing Environments for Bioinformatics - Lecture 8.
Lists in Python.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Section 4.1 Format HTML tags Identify HTML guidelines Section 4.2 Organize Web site files and folder Use a text editor Use HTML tags and attributes Create.
PHP meets MySQL.
 A database is a collection of data that is organized so that its contents can easily be accessed, managed, and updated. What is Database?
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 14 Tuples, Sets, and Dictionaries 1.
Chapter 8 Arrays and Strings
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
9 1 DBM Databases CGI/Perl Programming By Diane Zak.
If statements while loop for loop
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
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.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Built-in Data Structures in Python An Introduction.
Introduction to Perl Part III By: Bridget Thomson McInnes 6 Feburary 2004.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
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.
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.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
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),
Perl Scripting III Arrays and Hashes (Also known as Data Structures) Ed Lee & Suzi Lewis Genome Informatics.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
Unit 6 Repetition Processing Instructor: Brent Presley.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
FILE I/O: Low-level 1. The Big Picture 2 Low-Level, cont. Some files are mixed format that are not readable by high- level functions such as xlsread()
An Introduction to Programming with C++ Sixth Edition
Working with Collections of Data
Containers and Lists CIS 40 – Introduction to Programming in Python
The relational operators
Python I/O.
One-Dimensional Array Introduction Lesson xx
File Handling Programming Guides.
T. Jumana Abu Shmais – AOU - Riyadh
Remembering lists of values lists
Presentation transcript:

Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year

Regular expressions -recap /ab?c//ab*c//ab+c/ ac abc abbc /a(bc)?d//a(bc)*d//a(bc)+d/ ad abcd abccd abcbcd

Regular expressions -recap /ab?c//ab*c//ab+c/ acyes no abcyes abbcnoyes /a(bc)?d//a(bc)*d//a(bc)+d/ adYes no abcdYes abccdno abcbcdnoYes

Hashes (or associative arrays) Scalars are single data entities. Arrays are ordered collections of data entities. A hash is an unordered collection of data which may contain many sets of data, but there is no “first” or “last”, no “top” or “bottom” item in a hash. The order of the key-value pairs is random in the list representation and cannot be controlled. (Don’t rely on the ordering.) Hash data is always in pairs. Hashes are treated much like lists, but the hash variable is used. Hash variables all start with %.

Creating a hash A hash is created in the same way as a list: %hash1=(“Intel”, “Pentium”, “AMD”, “Athlon”, “Transmeta”, “Crusoe”); To help readability, the hash is usually laid out in its declaration: %hash1=(“Intel”, “Pentium”, “AMD”, “Athlon”, “Transmeta”,“Crusoe”); This shows the pairing of hash data more clearly

The => operator To make relationships in hashes even clearer, Perl allows the use of the => operator, which Perl interprets to mean a double-quoted string and a comma. This: %hash1=(“Intel”, “Pentium”, “AMD”, “Athlon”, “Transmeta”,“Crusoe”); becomes this: %hash1=(Intel => Pentium, AMD => Athlon, Transmeta => Crusoe); This is the form we are going to use to create hashes!

Hash keys When you have a pair like this: Intel => Pentium the left side of the pair is called the hash key, and the right side is the value. The hash key is used to look up the pair. All hash keys must be unique. If you have more than one hash key the same, the newer entries will overwrite the older ones in the list. Values do not have to be unique. Having unique hash keys leads to the use of hashes as a quick-lookup structure.

Locating hash entries To locate a specific entry in a hash, we use the hash keys. The key is enclosed in curly braces: %hash1=(Intel => Pentium, AMD => Athlon, Transmeta => Crusoe); print $hash1{AMD}; This will print the value corresponding to the AMD hash key, which is “Athlon”. Note the use of a scalar $hash1.

Exercise Create a hash with a set of names and phone numbers. Populate the hash with five sets of data. Ask the user which name they would like to look up the phone number for, and use that as the key to display the number. Tell the user which are the possible values for the names in your hash, don’t let him guess : )

Showing all entries with keys Instead of using a for or foreach loop to show all entries in a hash, the keys function can be used: keys(%hash) This will show a list of all the keys. To show keys and values, a for loop can be used: foreach $item (keys %hash) { print “$item has value $hash{$item}\n”; } How about printing the keys sorted? Harder: what if we want the values sorted?

Another way to print a hash is with the each function. each returns a key-value pair as a two-element list. Each time each is called it returns the next key- value pair until all the elements have been accessed. When no more pairs, each returns an empty list. while(($key,$value) = each(%planets)) { print "At $key we have $value\n"; }

Sort the hash in alphabetical order of its keys %planets = ( Mercury => 0.4, Venus => 0.7, Earth => 1, Mars => 1.5); foreach $name (sort keys %planets) { print $name. “=>”. $planets{$name}. “\n”; }

Sort the values of the hash foreach $distance (sort values %planets) { print $distance. “\n”; } sort the values based on the ASCII table foreach $distance (sort {$a $b} values %planets) { print $distance. “\n”; } sorts numerically

Sort the keys of the hash according to the values foreach $name (sort { $planets{$a} $planets{$b} } keys %planets) { print $name. “=>”. $planets{$name}; }

Reversing hashes You can use the reverse function on a hash to reverse the pairs, converting the value to a hash key and the hash key to a value. This allows you to look up with either value: reverse %hash; Reversing a hash swaps the hash key and value, not the order of the pairs in the hash Be careful when using reverse. Since all hash keys have to be unique, there is the chance of losing some data if the values are the same prior to reversing.

Exercise Set up a hash that contains a few city names and their corresponding zip codes (guess if you don’t know them). Then, display the names of the cities and ask the user to enter one. Display the corresponding zip code. Reverse the hash and display the zip codes, asking the user to choose one. Display the city for that zip code. You can display the lists of cities and zip codes any way you want, or display both at the same time with the keys function.

Adding entries to a hash You can add entries to a hash by specifying the key and value: %hash1=(Intel => Pentium, AMD => Athlon, Transmeta => Crusoe); $hash1{HP} = “PARISC”; This adds the entry HP as the key and “PARISC” as the value to the hash. Again, we use a scalar for the function.

Changing values You can change values in a hash by reassigning them: %hash1=(Intel => Pentium, AMD => Athlon, Transmeta => Crusoe); $hash1{AMD} = “Thunderbird”; This will change the value associated with the key AMD. To change the key, you can add a new entry and delete the old one, or reverse the hash and change the value, then reverse back (which may cause problems).

Deleting hash entries To remove an entry from a hash, the delete function is used with the hash key: %hash1=(Intel => Pentium, AMD => Athlon, Transmeta => Crusoe); delete $hash1{Transmeta}; This will delete the hash pair starting with the key “Transmeta”.

Converting hashes to arrays You can convert hashes to arrays and vice-versa easily, since they both have the same list structure. To convert, simply reassign using the proper variable name: %hash=(“A”, “B”, “C”, The first line creates a hash, the second converts the hash format to an array format, and the third line creates a new hash with the array’s contents.

Blanking a hash To remove all data from a hash (hash keys and values), you could use the delete function in a loop, but an easier way is to simply redefine the hash. Suppose you want to blank: %hash=(“A”, “B”, “C”, “D”); To blank this hash, just redefine it: %hash=(); and the old values are deleted and a hash with no contents is left.

Testing for hash keys You can test to see if a hash key exists, preventing problems with other statements. Most people would assume this would work: if ( $hash{$key})… but it doesn’t. To check whether a hash key exists, use the exists function instead: if (exists $hash{$key}) …

Exercise Modify the last program you wrote to: – display the current hash, and ask the user if they want to add a new entry. If they do, prompt for the city and zip and add it to the hash – display the hash again and ask if the user wants to delete an entry. If they do, ask for the hash key to delete, and remove that entry – perform the same lookups for city and zip as before, but check to make sure the keys exist. If not, display an error message.

Exercise Create a file with one page of text in English. Count the number of occurrences of each word. Print the 10 most frequent words.

Great! See you next time!