Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Perl Hashes Software Tools

2 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 value (not just small non-negative integers). l A hash index is called a key (keys must be unique). l The elements of a hash have no fixed order, unlike arrays. l The elements of a hash are like filing cards, where the top half of the card has the key, and the bottom half has the value. l The keys are used to lookup the values. 16 2046.1 “0 C” “32 F” keys: values:

3 Slide 3 Hash Variables l Hash variable names begin with the percent sign (%) followed by the usual variable name. There is no relationship between $bill, @bill, and %bill, Perl considers them to be separate variables. l Each element of a hash is a separate scalar variable, accessed by the key. Elements of the hash %bill, are referenced with $bill{$key}, where $key is any scalar expression.

4 Slide 4 Hash Variables l As with arrays, you create new hash elements: $bill{"Gates"} = "cheap"; $bill{"Clinton"} = "busy"; $bill{234.5} = 456.7; l Once created, you can access hash values similar to indexing arrays: print "Bill Gates is ", $bill{"Gates"}, "\n"; $n = "Clinton"; print "Bill $n is $bill{$n}\n"; $n = 234.5; print "Bill $n is $bill{$n}\n"; l Output: Bill Gates is cheap Bill Clinton is busy Bill 234.5 is 456.7

5 Slide 5 Hash Variables l Once created, you can change hash values if needed: $bill{234.5} = 456.7;... $bill{234.5} += 3;# makes it 459.7 Referencing a hash element that does not exist returns the undef value.

6 Slide 6 List Representation of a Hash l You can access the hash as a whole if you need to initialize it or to copy it. The hash unwinds as a list. Each pair in the list represents the key and its value. $bill{"Gates"} = "cheap"; $bill{"Clinton"} = "busy"; $bill{234.5} = 456.7; @billarray = %bill; # @billarray: qw(Gates cheap Clinton busy 234.5 456.7) %a = @billarray; # create %a like %bill %a = %bill; # faster way to do the same %b = qw(Gates cheap Clinton busy 234.5 456.7); # initialize %b like %bill from list values l The order of the key-value pairs is random in the list representation and cannot be controlled. (Don’t rely on the ordering.)

7 Slide 7 Hash reverse You can construct a hash with keys and values swapped using the reverse function: %aback = reverse %a; If %a has two identical values, those will end up as a single element in %aback ( reverse is best used on hashes with unique keys and values). $ cat reverse1 #!/usr/local/bin/perl5 -w $b{"Gates"} = "Bill"; $b{"Clinton"} = "Bill"; %revb = reverse %b; # print out revb key and value... $ reverse1 Bill Gates $

8 Slide 8 keys The keys(%hashname) function returns a list of all the keys currently in the hash. $bill{"Gates"} = "cheap"; $bill{"Clinton"} = "busy"; $bill{234.5} = 456.7; @list = keys(%bill); # @list gets qw(Gates Clinton 234.5) in some random order If there are no elements in the hash, then keys() returns an empty list. As with other Perl functions, the parentheses are optional: @list = keys %bill;

9 Slide 9 keys The keys function is often used in foreach loops to print or access the elements one-by-one: $ cat key #!/usr/local/bin/perl5 -w $bill{"Gates"} = "cheap"; $bill{"Clinton"} = "busy"; $bill{234.5} = 456.7; foreach $k (keys(%bill)){ # for each key of %bill print "At $k we have $bill{$k}\n"; } $ key At 234.5 we have 456.7 At Gates we have cheap At Clinton we have busy $

10 Slide 10 Printing Hashes You cannot print the entire hash like you can arrays: $ cat prhash #!/usr/local/bin/perl5 -w $bill{"Gates"} = "cheap"; $bill{"Clinton"} = "busy"; $bill{234.5} = 456.7; print "Bill hash: %bill\n"; $ prhash Bill hash: %bill $ Most Perl programmers use a foreach loop to print hashes (as in the previous slide).

11 Slide 11 keys In a scalar context, keys returns the number of elements in the hash: $ cat key1 #!/usr/local/bin/perl5 -w $n = keys(%bill); if($n==0){ print "Bill is empty\n"; exit; } print "Bill has $n elements\n"; $ key1 Bill is empty $ exit is like the C++ exit() function, and ends the program immediately.

12 Slide 12 values The values(%hashname) function returns a list of all the values currently in the hash. The values are in exactly the same order as the keys returned by keys(%hashname). $ cat value #!/usr/local/bin/perl5 -w $bill{"Gates"} = "cheap"; $bill{"Clinton"} = "busy"; $bill{234.5} = 456.7; @klist = keys(%bill); @vlist = values(%bill); print "@klist\n"; print "@vlist\n"; $ value 234.5 Gates Clinton 456.7 cheap busy $

13 Slide 13 each 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. $ cat each #!/usr/local/bin/perl5 -w $bill{"Gates"} = "cheap"; $bill{"Clinton"} = "busy"; $bill{234.5} = 456.7; while(($k,$v) = each(%bill)){ print "At $k we have $v\n"; } $ each At 234.5 we have 456.7 At Gates we have cheap At Clinton we have busy $

14 Slide 14 delete l How to remove hash elements? Answer: Use the delete function. l The argument is the keyed reference to be deleted: $ cat delete1 #!/usr/local/bin/perl5 -w $bill{"Gates"} = "cheap"; $bill{"Clinton"} = "busy"; $bill{234.5} = 456.7; delete $bill{"Gates"}; while(($k,$v) = each(%bill)){ print "At $k we have $v\n"; } $ delete1 At 234.5 we have 456.7 At Clinton we have busy $

15 Slide 15 Hash Slices l Like an array, a hash can be sliced to access a collection of elements. l We can use a hash slice to compact initialization. For example: $b{"Gates"} = "cheap"; $b{"Clinton"} = "busy"; $b{234} = 45; can be shortened to: ($b{"Gates"},$b{"Clinton"},$b{234}) = qw(cheap busy 45); can be hash sliced as: @b{"Gates","Clinton",234} = qw(cheap busy 45); (Note that it is @b, not %b.) Another example: @b{"A".. "Z"} = (1.. 26);

16 Slide 16 Hash Slices l We can also use a hash slice with variable interpolation : $ cat hslice #!/usr/local/bin/perl5 -w @b{"Gates","Clinton",234} = qw(cheap busy 45); @k = qw(Gates Clinton); print "The values are: @b{@k}\n"; $ hslice The values are: cheap busy $

17 Slide 17 Hash Slices - Merging Hashes l Hash slices can also be used to merge hashes. In the following example, if there are duplicate keys, the values from the %small hash are used: $ cat merge #!/usr/local/bin/perl5 -w @big{"Gates","Clinton",234} = qw(cheap busy 45); @small{"Horner",234} = qw(111 67); @big{keys %small} = values %small; while(($k,$v) = each(%big)){ print "At $k we have $v\n"; } $ merge At 234 we have 67 At Gates we have cheap At Horner we have 111 At Clinton we have busy $

18 Slide 18 Hash Slices - Merging Hashes l A simpler (though slower) way to merge hashes is: $ cat merge1 #!/usr/local/bin/perl5 -w @big{"Gates","Clinton",234} = qw(cheap busy 45); @small{"Horner",234} = qw(111 67); %big = (%big, %small); while(($k,$v) = each(%big)){ print "At $k we have $v\n"; } $ merge1 At 234 we have 67 At Gates we have cheap At Horner we have 111 At Clinton we have busy $


Download ppt "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."

Similar presentations


Ads by Google