1 / 5

Perl Variables: Hash

Perl Variables: Hash. Hash (Associative Array). Syntax key and value pair %hash = (key1, value1, key2, value2, ….); Hash Elements $hash{keyN} = valueN; Assigning values intialization %hash = (element list); %hash = @array; %hash = (key1=>value1, key2=>value2, …);

ismael
Télécharger la présentation

Perl Variables: Hash

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Perl Variables:Hash Web Programming

  2. Hash (Associative Array) • Syntax • key and value pair • %hash = (key1, value1, key2, value2, ….); • Hash Elements • $hash{keyN} = valueN; • Assigning values • intialization • %hash = (element list); • %hash = @array; • %hash = (key1=>value1, key2=>value2, …); • assigning values to individual hash element • $hash{“key1”} = “value1”; • Hash elements are stored in a random order  Example script Web Programming

  3. Hash Functions • @keys = keys (%hash); • return an array of hash keys • @values = values (%hash); • return an array of hash values • (key,value) = each (%hash); • return a key-value pair sequentially • delete ($hash{key}); • deletes a hash element • exists ($hash{key}) • returns true if key exists in %hash  Example script Web Programming

  4. Printing Hash • print %hash; • hash is collapsed into an array and then printed • i.e. print @hash • print join (“:”,%hash); • hash collapsed into array is joined and printed • i.e. print join (“:”,@hash) • while (($key, $value) = each %hash) { print “$key=$value\n”;} • foreach $key (sort keys %hash) { print “$key=$hash{$key}\n”; }  Example script Web Programming

  5. Hash Examples • Word Countopen(IN,$file) || die “can’t open $file”;@lines=<IN>;close IN;foreach $line(@lines) { @words= split(/ +/,$line); foreach $wd(@words) { $wdcnt{$wd}++; }} Example script Web Programming

More Related