<?xml version="1.0" encoding="windows-1252"?>
<node id="20769" title="(code) neither clever nor useful array vs. hash example" created="2000-07-01 21:10:28" updated="2005-08-14 10:27:36">
<type id="1980">
snippet</type>
<author id="14909">
ybiC</author>
<data>
<field name="doctext">
</field>
<field name="snippetdesc">
My background isn't programming, so to most Monks this snippet is likely incredibly rudimentary.  Nonetheless, after ~2 years dabbling with Perl &lt;i&gt;&amp;#040;mostly regex logfile analysis and non-DB, HTML-generating-CGI&amp;#041&lt;/i&gt;, I was recently forced to actually learn how to implement arrays and hashes in a script.  Here's how I got my brain to begin grasping them:
&lt;br&gt;&amp;nbsp;&lt;br&gt;
&lt;i&gt;p.s. I think the Debian stable version of Perl &amp;#040;5.00404&amp;#041; is keeping me from doing insertion order retrieval.&lt;/i&gt;

&lt;p&gt;
&lt;b&gt;Update: &lt;/b&gt; added Tie::IxHash for insertion-order retrieval Jan 3, 2001</field>
<field name="snippetcode">
&lt;code&gt;#!/usr/bin/perl -w

# notclever.pl
# Rudimentary examples of array, hash, tied hash
# Updated using Komodo beta 1.0.0 build 12686 on Win2k
# Tested: Perl 5.00503 on Debian
#         ActivePerl 5.006 on Win2k

use strict;
use Tie::IxHash;


print "\nPASSEL O' PRINTS";
print "\n 1 script        : $0";
print "\n 2 executable    : $^X $]";
print "\n 3 host OS       : $^O";
print "\n 4 start time    : $^T";
print "\n";



print "\nARRAY+FOREACH";
my @varlist = (
    "\n 1 script        : $0",
    "\n 2 executable    : $^X $]",
    "\n 3 host OS       : $^O",
    "\n 4 start time    : $^T",
    );
foreach (@varlist) {
    print;
    }
print "\n";



print "\nHASH+WHILE";
my %varhash = (
    ' 1 script'     =&gt;  "$0" ,
    ' 2 executable' =&gt;  "$^X $]" ,
    ' 3 hostOS'     =&gt;  "$^O" ,
    ' 4 starttime'  =&gt;  "$^T" ,
    );
while((my $key, my $value) = each(%varhash)) {
    print "\n", $key, " is ", $value;
    }
print "\n";



print "\nTIED HASH+FOR";
tie my %tiedhash, "Tie::IxHash";
%tiedhash = (
    ' script'     =&gt;  "$0" ,
    ' executable' =&gt;  "$^X $]" ,
    ' hostOS'     =&gt;  "$^O" ,
    ' starttime'  =&gt;  "$^T" ,
    );
for my $key (keys %tiedhash) {
    print "\n", $key, " is ", $tiedhash{$key};
    }
print "\n";
&lt;/code&gt;</field>
</data>
</node>
