Re: hash lookup table



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
.



Relevant Pages

  • Re: FAQ 20-12
    ... In order to place the digits in the correct order, ... set to point to address the last byte of the array, ... The only bit of maths that you might not understand is that (num % ...
    (comp.lang.c)
  • Re: Updated datestamp doesnt work
    ... Public Sub StoreMyOldVals ... ' store values of current row in array ... Dim dbs As DAO.Database, rst As DAO.Recordset ... Dim var As Variant ...
    (microsoft.public.access.gettingstarted)
  • Re: Multiple OUs?
    ... something is up with adding multiple values to the array... ... ' If the AD enumeration runs into an OU object, call the Sub again to ... strLine = Trim ... strNewContents = strNewContents & strLine & vbCrLf ...
    (microsoft.public.scripting.vbscript)
  • Re: Packages and returning errors
    ... > array intact. ... sub is_a_instance_method { ... my $class = shift; ... You need to fix the scope of $error by moving its declaration outside ...
    (comp.lang.perl.misc)
  • Re: Updated datestamp doesnt work
    ... Public Sub StoreMyOldVals ... ' store values of current row in array ... Dim dbs As DAO.Database, rst As DAO.Recordset ... Dim var As Variant ...
    (microsoft.public.access.gettingstarted)