Accessing an Associative Array 

Accessing an Associative Array
  • If we want to extract a value from the associative array, we reference it with the following syntax: 
  • $variable_equal_to_value = 
    $ASSOCIATIVE_ARRAY_NAME{'[key]'}; 
  • Thus, to pull out the value of the "full_name" key from %CLIENT_ARRAY, we use the following syntax: 
  • $full_name = $CLIENT_ARRAY{'full_name'} 
  • The variable $full_name would then be equal to "Selena Sol". Think of it as using a "key" to unlock a "value". 
When accessing an associative array using a scalar variable as a key, you should not surround the key with single quotes because the scalar variable will not be interpolated. For example, the following syntax generates the value for the age key 

$key_name = "age"; 
$age = $CLIENT_ARRAY{$key_name}; 

  • Accessing an associative array is one of the most basic CGI functions and is at the heart of the ReadParse routine in cgi-lib.pl that creates an associative array from the incoming form data. We will talk more about ReadParse later today. 
  • By accessing this associative array (usually referred to as %in or %form_data), your CGI script will be able to determine what it is that the client has asked of it since HTML form variables are formed in terms of administratively-defined NAMES and client-defined VALUES using syntax such as the following: 
  • <INPUT TYPE = "text" NAME = "full_name"
           SIZE = "40">
  • The "key" of the associative array generated by ReadParse will be "full_name" and the "value" will be whatever the client typed into the text box. 

Additional Resources:

Perl Associative Arrays
Table of Contents
Using the keys and values functions


Graphics & Media Lab. >> Библиотека | Курсы | Графикон

Hosted by Graphics & Media Lab
http://graphics.cs.msu.su
lab_logo
mailto: Laboratory