Perl Chapter 5 Hashes. Outside of world of Perl, know as associative arrays Also called hash tables...

16
Perl Chapter 5 Hashes

Transcript of Perl Chapter 5 Hashes. Outside of world of Perl, know as associative arrays Also called hash tables...

Page 1: 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.

Perl Chapter 5

Hashes

Page 2: 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.

Hashes

• Outside of world of Perl, know as associative arrays

• Also called hash tables• Perl one of few languages that has hashes

built-in

Page 3: 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.

Structure of Hashes

• Hashes are lists of scalar values BUT have string indices (called keys)

• keys also stored in structure• variable name starts with % • have their own namespace (like arrays)• need not be declared, grow and shrink• no way to determine order• internal has function to store and retrieve

Page 4: 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.

literals

• no hash literals (use list literals)(“bob”, 42, “carol”, 40, …)

• or use => instead of comma(“bob” => 42, “carol” => 40, …)

or(bob => 42, carol => 40, …) – do not need “ “’s, left of => implicitly quotes

barewords

Page 5: 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.

• first is actually a list, odd subscripted elements of array keys of the hash

@list = (Bob, 42, Carol, 40);

%ages = @list;

same as%ages =(“bob” => 42, “carol” => 40);

must be even length!

%salaries = (“Bob” => 79_500, “Carol” => 43_000);

Page 6: 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.

• accessed by “subscripting” with key$salaries{“Bob”} 79500

• insert new values$salaries{“Mike”} = 51_950;

• if Mike not in table, adds it• if Mike is in table, changes value• set to empty%salaries = ();

undef %salaries

• NOT %salaries = undef; (1 element, undef)

Page 7: 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.

printing

• hash variables not interpolated in double-quoted strings

• print “%salaries\n”; – prints %salaries

• print %salaries; – prints keys and values, no spaces

Page 8: 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.

slice of hash

• gives us a list or array

@some_salaries = @salaries{“Bob”, “Mike”};

• @some_salaries (79500, 51950)• note the @ form of the variable• since slice of a hash is an array, can be

interpolated in double quoted strings

Page 9: 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.

operators

delete $salaries {“Billie”};

– key and salary deleted from %salariesif (exists $salaries{“Billie”}) …

– to find out if in hash

Page 10: 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.

keys and values operators

• keys and values of a hash are arrays• keys operator list of keys• values operator list of values

%highs=(“mon”=>64, “tue”=>66, “wed”=>72,

“thu”=>55, “fri”=>35);

@days = keys %highs; #array context• @days is (“mon”, “tue”, “wed”, “thu”, “fri”)

Page 11: 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.

foreach

foreach $day (@days) { … }

• orforeach $day (keys %highs){

print “on $day,the temp was $highs{$day}\n”;

} ^hashing• of course, can sort (sort (keys %highs))

• keys in scalar context $length = keys %highs;

Page 12: 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.

values operator

@temps = values %highs;

foreach $temp (values %highs){

print “$tep\n”;

}

Page 13: 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.

Process pairs

• use each operator to return next element ($day, $temp) = each %highs;

• usually iterate on it while (($day, $temp)= each %highs){

print “On $day, the high temp was

$temp.\n”;

}

• cannot add to hash in loop body, if keys or each used in loop

Page 14: 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.

in boolean expression

if (%highs) …• scalar context –> boolean expression, true if

hash not empty

Page 15: 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.

• Predefined hashes– %ENV in first example

• When to use array vs. hash– when you have many accesses to specific

elements

Page 16: 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.

Examples

• freq.pl• FindFiles.pl