Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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 # 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' => "$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";

In reply to (code) neither clever nor useful array vs. hash example by ybiC

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-03-28 16:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found