re: hash lookup table

2006-08-28 Thread Lawrence Statton XE1/N1GAK

Trivial problem.
What does $words *really* contain in your subrotuine words_to_num?


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: hash lookup table

2006-08-28 Thread John W. Krahn
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

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: hash lookup table

2006-08-28 Thread Derek B. Smith


--- "John W. Krahn" <[EMAIL PROTECTED]> wrote:

> 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'?
> 
> 



No I do not want to do thisnice catch. It should
read /bpcd\/log|bpdbm\/log|bptm\/log\.\d+/);
so that I catch log and a period and number after the
period.

thank you
derek



__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: hash lookup table

2006-08-28 Thread Mumia W.

On 08/28/2006 08:37 AM, Derek B. Smith wrote:
All, 

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. 
[...]


So instead of doing the code below because the subdirs 
are unique but all log file names are the same, 
log.\d+,  I want 
to use a hash table to decide what log files to copy 
where based on the subdir names or the key/value 
relationship.


if (@NBlogs2) {
  for my $log(@NBlogs2) {
  if ($log =~ 'bpcd') {
 qx(cp $log $oldir/bpcd/);   
  }

  elsif ($log =~ 'bpdbm') {
 qx(cp $log $oldir/bpdbm);
  }
  elsif ($log =~ 'bptm') {
 qx(cp $log $oldir/bptm);
  }
 } 
}




This was the only code in your post that I was able to 
understand because I wasn't able to figure out what 
words_to_num() was supposed to do. Here are two ways to go 
about something like what you want to do:



# This is ultra-simple and doesn't do
# what you want.

if (@NBlogs2) {
  for my $log (@NBlogs2) {
if ($log =~ m{([[:alpha:]]+)/log.\d+}) {
  my $word = $1;
  qx(echo cp $log $oldir/$word);
}
  }
}


# This might come closer to what you want.

foreach my $log (@NBlogs2) {
  if ($log =~ m{([[:alpha:]]+)/log.\d+}) {
my $word = $1;
my $number = $subdir_for{$word};
qx(echo cp $log $oldir/$number);
  }
}

I decided to echo the command rather than to execute it. ALL 
CODE UNTESTED.



thank you
derek



You're welcome.



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: hash lookup table

2006-08-28 Thread John W. Krahn
Derek B. Smith wrote:
> 
> --- "John W. Krahn" <[EMAIL PROTECTED]> wrote:
> 
>>Derek B. Smith wrote:
>>
>>>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'?
> 
> No I do not want to do thisnice catch. It should
> read /bpcd\/log|bpdbm\/log|bptm\/log\.\d+/);
> so that I catch log and a period and number after the
> period.

If you want all the logs to end with '\.\d+' then you need something like:

m!bp(?:cd|dbm|tm)/log\.\d+!



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: hash lookup table

2006-08-29 Thread Derek B. Smith


> 
> # This might come closer to what you want.
> 
> foreach my $log (@NBlogs2) {
>if ($log =~ m{([[:alpha:]]+)/log.\d+}) {
>  my $word = $1;
>  my $number = $subdir_for{$word};
>  qx(echo cp $log $oldir/$number);
>}
> }
##

I used Data::Dumper to test this code and am wondering
why the scalar variable assign. my $number is not
working or "sticking"???  See below

foreach my $log (@twoweekdir_contents) {
  $NBlogs2[ $i++ ] = $log 
  if ($log =~ /bpcd\/log|bpdbm\/log|bptm\/log\.\d+/);
}
  if (@NBlogs2) {
foreach my $logs (@NBlogs2) {
   if ($logs =~ m{([[:alpha:]]+)/log\.\d+}) {
  print "word variable assgn:\t",
  Dumper(my $word = $1);
 
  print "number assgn from hash:\t",
  Dumper(my $number = $subdir_for{$word});
  
  print "word output:\t",
  Dumper($word,"\n");

  print "number output:\t",
  Dumper($number,"\n");
 
  print "copy call:\t",
  Dumper(qx(echo cp $logs $oldir/$number) );
   }
   }
}
else {
 print LOG dateme(),
 "\nArray NBlogs2 is null $!";
 die $!;
}

__END_CODE__


If I change $number to $word is obviously does the
copy correctly, but what is the point of 
my $number = $subdir_for{$word};

???

__CODE_OUTPUT__

word variable assgn:$VAR1 = 'bpcd';
number assgn from hash: $VAR1 = 8;
word output:$VAR1 = 'bpcd';
$VAR2 = '
';
number output:  $VAR1 = 8;
$VAR2 = '
';
copy call:  $VAR1 = 'cp
/usr/openv/netbackup/logs/bpcd/log.082906
/usr/openv/logs/old/8
';
word variable assgn:$VAR1 = 'bpdbm';
number assgn from hash: $VAR1 = 13;
word output:$VAR1 = 'bpdbm';
$VAR2 = '
';
number output:  $VAR1 = 13;
$VAR2 = '
';
copy call:  $VAR1 = 'cp
/usr/openv/netbackup/logs/bpdbm/log.082906
/usr/openv/logs/old/13
';
word variable assgn:$VAR1 = 'bptm';
number assgn from hash: $VAR1 = 39;
word output:$VAR1 = 'bptm';
$VAR2 = '
';
number output:  $VAR1 = 39;
$VAR2 = '
';
copy call:  $VAR1 = 'cp
/usr/openv/netbackup/logs/bptm/log.082906
/usr/openv/logs/old/39


thank you
derek


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: hash lookup table

2006-08-29 Thread Mumia W.

On 08/29/2006 01:46 PM, Derek B. Smith wrote:

[...]
If I change $number to $word is obviously does the
copy correctly, but what is the point of 
my $number = $subdir_for{$word};


???



Forget it. Do what works. :-)




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]