Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

(code) neither clever nor useful array vs. hash example

by ybiC (Prior)
on Jul 02, 2000 at 01:10 UTC ( #20769=snippet: print w/replies, xml ) Need Help??
Description: My background isn't programming, so to most Monks this snippet is likely incredibly rudimentary. Nonetheless, after ~2 years dabbling with Perl (mostly regex logfile analysis and non-DB, HTML-generating-CGI), 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:
p.s. I think the Debian stable version of Perl (5.00404) is keeping me from doing insertion order retrieval.

Update: added Tie::IxHash for insertion-order retrieval Jan 3, 2001

#!/usr/bin/perl -w

# 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 "\n";

print "\nHASH+WHILE";
my %varhash = (
    ' 1 script'     =>  "$0" ,
    ' 2 executable' =>  "$^X $]" ,
    ' 3 hostOS'     =>  "$^O" ,
    ' 4 starttime'  =>  "$^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'     =>  "$0" ,
    ' executable' =>  "$^X $]" ,
    ' hostOS'     =>  "$^O" ,
    ' starttime'  =>  "$^T" ,
for my $key (keys %tiedhash) {
    print "\n", $key, " is ", $tiedhash{$key};
print "\n";
Replies are listed 'Best First'.
RE: neither clever nor useful array vs. hash example
by ZZamboni (Curate) on Jul 03, 2000 at 18:50 UTC
    It's not the version of perl that keeps you from doing retrieval in the order of insertion. Hashes are that way by nature. There is no guarantee of the order in which the elements will come out when you use keys or each.


      On an aside, you can look at japhy's YAPC 19100 talk on making a ordered hash. No real revelations, but is pretty interesting and the technique is useful. It is here.


      Thanks for taking time to comment, ZZamboni. I really should have phrased my "p.s." something like :

      p.s. I played a bit with a module that facilitates Hash insertion order retrieval, but couldn't make it work. Wish I could remember the module - stumbled across it somewhere in O'Reilly Perl CD Bookshelf.

      It's as likely or more that I had bad syntax than a down-level Perl incompatibility. Is nothing important to me at the moment. By the time I expect to pick it back up, hope to be on Debian 2.2 with a newer $^X$]
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://20769]
[Corion]: ambrus: Hmmm. It makes sense that AnyEvent would implement the push_write itself, but I think I don't have a good idea of where the boundary between AnyEvent and the underlying event system lies... Implementing the timer should give me a better idea
[ambrus]: Corion: push_write is in the higher level abstraction of AnyEvent::Handle, not in the watcher
[Corion]: ambrus: Hmm - rereading Prima::File, that merrily coincides with what Prima does - it tells you "you can read", and you're supposed to read from the fh yourself. I thought it called you with the data already read, which would've been harder to integrate
[ambrus]: you just need an io watcher, created by &AnyEvent::Impl:: Whatever::io(...)
[Corion]: So after talking it through with you even while I'm still not entirely clear on where AE ends and my implementation begins, I think I understand that I only need to implement some smaller parts for each functionality I want to support.
[Corion]: Yeah... and you might even be able to mix and match additional functionality if you have additional async suppliers, like from a separate thread
[ambrus]: You hvae to be careful with the timer, because apparently Prima::Timer insists on being periodic, wheras AnyEvent::Impl:: Whatever::timer should give a one-shot timer watcher

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2016-12-08 12:23 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (141 votes). Check out past polls.