Perl variables hash
This presentation is the property of its rightful owner.
Sponsored Links
1 / 5

Perl Variables: Hash PowerPoint PPT Presentation


  • 63 Views
  • Uploaded on
  • Presentation posted in: General

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, …);

Download Presentation

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Perl variables hash

Perl Variables:Hash

Web Programming


Hash associative array

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


Hash functions

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


Printing hash

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


Hash examples

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


  • Login