I am very new to perl and programming in general. I have been trying to figure out how to convert an array into a hash. The goal is to get an array of files within a directory and then store those in a hash for HTML::Template to display (via the TMPL_LOOP)
use File::Find::Rule;
use Data::Dumper;
my $rule = File::Find::Rule->new;
$rule->file;
$rule->name( '*.zone' );
my @files = $rule->in("/home/dns");
This seems to take care of creating my array. The problem lies when converting my array into a hash. The code I have below is what I have come up with, but since the only real data I care about in the hash is the "key" do I need to add a value to it?
%hash = map { $files[$_], $_ + 1 } 0 .. $#files;
foreach my $name (sort keys %hash) {
my $host = $hash{$name};
print $host;
my %row = {
name => $name,
};
perl test.cgi
141629131217242221182019162310411315587
is the output. What appears to be happening is the foreach loop is iterating over the wrong field, but I can't figure out why.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.