+ All Categories
Home > Documents > Perl Tutorial - 2 林光龍. Memory Address (1/3) A number that is assigned to each byte in a...

Perl Tutorial - 2 林光龍. Memory Address (1/3) A number that is assigned to each byte in a...

Date post: 04-Jan-2016
Category:
Upload: kelley-wade
View: 215 times
Download: 2 times
Share this document with a friend
22
1 Perl Tutorial - 2 林林林
Transcript

1

Perl Tutorial - 2

林光龍

2

Memory Address (1/3)

• A number that is assigned to each byte in a computer’s memory that the CPU uses to track where data and instructions are stored in RAM. Each byte is assigned a memory address whether or not it is being used to store data. The computer’s CPU uses the address bus to communicate which memory address it wants to access, and the memory controller reads the address and then puts the data stored in that memory address back onto the address bus for the CPU to use.

3

Memory Address (2/3)

$b = 17;$a = \$b;

4

$b[2]$b[1]

$a @b $b[0]$c{“k3”}

$c{“k2”}

%c $c{“k1”}

E015

E001 E013

E017

E033

E031

E035

Memory Address (3/3)

5

$b[2]$b[1]

$a @b $b[0]$c{“k3”}

$c{“k2”}

%c $c{“k1”}

E015

E001 E013

E017

E033

E031

E035

\$a \@a

\%c

Reference (1/2)

6

Reference (2/2)

• A reference is a scalar value that points to a memory location that holds some type of data.

$a = 10;@b = (1, 2, 3);%c = ("k1" => "foo", "k2" => "bar");

$add_of_a = \$a;$add_of_b = \@b;$add_of_c = \%c;

print <<"END";The address of \$a is $add_of_aThe address of \@b is $add_of_bThe address of \%b is $add_of_cEND

7

Multi-Dimensional Array

@array1 = ('20020701', 'Sending Mail in Perl', 'Philip Yuson');@array2 = ('20020601', 'Manipulating Dates in Perl', 'Philip Yuson');@array3 = ('20020501', 'GUI Application for CVS', 'Philip Yuson');@main = (@array1, @array2, @array3);

The result would be similar to this:

@main = ('20020701', 'Sending Mail in Perl', 'Philip Yuson','20020601', 'Manipulating Dates in Perl', 'Philip Yuson','20020501', 'GUI Application for CVS', 'Philip Yuson');

8

Multi-Dimensional Array

Instead of pumping these into one list, you can put the references of these arrays in the list:@main = (\@array1, \@array2, \@array3);

Or to simplify:

@main = ( ['20020701', 'Sending Mail in Perl', 'Philip Yuson'],['20020601', 'Manipulating Dates in Perl', 'Philip Yuson'],['20020501', 'GUI Application for CVS', 'Philip Yuson']);

9

To Get Value in Multi-Dimensional Array (1/2)

To reference the first column of the first row:$ref = $main[0]; # set $ref to reference of @array1$ref->[0]; # Returns the first item in @array

To simplify:$main[0]->[0];

You can also simplify this as:$main[0][0];

To get the value of the second column of the third row:$ref = $main[2]; # Third row;$ref->[1]; # second column;

10

To Get Value in Multi-Dimensional Array (2/2)

use strict;my @john = (86, 77, 82, 90);my @paul = (88, 70, 92, 65);my @may = (71, 64, 68, 78);my @grades = (\@john, \@paul, \@may);

for (my $row = 0; $row < $#grades+1; $row++) { for (my $col = 0; $col < $#{$grades[$row]}+1; $col++) { print $grades[$row][$col] . ", "; } print "\n";}

11

Associative Array (1/2)

• Scalar is the simplest Perl data type which was designed to hold only one thing like a number, a string or a reference.

• A list is an ordered collection of scalars. An array is a variable that contains a list.

• A hash is a data structure like an array, in that it can hold any number of values and retrieve these values at will. However, instead of indexing the values by number, as we did with arrays, we'll look up the values by name. That is, the indices (here, we'll call them keys) aren't numbers but are arbitrary unique strings

12

Associative Array (2/2)

3$x

Dog

$y

3$a[2]

21$a[1]

$a[0] @a

188

$h{"he"}

Wall Larry$h{“ln"}

$h{"fn"} %h

13

Creating a Hash

my %weekdays = ('Sun' => 'Sunday','Mon' => 'Monday','Tue' => 'Tuesday','Wed' => 'Wednesday','Thu' => 'Thursday','Fri' => 'Friday','Sat' => 'Saturday', );

my %month = ('January' => '01','February' => '02','March' => '03','April' => '04','May' => '05','June' => '06','July' => '07','August' => '08','September' => '09','October' => '10','November' => '11','December' => '12', );

14

Hash Operation

• Retrieving a value from a hash.

• Adding new key/values to a hash.

• Changing the value of an existing hash key.

• Deleting a key/value from a hash.

my $day_of_the_week = $weekdays{'Wed'};

delete $weekdays{'some'};

$weekdays{'some'} = 'someday';

$weekdays{'some'} = 'some day';

15

Hash Function

Function Example

keys(%hashname)

$fred{"aaa"}="bbb";

$fred{234.5}=456.7;

@list = keys(%fred);

$num_of_hash_pair = keys(%fred);

values(%hashname)

%lastname = ();

$lastname{"fred"}="flintstone";

$lastname{"barney"}="rubble";

@lastnames=values(%lastname);

each(%hashname) ($key, $val) = each(%lastname)

delete $hashname{$key}

16

%map = qw(red apple green leaves blue ocean);print "A string please: "; chomp($some_string = <STDIN>);print "The value for $some_string is $map{$some_string}\n" if (defined( $map{$some_string}));

# DEFINE A HASH%coins = ( "Quarter" , 25, "Dime" , 10, "Nickel", 5 ); # LOOP THROUGH ITwhile (($key, $value) = each(%coins)){ print "($key = $value)\n";}

17

Sorting Hashes by Key

%grades = (kim => 96,al => 63,rocky => 87,chrisy => 96,jane => 79,

);

print "\n\tGRADES SORTED BY STUDENT NAME:\n";foreach $key (sort (keys(%grades))) { print "\t\t$key \t\t$grades{$key}\n";}

18

Sorting Hashes by Valuesub hashValueAscendingNum { $grades{$a} <=> $grades{$b};}sub hashValueDescendingNum { $grades{$b} <=> $grades{$a};}%grades = ( student1 => 90,

student2 => 75,student3 => 96,student4 => 55,student5 => 76,);

print "\nGRADES IN ASCENDING NUMERIC ORDER:\n";foreach $key (sort hashValueAscendingNum (keys(%grades))) { print "\t$grades{$key} \t\t $key\n";}print "\nGRADES IN DESCENDING NUMERIC ORDER:\n";foreach $key (sort hashValueDescendingNum (keys(%grades))) { print "\t$grades{$key} \t\t $key\n";}

19

The Standard File Handles Function Description

STDINReads program input. Typically this is the computer's keyboard.

STDOUTDisplays program output. This is usually the computer's monitor.

STDERR

Displays program errors. Most of the time, it is equivalent to STDOUT, which means the error messages will be displayed on the computer's monitor.

# To read a line from the keyboard and then display it. # This will continue until you press Ctrl+Z on DOS systems or Ctrl-D on UNIX systems.

while (<STDIN>) { print();}

20

$INPUT_FILE = $ARGV[0];

open(INPUT_FILE);@array = <INPUT_FILE>;close(INPUT_FILE);

foreach (@array) { print();}

2121

Reference• Perl Tutorial: Start,

– http://www.comp.leeds.ac.uk/Perl/start.html– http://www.cyut.edu.tw/~hcchen/perl/Perl%20tutorial%20Start.ht

m (Big5 Version)

• The perl.ogr Online Library, http://www.perl.org/books/library.html

• Beginning Perl – perl.org http://www.perl.org/books/beginning-perl/

• Perl For CGI tutorialshttp://www.developingwebs.net/perl/

22

• A. D. Marshall, Practical Perl Programming, 1999-2005, http://www.cs.cf.ac.uk/Dave/PERL/perl_caller.html

• Simon Cozens, Ten Perl Myths, February 23, 2000, http://www.perl.com/pub/a/2000/01/10PerlMyths.html

• 簡信昌 , Perl 學習手札全文 , http://perl.hcchien.org/toc.html

• 朱孝國 , Perl 筆記 , http://irw.ncut.edu.tw/peterju/perl.html

• Perl的安全性監測 , http://www.linuxuser.com.tw/skill_detail.php?cid=869

• Programmer's File Editor, http://www.lancs.ac.uk/staff/steveb/cpaap/pfe/default.htm

• The CPAN Search Site, http://search.cpan.org/• Regular Expression Tutorial, http://www.regular-

expressions.info/tutorial.html

22


Recommended