Perl Day 5. Arrays vs Hash Arrays are one way to store multiple things in a variable. Hashes are another. Arrays are one way to store multiple things.

Slides:



Advertisements
Similar presentations
» PHP arrays are lists of values stored in key-value pairs. » Uses of arrays: Many built-in PHP environment variables. Database functions use arrays.
Advertisements

Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
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.
“What do you want me to do now?”
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.
Microsoft ® Office Word 2007 Training Bullets, Numbers, and Lists ICT Staff Development presents:
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
Hashes a “hash” is another fundamental data structure, like scalars and arrays. Hashes are sometimes called “associative arrays”. Basically, a hash associates.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
Alice Variables Pepper. Set to Java look Edit / preferences restart.
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
Conversational Computers
JavaScript Switch Statement. Switch JavaScript Switch Statement If you have a lot of conditions, you can use a switch statement instead of an if…elseif…
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Subroutines Just like C, PERL offers the ability to use subroutines for all the same reasons – Code that you will use over and over again – Breaking large.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
INTERNET APPLICATION DEVELOPMENT For More visit:
1 Day 3 Directories Files Moving & Copying. 2 Case Sensitive First thing to learn about UNIX is that everything is case sensitive. Thus the files: –enda.
Loops Doing the same thing over and over and over and over and over and over…
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Arrays – What is it? – Creation – Changing the contents Functions – What is it? – Syntax – How they work – Anonymous functions A quick lesson in Objects.
Invitation to Computer Science, Java Version, Second Edition.
Python November 28, Unit 9+. Local and Global Variables There are two main types of variables in Python: local and global –The explanation of local and.
Control Structures. Important Semantic Difference In all of these loops we are going to discuss, the braces are ALWAYS REQUIRED. Even if your loop/block.
Perl Day = 2 We’ve previously seen you can do math on variables We’ve previously seen you can do math on variables$Num=1$Num2=$Num+7;print(“$Num2\n”);
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Hash Table Concepts & Implementations. Sorting by theory Hash Table Concepts Implementation.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (
Perl Day 1. Programming Computers know how to execute a sequence of instructions Computers know how to execute a sequence of instructions –Instructions.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Introduction to Perl Part III By: Bridget Thomson McInnes 6 Feburary 2004.
David Stotts Computer Science Department UNC Chapel Hill.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
CRYPTOGRAPHY. WHAT IS PUBLIC-KEY ENCRYPTION? Encryption is the key to information security The main idea- by using only public information, a sender can.
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 6 Functions. Subprograms In Perl, all subprograms are functions – returns 0 or 1 value – although may have “side-effects” optional function.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
ActionScript: For Loops, While Loops, Concatenation and Arrays MMP 220 Multimedia Programming This material was prepared for students in MMP220 Multimedia.
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.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Review of Previous Classes Declaring Variables - var myVar:DataType = value Data Types – Number, uint, String, Boolean Functions – parameters, return.
Pass the Buck Every good programmer is lazy, arrogant, and impatient. In the game “Pass the Buck” you try to do as little work as possible, by making your.
Changing HTML Attributes each() function Anonymous Functions $(this) keyword.
Perl Day 6. Multiline Strings Perl supports a mechanism to deal with multiple lines of text rather than having to add them one at a time Perl supports.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
JavaScript Modularity. Goals By the end of this lecture, you should … Understand why programmers use modularity. Understand how to create a function in.
Aims: To learn about some simple sorting algorithms. To develop understanding of the importance of efficient algorithms. Objectives: All:Understand how.
Chapter 6 Functions The Tic-Tac-Toe Game. Chapter Content In this chapter you will learn to do the following: 0 Write your own functions 0 Accept values.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
References and Data Structures
FLOWCHARTS Part 1.
Introduction To Repetition The for loop
C Functions Pepper.
Functions and Procedures
While Loops BIS1523 – Lecture 12.
Functions BIS1523 – Lecture 17.
Remembering lists of values lists
Presentation transcript:

Perl Day 5

Arrays vs Hash Arrays are one way to store multiple things in a variable. Hashes are another. Arrays are one way to store multiple things in a variable. Hashes are another. –In an array, the elements in the array are automatically numbered, 0, 1, 2, 3…. –In a hash, you can decide what they are called –Hashes start with a % instead of –You use => to indicate what belongs to what. –When you want to pull the answer back you use $hash{key} %FruitColors=( 'apple' => 'green', 'banana' => 'yellow', 'orange' => 'orange'); print("Apples are $FruitColors{apple}\n");

Keys Each array element is said to have a “key” and a “value”. Each array element is said to have a “key” and a “value”. –In the previous example, the fruit name (apple, banana etc) are keys. The colors are “values”. –You can get the list of “keys” in a hash with the keys command. It returns an %FruitColors; –You can also use a loop show all of them: foreach $Fruit (keys %FruitColors) { print(“$Fruit is $FruitColors{$Fruit}\n”); print(“$Fruit is $FruitColors{$Fruit}\n”);}

sort Sometimes you want your fruits alphabetized: Sometimes you want your fruits alphabetized: The fruits in order are: The fruits in order are: foreach $Fruit (sort keys %FruitColors) { print(“$Fruit is $FruitColors{$Fruit}\n”); print(“$Fruit is $FruitColors{$Fruit}\n”);}

Uses for Hashes Lets imagine you had of the first name of everyone in our office. You might want to know how many people named “Michael” we have. Lets imagine you had of the first name of everyone in our office. You might want to know how many people named “Michael” we have. All Hash values start off as 0, so you can simply add 1 to them each time you find a Michael All Hash values start off as 0, so you can simply add 1 to them each time you find a encer’,’kris’); foreach $Name { $NameCount{$Name}++; $NameCount{$Name}++;} foreach $FirstName (keys %NameCount) { print(“There are $NameCount{$FirstName} people named $FirstName\n”); print(“There are $NameCount{$FirstName} people named $FirstName\n”);}

Removing something from a hash If you need to get rid of something out of a hash, you use delete If you need to get rid of something out of a hash, you use deletedelete($FruitColor{banana});

Abstraction One of the most important concepts in doing a big task is Abstraction. One of the most important concepts in doing a big task is Abstraction. –Imagine I told you to build this building. How would you do it? 1.Hire an architect 2.Buy some land 3.Hire a contractor to oversee building it –Note the details of how the contractor hires his people etc are not important right now.  This is abstraction, solving smaller problems, which will ultimately allow you to solve the bigger problem.

Abstraction in programming Likewise when asked to write a big script, you don’t just try to do it all at once. Likewise when asked to write a big script, you don’t just try to do it all at once. –Do all the little parts you know how to do first –Use those building blocks to solve the bigger problem –Examples of how to break things up  Dealing  Opening a file and reading it’s contents, putting them into an array  Doing some regular expression magic to figure something out  Writing out the result to a file. –There is no single right answer on how things should be broken up, but a good rule of thumb is if you can’t explain in 1 sentence what something it doing, it should be broken up into smaller tasks.

Sub tasks In perl each sub task is called a subroutine (sub for short) In perl each sub task is called a subroutine (sub for short) –Each has a name, you should call it something descriptive –Subroutines can be used over an over (that’s the whole point)  Each time you call it, you can pass different “parameters” –Subs can return results –Subroutines are called with & sub sayhi { print(“Hello world\n”); print(“Hello world\n”);}&sayhi;

Subroutine scope The inner workings of a subroutine should be hidden from the rest of the script The inner workings of a subroutine should be hidden from the rest of the script –Think of a sub as a black box  When you turn on your TV, you don’t care about what’s happening inside it, you just expect it to produce pictures and sound.  Thus you need to ensure that any variables you use inside a sub are hidden from the outside world –It’s suck if your TV changed channel just because your neighbor changed his TV. If the “$CurrentChannel” variable in your TV was the same as the “$CurrentChannel” variable in your neighbors TV, this might just happen. –To ensure that variables are hidden inside your sub, use these special keywords to indicate your variables are just for this sub:  my  local –There is a slight difference between them, however it’s unimportant for now, use my for all scalars, and local for all arrays and hashes.

Passing parameters Each sub you define automatically gets an array passed to it Each sub you define automatically gets an array passed to it –This array contains anything you pass to the sub when you call it. sub add2numbers { $Answer=$Num1+$Num2; $Answer=$Num1+$Num2; return($Answer); return($Answer);}$Result=&add2numbers(17,3); print(“ = $Result\n”); $Result2=&add2numbers(32,8); print(“ = $Result2\n”);

Getting results To stop working in a subroutine and return to wherever you called it from, you simply say: To stop working in a subroutine and return to wherever you called it from, you simply say:return; If you need to send back results, you’d say: If you need to send back results, you’d To get the results, you’d have assigned the call to the sub to a variable, or list of variables: To get the results, you’d have assigned the call to the sub to a variable, or list of

One weirdness If you need to pass an array + (a scalar, or another array) to a sub, there is a problem. If you need to pass an array + (a scalar, or another array) to a sub, there is a problem. –Because of the way that all parameters to a sub are magically sent if you send 2 doesn’t know where one starts and the other ends, so you’ll end up with a single array. This is probably not what you want –There are 2 solutions to this depending on what you are doing.  If you are sending an Array and some Scalars, be sure to send the scalars FIRST, and the Array LAST. –This will work fine:  –This will not:  ($Var1 will end up empty) –If you must send 2 arrays do it like this:  –Call it like this:  &Sub(*Arr1,*Arr2);

Sorting by value In our last example, we got a list of all the names, and how many people have each name, but what if you want that sorted by the most popular names? In our last example, we got a list of all the names, and how many people have each name, but what if you want that sorted by the most popular esh','Kris','Chad','Michael','Scott','Michael','Rich','Shawn','Wes','Jeff','Adam','Hyun Jin','Sean','Enda'); foreach $Name { $NameCount{$Name}++; $NameCount{$Name}++;} sub SortByValue { $NameCount{$b} $NameCount{$a}; $NameCount{$b} $NameCount{$a};} foreach $PopularName (sort SortByValue keys %NameCount) { print("There are $NameCount{$PopularName} people named $PopularName\n"); print("There are $NameCount{$PopularName} people named $PopularName\n");}

Next and Last When you are in a loop, sometimes you just want to stop abruptly When you are in a loop, sometimes you just want to stop abruptly –Loops have 2 controls:  last; –This exits the loop immediately, and continues past the closing }  next; –This stops this iteration of the loop, and moves onto the next, restarting at the { Imagine you want the top 3 most popular names Imagine you want the top 3 most popular names –Change our last example to this: $Count=0; foreach $PopularName (sort SortByValue keys %NameCount) { $Count++; $Count++; print("There are $NameCount{$PopularName} people named $PopularName\n"); print("There are $NameCount{$PopularName} people named $PopularName\n"); if($Count>3) if($Count>3) { last; } { last; }}