Re: hash lookup table
- From: krahnj@xxxxxxxxx (John W. Krahn)
- Date: Mon, 28 Aug 2006 08:58:53 -0700
Derek B. Smith wrote:
All,
Hello,
I am trying to run logic that will copy/delete 3
versions of log.\d+ files to their respective
directories. Because there are so many directories, I
have built a hash table instead of using a bunch of
"if else conditions" with reg exps. My problem is it
is not returning the words_num translation from the
print sub routine call words_to_num.
BEGIN CODE
foreach my $log (@twoweekdir_contents) {
$NBlogs2[$i++] =
$log if ($log =~
/bpcd\/log|bpdbm\/log|bptm\/log.\d+/);
Your pattern says match the string 'bpcd/log' OR 'bpdbm/log' OR 'bptm/log'
followed by any character followed by one or more digits and the pattern can
be located anywhere in the $log variable. Are you sure that you don't want
digits after 'bpcd/log' or 'bpdbm/log'?
}
##-- Build a hash look-up table for subdirs --##
my %subdir_for = (
'admin' => 0, 'bp' => 1,
'bparchive' => 2, 'bpbackup' => 3,
'bpbkar' => 4, 'bpbrm' => 5,
[ snip ]
'tar' => 47,'vault' => 48,
'vnetd' => 49,'vopied' => 50,
'bporaexp64' => 51, 'mklogdir' => 52,
);
sub words_to_num {
my $words = @_;
An array in scalar context returns the number of elements in that array. You
want to use either:
my $words = shift;
Or:
my $words = $_[ 0 ];
Or:
my ( $words ) = @_;
##-- Treat each sequence of \S+ as a word --##
my @words = split /\s+/, $words;
##-- Translate each word to its appropriate
number --##
my $num = q{};
foreach my $word (@words) {
my $digit = $subdir_for{lc $word};
if (defined $digit) {
$num .= $digit;
}
}
return $num;
That could be written as:
sub words_to_num {
no warnings 'uninitialized';
join '', @subdir_for{ split ' ', lc shift }
}
John
--
use Perl;
program
fulfillment
.
- Follow-Ups:
- Re: hash lookup table
- From: Derek B. Smith
- Re: hash lookup table
- References:
- hash lookup table
- From: Derek B. Smith
- hash lookup table
- Prev by Date: Re: odd variable result
- Next by Date: Re: odd variable result
- Previous by thread: Re: hash lookup table
- Next by thread: Re: hash lookup table
- Index(es):
Relevant Pages
|