15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

570<br />

Python Data Structures<br />

One unique feature of Perl arrays is that you can use arrays to mimic a stack<br />

data structure with Perl’s push and pop commands. To push a new item<br />

onto an array, you can use the push command:<br />

push(@arrayname, item2add);<br />

To pop an item off the array, use the pop command like this:<br />

$variablename = pop(@arrayname);<br />

Creating a Perl hash array<br />

A hash array stores an item along with a key. Perl offers two ways to store<br />

values and keys. The first is like this:<br />

%hasharray = (<br />

key1 => value1,<br />

key2 => value2,<br />

key3 => value3,<br />

);<br />

Notice that hash arrays are identified by the percentage (%) symbol.<br />

A second way to define a hash array is like this:<br />

%hasharray = (“key1”, value1, “key2”, value2, “key3”,<br />

value3);<br />

To retrieve data from a hash array, you need to know the key associated with<br />

that value and identify the hash array name by using the $ symbol like this:<br />

$variable = $hasharray (“key1”);<br />

The preceding command would store the value associated with “key1” into<br />

the $variable.<br />

Python Data Structures<br />

Python offers tuples, lists, and dictionary data structures. Both tuples and<br />

lists contain a series of items, such as numbers and strings. The main difference<br />

is that items in a tuple can’t be changed whereas items in a list can be<br />

changed. A dictionary stores values with keys, allowing you to retrieve<br />

values using its distinct key.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!