Question

In: Computer Science

In Perl: Create a dictionary of at least 5 key/value pairs. Add a new key/value pair...

In Perl:

  1. Create a dictionary of at least 5 key/value pairs.
  2. Add a new key/value pair to the dictionary.
  3. Change one of the values through reassignment.
  4. Access at least 3 values using the keys.
  5. Access at least 3 keys using the values.
  6. Delete an element.
  7. Test for key inclusion for 3 elements.
  8. Loop through the dictionary, printing out the key and value on the same line separated by a space.

Key1, value1

Key2, value2

…,     …

(Please if you can, provide screenshots)

Thank YOU!

Solutions

Expert Solution

The Perl source code along with the relevant comments is given below.

#!/usr/bin/perl

#create and initialize a dictionary called dict containing five elements
%dict = ('Jean Paul', 55,
         'Lloyd', 32,
         'Khan', 40,
         'Woodie', 26,
         'James', 18);
       
@name = keys %dict;     #name is now an array of all the keys in the dictionary dict
@age = values %dict;    #age is now an array of all the values in the dictionary dict

print "Accessing values using keys:-\n";
#accessing value using keys
print "$name[0]\n";
print "$name[1]\n";
print "$name[2]\n\n";

print "Accessing values using value:-\n";
#accessing value using values
print "$age[0]\n";
print "$age[1]\n";
print "$age[2]\n\n";

print "Adding a new key-value pair:-\n";
#adding a new key value pair to the dictionary dict
$dict{'Albert'} = 65;

print "\$dict{'Albert'} = $dict{'Albert'}\n\n";

print "Update an existing key-value pair:-\n";
#updating an existing pair using assignment
$dict{'James'} = 30;
print "\$dict{'James'} = $dict{'James'}\n\n";

print "Delete a key-value pair:-\n\n";
#delete a key value pair
delete $dict{'Lloyd'};

print "Checking for key existence:-\n";
#check if a key exists in the dictionary
if(exists($dict{'Lloyd'}))
{   print "\$dict{'Lloyd'} value found\n";}
else
{   print "\$dict{'Lloyd'} - no records found\n";}

if(exists($dict{'Khan'}))
{   print "\$dict{'Khan'} value found\n";}
else
{   print "No records found\n";}

if(exists($dict{'Albert'}))
{   print "\$dict{'Albert'} value found\n\n";}
else
{   print "No records found\n\n";}

print "Iterating the dictionary using while loop:-\n";
#iterating dictionary using while loop
while(my($k, $v) = each %dict)
{   print "$k, $v\n"
}

If the indendation are not clear, please refer the screenshot of the code given below.

The screenshot of the output is also given below.

Explanation

  • Dictionary or a hash is a data structure which holds data in the form of a key value pair structure. The key and value can be initialized to a hash by either using , or => symbols.
  • A dictionary dict is initialized with five elements.
  • Using the keywords keys and values, the key and value of the dictionary will be stored into respective arrays.
  • Deletiion of an element can be done using the keyword delete. Eg: delete $dict{'Lloyd'};
  • To check whether a key value pair exist in a dictionary, the command exists can be used. Eg: exists($dict{'Albert'})
  • Iterating a dictionary can be done using a while loop. The each keyword is used to update the state of the hash after each iteration.

Hope this helps. Doubts, if any, can be asked in the comment section.


Related Solutions

In Python: Create a dictionary of at least 5 key/value pairs. Add a new key/value pair...
In Python: Create a dictionary of at least 5 key/value pairs. Add a new key/value pair to the dictionary. Change one of the values through reassignment. Access at least 3 values using the keys. Access at least 3 keys using the values. Delete an element. Test for key inclusion for 3 elements. Loop through the dictionary, printing out the key and value on the same line separated by a space. Key1, value1 Key2, value2 …,     … (Please if you...
Write a python program that keeps names and email addresses in a dictionary as key-value pairs....
Write a python program that keeps names and email addresses in a dictionary as key-value pairs. The program should display a menu that lets the user look up a person’s email address, add a new name and email address, change an existing email address, and delete an existing name and email address. The program should bind the dictionary and save it to a file when the user exits the program. Each time the program starts, it should retrieve the dictionary...
Python - Create/Initialize a dictionary with 5 state/capital pairs of your choosing. The state is the...
Python - Create/Initialize a dictionary with 5 state/capital pairs of your choosing. The state is the key and the capital is the value (e.g. “Kansas”:”Topeka”). Only one statement is needed to do this. - Then prompt the user for a state. e.g. The output Enter a state, and I will display the capital. State: Colorado Colorado's capital is Denver. - If that state exists in the dictionary, display the capital, else display "I'm sorry, but I still need to record...
Use a dictionary to represent a directed graph in which each key is a pair (tuple)...
Use a dictionary to represent a directed graph in which each key is a pair (tuple) of two nodes, with the corresponding value set to the edge weight. For example, W[u,v] =42. Where in u → v. Write a function to calculate the in and out degree of a given node. What would be the advantages and disadvantages of this representation? in python
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate the key 'class_name' with the value 'MAT123', and associate the key 'sect_num' with '211_145'
Process each line so you create dictionary where key is name of the company and value...
Process each line so you create dictionary where key is name of the company and value is a net balance #2.a. First column of each row is a name of the company #2.a.a. Utes company has different types of spelling, so you need to put all of them under the same key #2.b. Second column is payments in #2.c. Third column is payments out. Note that they can be negatives as well. # Hint: Absolute value could be derived by...
Create two functions that you can use for a dictionary manager: 1. remove_item(dictionary,key): a function that...
Create two functions that you can use for a dictionary manager: 1. remove_item(dictionary,key): a function that removes the item with the supplied key from the dictionary, if it exits. If the input key doesn’t exist in the dictionary, print out a message saying that the new item has not been removed because there is no matching key in the dictionary. Your function should not produce a Python error if the item does not exist; 2. add_new_item(dictionary,key,value): a function that adds...
Using python. 1. How to create a dictionary that has an integer as a key and...
Using python. 1. How to create a dictionary that has an integer as a key and a byte as a value? the dictionary keys must contain integers from 0 to 255. It should be similar to UTF-8. When we enter an integer, it should return 1 byte. 2. Create a function when a user gives 1 byte, it should return the key from the previous dictionary.
5. Create this hash variable: in perl language Scalar => ‘dollar sign’, Array => ‘at sign’,...
5. Create this hash variable: in perl language Scalar => ‘dollar sign’, Array => ‘at sign’, Hash => ‘percent sign’ Process it with a foreach loop that prints the key/value pairs so that the keys are printed in sorted order: Array: at sign Hash: percent sign Scalar: dollar sign.
How many 5-card hands have at least one pair?
How many 5-card hands have at least one pair?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT