How to declare and write a script with hash tables on windows and linux

  • Hash tables with powershell on windows

    Declaration:
    $states=@{“Alberta” = “Calgary”; “British Columbia” = “Vancouver”; “Ontario” = “Toronto” ; “Quebec” = “Montreal”}

    Name
    _____
    Value
    _______
    Alberta Calgary
    British Columbia Vancouver
    Ontario Toronto
    Quebec Montreal

    Add new key-value in hashtable:
    $states.Add(“Manitoba”,”Winnipeg”)

    Remove key-value in hashtable:
    $states.Remove(“Manitoba”,”Winnipeg”)
    Change value in hashtable:
    $states.Set_Item(“Ontario”,”Ottawa”)
    Retrieve value in hashtable:
    $states.Get_Item(“Alberta”)
    Find key in hashtable:
    $states.ContainsKey(“Alberta”)
    Find Value in hashtable:
    $states.ContainsValue(“Calgary”)
    Count items in hashtable:
    $states.Count
    Sort items by Name in hashtable:
    $states.GetEnumerator() | Sort-Object Name -descending
    Sort items by Value in hashtable:
    $states.GetEnumerator() | Sort-Object Value -descending

  • Hash tables with perl on linux or windows

     
    Declaration:
    my %hash = (); #Initialize a hash
    my $hash_ref = {}; # Initialize a hash reference. ref will return HASH
    Clear (or empty) a hash
    for (keys %hash)
    {
    delete $hash{$_};
    }
    Clear (or empty) a hash reference
    for (keys %$href)
    {
    delete $href->{$_};
    }
    Add a key/value pair to a hash
    $hash{ ‘key’ } = ‘value’; # hash
    $hash{ $key } = $value; # hash, using variables
    Using Hash Reference
    $href->{ ‘key’ } = ‘value’; # hash ref
    $href->{ $key } = $value; # hash ref, using variables
    Add several key/value pairs to a hash
    %hash = ( ‘key1’, ‘value1’, ‘key2’, ‘value2’, ‘key3’, ‘value3’ );
    %hash = (
    key1 => ‘value1’,
    key2 => ‘value2’,
    key3 => ‘value3’,
    );

    Copy a hash
    my %hash_copy = %hash; # copy a hash
    my $href_copy = $href; # copy a hash ref
    Delete a single key/value pair
    delete $hash{$key};
    delete $hash_ref->{$key};

  • Hash tables with python on linux or windows

    Hash tables are called dictionary in python.
    Declaration:
    dict = {‘Name’: ‘Zara’, ‘Age’: 7, ‘Class’: ‘First’}
    Accessing Values
    print “dict[‘Name’]: “, dict[‘Name’]
    print “dict[‘Age’]: “, dict[‘Age’]
    Output:
    dict[‘Name’]: Zara
    dict[‘Age’]: 7
    Updating Dictionary
    dict = {‘Name’: ‘Zara’, ‘Age’: 7, ‘Class’: ‘First’}

    dict[‘Age’] = 8; # update existing entry
    dict[‘School’] = “DPS School”; # Add new entry
    Delete Dictionary Elements
    #!/usr/bin/python

    dict = {‘Name’: ‘Zara’, ‘Age’: 7, ‘Class’: ‘First’}

    del dict[‘Name’]; # remove entry with key ‘Name’
    dict.clear(); # remove all entries in dict
    del dict ; # delete entire dictionary

Source:

  1. https://technet.microsoft.com/en-us/library/ee692803.aspx
  2. http://www.cs.mcgill.ca/~abatko/computers/programming/perl/howto/hash/
  3. http://www.tutorialspoint.com/python/python_dictionary.htm