Re: recursive search

2005-12-02 Thread Jeff 'japhy' Pinyan

On Dec 2, The Ghost said:

I want to know how many new line chars there are in all files in a directory 
(and it's subdirectories).  What's the best way?


You'll want to use File::Find (a standard module) to do your directory 
recursion for you.  For each file you get to, open it, count its newlines, 
and add that to your running total.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Interpolating variables in a string from a file

2005-12-01 Thread Jeff &#x27;japhy&#x27; Pinyan

On Dec 1, Andrew Brosnan said:


my $var = 'world';
my $data = ;

__DATA__
hello $var


Read 'perldoc -q expand'.

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Count number of times matched

2005-12-01 Thread Jeff &#x27;japhy&#x27; Pinyan

On Dec 1, SG Edwards said:

If I want to count the number of times that a match occurs in a line is there 
a way of doing this, if there is I have not found it!



$line="This is a sentence about matching sentences.\n";
$line=~/sentence/ig;

So I will have matched "sentence" twice and I want to record this.


No, you haven't.  A pattern match in scalar context only matches ONCE. 
What you want to do is:


  my @matches = $line =~ /($pattern)/ig;

Assigning the return value of the pattern match to an array means that the 
pattern match is executed in LIST context (instead of scalar context), so 
it will match as many times as it can (due to the /g modifier).


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: formatting text

2005-11-29 Thread Jeff &#x27;japhy&#x27; Pinyan

On Nov 29, Ing. Branislav Gerzo said:


I'd like to know if there is module for following:


Yes, Perl6::Form.  It's a Perl 5 implementation of Perl 6's formats.


112
12345678901234567890
=== OUT ===
| This is just |
| small sentence   |
| about nothing.   |
=== OUT ===

So, I'd like to define "|" as start and as end, word-wrapped text,
and length of line is lets say "20" chars.


You could use Perl 5's formats, but they're ugly to work with. 
Perl6::Form presents them in a simpler and better fashion.


  use Perl6::Form;

  print form
"| {} |",
   $string
  ;

does what you want.  See the module's documentation for details.

You'll have to download the module from CPAN, since I highly doubt you 
already have it.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: From column to row?

2005-11-28 Thread Jeff &#x27;japhy&#x27; Pinyan

On Nov 28, Andrej Kastrin said:


I suppose that above example is not good one; here is the real one:
Mark
Francesco
Ann
Robert

transform to: "Mark","Francesco","Ann","Robert"



while ()
{
my($line) = $_;
if ($line =~ $) #match to end of line


You don't need a regex here at all, and even if you did, the contents of 
the regex need to be inside /.../.



 {
print "$line"; #how to make e.g. "Mark",
 }
}


In your case, you want to do something like:

  while () {
chomp;  # to remove the newline at the end of the string in $_
print qq{"$_",};
  }

The qq{...} is a double-quoted string; that is, it's the same as "..." 
except that it doesn't use " to delimit the string, but rather { and }.


This isn't a perfect solution for you, though, because it keeps a trailing 
comma that I'm quite sure you don't want.  But this is a start for you.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Pattern Matching Question

2005-11-28 Thread Jeff &#x27;japhy&#x27; Pinyan

On Nov 27, Dax Mickelson said:


I am having problems matching ALL possible matches of a string against
another (very large) string.  I am doing something like:  @LargeArray =
($HugeString =~ m/$Head/ig);  Where $Head is an 8 character
string.  (Basically I want to get all 16 character long substrings out of
$HugeString where the first 8 characters match $Head.)

My problem comes about when (for example) I want to match a 16 character
string that starts with .  Suppose $HugeString=AASDFGHJKL
and $Head=  I want @LargeArray[0]=AASDFGHJ,
@LargeArray[1]=ASDFGHJK, and @LargeArray[2]=SDFGHJKL


So you want to get OVERLAPPING matches, is that correct?  If so, your 
problem can be solved with a "look-ahead" assertion:


  my @matches = $string =~ /(?=($pattern.{8}))/g;

The look-ahead matches without advancing in the string.

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: 15 Million RAW

2005-11-25 Thread Jeff &#x27;japhy&#x27; Pinyan

On Nov 25, Lorenzo Caggioni said:


I made some changes in the program (delete eval, edjust subs... )

Now the program takes less then 3 sec but it loses all the structure...

The main thing that increase performance is delete the eval("fun name").
I do it in this way because the name of the function is retrived from a
database.
is there another way to recal a function retrining his name from a variable?


Yes, it's called a dispatch table:

  my %functions = (
abc => \&do_this,
def => \&do_that,
ghi => \&do_something_else,
  );

Those \&... things are REFERENCES to functions.  So you do:

  while (my @row = get_stuff_from_database()) {
# assuming $row[0] is abc or def or ghi
# that is, $row[0] holds the nickname of the function
my $code = $functions{$row[0]};

$code->(@arguments);
  }

So when $row[0] is 'abc', we call do_this(...).  Etc.

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: transforming numerous lists to a hash

2005-11-24 Thread Jeff &#x27;japhy&#x27; Pinyan

On Nov 24, Alexandre Checinski said:


I have lists that looks like this :
(A,B,C,1,5)
(A,B,C,1,2)
(A,B,2,1,2)
(A,B,C,D,1)
(A,B,Y)

And I would like to get something like this :


Here's how to do it without using eval().

  use strict;
  use warnings;
  use Data::Dumper;
  $Data::Dumper::Indent = 1;

  my @lists = (
[qw( A B C 1 5 )],
[qw( A B C 1 2 )],
[qw( A B 2 1 2 )],
[qw( A B C D 1 )],
[qw( A B Y )],
  );

  my %hash;

  for my $path (@lists) {
add_path([EMAIL PROTECTED], \%hash);
  }

  print Dumper(\%hash);

  sub add_path {
my $path = shift;

if (@$path) { add_path($path, $_[0]{shift @$path}) }
else { $_[0] = 1 }
  }

It works because of a nifty Perl behavior:  the elements of @_ (the 
arguments to a function) are *aliases* to the things passed to the 
function.  That means, by altering $_[0], you can alter the first argument 
to a function.


In my code, I've shift()ed the @_ array, so altering $_[0] is actually 
changing the *second* argument to the function.  But anyway.


We send the add_path() function an array reference (the path) and a 
reference to where to store the path.  So for (A, B, C, 1, 5), we do:


  add_path([A,B,C,1,5], \%hash);

The add_path() function checks to see if there are still elements in the 
path arrayref.  If there are, it removes the first part and uses it as a 
key to the hash reference it was given and calls itself again:


  add_path([B,C,1,5], $_[0]{A});

where $_[0] is \%hash, which calls

  add_path([C,1,5], $_[0]{B});

where $_[0] is $hash{A}, which calls

  add_path([1,5], $_[0]{C});

where $_[0] is now $hash{A}{B}, which calls

  add_path([5], $_[0]{1});

where $_[0] is now $hash{A}{B}{C}, which calls

  add_path([], $_[0]{5});

where $_[0] is now $hash{A}{B}{C}{1}...

Now, when the path is empty, instead of calling add_path() again, we set 
$_[0] to 1.


This means that $hash{A}{B}{C}{1}{5} is set to 1.

This can be done WITHOUT a recursive function.  Exercise to the reader.

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: call ext prog - best way

2005-11-24 Thread Jeff &#x27;japhy&#x27; Pinyan

On Nov 24, Gary Stainburn said:


I'm writing a script to be called from exim to allow multiple virus
scanners to be used, and I'm wondering what the best way to do it is.

My script as it stands is below, and I'm using qx{}, to capture the
output, but want to also capture the called program's exit code.


The exit code is in the $? variable.  You'll need to right-shift it 8 
places to get the value you want (see perldoc -f system).



#!/bin/perl -w


You've got warnings on (albeit via -w instead of 'use warnings'), but 
you're not turning strictures on (via 'use strict').  It's strongly 
suggested.



my %engines={'sweep'=>{'cmd'=>'/usr/local/bin/sweep -all -rec',


If you've got warnings turned on, surely you should've seen that you're 
assigning a hash REFERENCE to a hash.  That is, you're doing:


  my %engines = { ... };

where you mean to say

  my %engines = ( ... );

You need parentheses there, not curly braces.  The OTHER curly braces are 
appropriate.



  'viri'=>3,
  'regex'=>'found'},


The benefit of using the => operator is that the left-hand side doesn't 
need to be quoted if it's a simple bareword.



'clamscan'=>{'cmd'=>'/usr/bin/clamscan --quiet --stdout -r
--no-summary -i',
 'viri'=>1,
 'regex'=>'found'}
};


Which would render:

  my %engines = (
sweep => {
  cmd => '/usr/local/bin/sweep -all -rec',
  viri => 3,
  regex => 'found',
},
clamscan => {
  cmd => '/usr/bin/clamscan --quiet --stdout -r --no-summary -i',
  viri => 1,
  regex => 'found',
},
  );


chdir $ARGV[0] || die "$0: cannot chdir: $!\n";


I fear that the || binds too tightly here, and your code is parsed as:

  chdir( $ARGV[0] || die "..." );

which means "chdir to either $ARGV[0] or the return value of die(...) if 
$ARGV[0] is false".  Now, as it turns out, I'm wrong (in this particular 
case).  But this problem will bite you in other circumstances.  Therefore, 
to safeguard yourself, either parenthesize specifically, or avoid trouble 
by using the loosely-binding 'or' operator:


  chdir($ARGV[0]) || die ...;
  chdir $ARGV[0]  or die ...;


foreach my $engine (%engines) {


You're looping over the keys AND values of the hash.  You mean to say

  foreach my $engine (keys %engines) { ... }


 my @output=qx{$engine->{'cmd'} .};
# check for result code to indicate success/failure/virus found etc
# if virus, return lines reporting it from @output
# use map/grep/whatever to extract lines.
}


That's all I have to say about that.

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: regex mutil-line matching

2005-11-23 Thread Jeff &#x27;japhy&#x27; Pinyan

On Nov 23, Dermot Paikkos said:


Looking at the mail queue on my smtp server I notice the usual amount
of crap. The mailq output is in this format:

11h  8.5K 1EeoEz-0003t2-00 <>
 [EMAIL PROTECTED]

10h   10K 1EepGl-0004VH-00 <>
 [EMAIL PROTECTED]

11h  8.4K 1EeoUI-0004YR-00 <>
 [EMAIL PROTECTED]

In case the formatting doesn't make it, that's a newline after '<>'
and a newlines between each (two-line) record.


Well, if you read the file one line at a time, you'll never have BOTH a 
message-ID AND an email address in a string at the same time.  You'll need 
to alter your input record seperator:


  local $/ = "";  # matches on 2 or more newlines in a row

Or, from the command-line:

  perl -00 ...


mailq | perl -e 'while (<>) {print $1, "\n" if /([\d+|\w+]-[\d+|\w+]-
00).*porn/m }'


Your regexes are funky.  First of all, [...] is for CHARACTERS.  [\d+|\w+] 
is the same as [\d\w|+] which is the same as [\w|+] anyway, since \w 
includes \d.


So you could do:

  perl -00 -ane 'print $F[2] if $F[4] =~ /porn/' ...

The -a switch autosplits $_ into @F on whitespace.

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Replacing a Bracketed String with "N"

2005-11-22 Thread Jeff &#x27;japhy&#x27; Pinyan

On Nov 22, Edward Wijaya said:


$str1  = "ATC[TG]CC";
# into
$ans_str1  = "ATCNCC";

$str2="ATC[TG]CCGC[ACTG]";
#into
$ans_str2="ATCNCCGCN";


The simplest way I can think of is

  $string =~ s/\[[ACTG]+\]/N/g;

which is very explicit:  it replaces a '[' followed by A's, C's, T's, 
and/or G's, followed by a ']' with 'N'.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: rearranging a string

2005-11-21 Thread Jeff &#x27;japhy&#x27; Pinyan

On Nov 21, [EMAIL PROTECTED] said:


In my situation I have a string like so:

my $string = '20050516';

and I want to rearrange it to look like: 05162005, so I tried:

my $newstring =
substr($string,0,8).substr($string,0,4).substr($string,8,0);


That should be:

  substr($string, 4, 2) . substr($string, 6, 2) . substr($string, 0, 4)

The first argument is the OFFSET, and the second argument is the LENGTH.

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: LWP package problem

2005-11-17 Thread Jeff &#x27;japhy&#x27; Pinyan

On Nov 17, Pine Yan said:


#!/home/gnu/bin/perl

use LWP;

my $browser = LWP::UserAgent->new;

my $url = 'http://www.google.com';

my $response = $browser->get($url);


You didn't load the LWP::UserAgent module, though.  You loaded LWP, not 
LWP::UserAgent.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: help explaining for this script

2005-11-16 Thread Jeff &#x27;japhy&#x27; Pinyan

On Nov 16, Paul Johnson said:


On Tue, Nov 15, 2005 at 06:48:40PM -0500, Jeff 'japhy' Pinyan wrote:


whereas if $_[1] is tainted, then the eval { ... } returns false since a
fatal error is raised because

  eval 1 . substr($_[0], 0, 0)

is illegal if $_[0] is tainted.


I would be wary of even this solution.  The backwards compatibility
police would probably catch it, but someone might say that a zero length
string could never be tainted and then this code would break.


I would be very annoyed at the person who made that decision.


I would go with Scalar::Util::tainted().


Certainly.  And hooray, as of 5.8, Scalar::Util is a core module.  Huzzah.

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: help explaining for this script

2005-11-15 Thread Jeff &#x27;japhy&#x27; Pinyan

On Nov 15, Jennifer Garner said:

I can't understand for this script below,I want somebody here to give me 
some help.Thanks.


sub is_tainted{
   my $var=shift;
   my $blank=substr($var,0,0);
   return not eval {eval "1 || $blank" || 1};
}

That subroutine estimate for if some given var is tainted or not.But I 
can't know how it works.


If a variable is tainted, then any substring of that variable is also 
tainted.  In addition, it is illegal to eval() any string that is tainted. 
The inclusion of a tainted string inside another string makes that whole 
string tainted.


Therefore:  if $var is tainted, the $blank will also be tainted (even 
though it's a substring of zero characters).  If $blank is then tainted, 
then the code


  eval { eval "1 || $blank" || 1 }

will return false (since the eval { ... } catches fatal errors, and the 
eval "1 || $blank" raises a fatal error because $blank is tainted), and 
therefore,


  return not eval { eval "1 || $blank" || 1 };

returns true, stating that $var is indeed tainted.  If $var wasn't 
tainted, then


  eval "1 || $blank" || 1

returns 1, and

  return not eval { 1 }

returns false, stating that $var wasn't tainted.

*whew*

Frankly, I find the 'eval "1 || $blank" || 1' silly, since the whole 
reason the '... || 1' is needed is since $blank is a blank string and the 
code '1 || ' is invalid Perl.  Long story short, I'd have written:


  sub is_tainted {
return not eval { eval 1 . substr($_[0], 0, 0) };
  }

It's much more concise.  If $_[0] isn't tainted, then

  not eval { eval 1 . substr($_[0], 0, 0) }
  ->
  not eval { eval 1 }
  ->
  not eval { 1 }
  ->
  not 1
  ->
  false

whereas if $_[1] is tainted, then the eval { ... } returns false since a 
fatal error is raised because


  eval 1 . substr($_[0], 0, 0)

is illegal if $_[0] is tainted.

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: tricky problem about stat function

2005-11-13 Thread Jeff &#x27;japhy&#x27; Pinyan

On Nov 13, ZHAO, BING said:

opendir GOP, "SCRATCH/BACKUP" or die "cannot open directory 
SCRATCH/BACKUP:$!";

my @smm=grep{($_ ne ".") && ($_ ne "..")} readdir GOP;
foreach (@smm){
   my($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime,
$mtime, $ctime, $blksize, $blocks) = stat($_);
   print $ctime;
} closedir GOP;


This piece of code WORKS when I point opendir GOP to the current directory. 
But it DOESN"T work when I point to the subdirectory SCRATCH/BACKUP within the 
current directory.
It's tricky. And " print $_  " successfully prints out the files in 
SCRATCH/BACKUP dir.


It prints the NAME of the file.  But that file doesn't exist in the 
current directory, it exists in the SCRATCH/BACKUP directory.  You need to 
prepend the directory path to the filename:


  foreach (@files) {
    my $ctime = (stat "SCRATCH/BACKUP/$_")[10];
  }

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Is it possible with RegEx

2005-11-09 Thread Jeff &#x27;japhy&#x27; Pinyan

On Nov 10, Gilles said:


I try do to a < simple > thing :

Knowing If a string like "13" exist in a string like "123"

Or if "37" exist in "12356789"


You're not looking for '13', you're looking for '1..3'.  The simplest 
way to do this is:


  if ("123" =~ /1.*3/) {
print "found 1...3\n";
  }

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: What am I doing wrong?

2005-11-08 Thread Jeff &#x27;japhy&#x27; Pinyan

On Nov 8, jean said:


The double print was produced by:
foreach (my @result = $sth->fetchrow_array) {
I've changed this into:
my @result=$sth->fetchrow_array;
foreach ($result[0]){


There's really no need for a 'foreach' loop there at all.


I've been looking at my variable declaration following Jeff's remarks. I
thought
my (@sku, @qty, @t_qty);
could be palaced after
for my $i(1 .. $n) {
since I only need them in that part of the program. However when I do so I get


Just because you only use them inside that for loop doesn't mean they can 
be DECLARED in there.  They must be declared OUTSIDe that loop because you 
need the arrays to be built up over time -- if you declared them inside 
the loop, they'd be reset to () for each value of $i.



my current code is

#!/usr/bin/perl
#
#   webstore-rijselect-6-werkt.pl   #
#
use warnings;
use strict;

use integer;
use DBI;

my ($dbh, $sth, $n, @sku, @qty, @t_qty);


I'd love the array declarations a little below:


$dbh = DBI->connect('dbi:mysql:DB','U','PW',  {
  PrintError => 1,
  RaiseError =>1
});


my $row_total = 'SELECT num FROM nra_slim';
$sth=$dbh->prepare($row_total);
$sth->execute;
$sth->fetchrow_array();
$n = $DBI::rows;


Here is where I'd put them.  Yes, it's the same scope, but declaring them 
here is a VISUAL tool to you and anyone else who sees your code.


  my (@sku, @qty, @t_qty);


for my $i(1 .. $n) {
my $sku_compare = 'SELECT sku_srs, aantal FROM nra_slim WHERE num = ?';
$sth=$dbh->prepare($sku_compare);


Those two lines above can go outside this loop; there's no need to prepare 
the same query over and over again.



$sth->execute($i);
my @result = $sth->fetchrow_array;
foreach ($result[0]) {


Get rid of the line above (and its matching '}' below).


$sku[$i]=$result[0];
$qty[$i]=$result[1];
$t_qty[$i]=0;
if ($i < 2) {
$t_qty[$i]=$qty[$i];
}
else {
if ($sku[$i]==$sku[$i-1]){
$t_qty[$i]= $t_qty[($i-1)] + $qty[$i];
if($i==$n){
print "$sku[$i]",', totaalaantal is ', 
"$t_qty[$i]\n";


I'd just print one string:

  print "$sku[$i], totaalaantal is $t_qty[$i]\n";

And so on, for your other print() statements.  This is just so much easier 
to look at than a bunch of strings with different quotes and commas 
between them.



}
}
else {
$t_qty[$i]=$qty[$i];
if($i==$n){
print "$sku[$i-1]",', totaalaantal is ', 
"$t_qty[$i-1]\n";
print "$sku[$i]",', totaalaantal is ', 
"$t_qty[$i]\n";
}
else {
        print "$sku[$i-1]",', totaalaantal is ', 
"$t_qty[$i-1]\n";
}
}
}
}


I believe the } above is the one matching your foreach statement.


}

$sth->finish;
$dbh->disconnect;


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: hash of hash - copy

2005-11-04 Thread Jeff &#x27;japhy&#x27; Pinyan

On Nov 4, Frank Bax said:


$aSuit{0}{'a'} = 'A';
$aSuit{0}{'b'} = 'B';
$aSuit{0}{'c'} = 'C';
$aSuit{1}{'a'} = 'D';
$aSuit{1}{'b'} = 'E';
$aSuit{1}{'c'} = 'F';

Now I want to make $aSuit{1} a copy of $aSuit{0}
%aSuit{1} = %aSuit{0};


Well, $aSuit{1} = $aSuit{0} would make $aSuit{1} and $aSuit{0} IDENTICAL. 
Meaning, when $aSuit{0}{a}, $aSuit{1}{a} changes too.  If you don't want 
that, you can do:


  %{ $aSuit{1} } = %{ $aSuit{0} };

But that only works one level deep.  For generic copying of data 
structures, see the Storable module (comes with Perl):


  use Storable;
  $aSuit{1} = dclone($aSuit{0});

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Dates

2005-11-04 Thread Jeff &#x27;japhy&#x27; Pinyan

On Nov 4, Scott Taylor said:


I have a date in UT that looks like this:
"2005-11-02 01:36:00.0"

Can anyone tell me the best way to subtract 8 hours from it, or to convert
it to PDT?

I'm reading up on Date::Manip, but I don't know if that is going to help,
yet.


Date::Manip can probably do it, and it can also cook you a three-course 
meal and help you find your wallet.  For a slightly lighter-weight 
solution, consider the simple Date::Parse (which comes with Perl).  It can 
handle the format you've provided:


  use Date::Parse;
  my $gmtime = str2time("2005-11-02 01:36:00.0", "UTC");

Now you have the number of seconds in $gmtime.  Subtract 8 hours by 
subtracting 60*60*8 seconds from that.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: conditional use()

2005-11-04 Thread Jeff &#x27;japhy&#x27; Pinyan

On Nov 4, JupiterHost.Net said:

I'm beating my head against the wall trying to remember where I saw this 
/ how to do this:


In some documentation I remember seeing a use statement that had some sort of 
condition.


  use if ...;

perldoc if

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




RE: What am I doing wrong?

2005-11-04 Thread Jeff &#x27;japhy&#x27; Pinyan

#!/usr/bin/perl
#
#   webstore-rijselect-5-werkt.pl 
#

use warnings;
use strict;

use integer;
use DBI;

my ($dbh, $sth, $sta, $sql_een, $sql_twee, $i, $n, @sku, 
@qty, @t_qty);


You've just made a bunch of "global" variables.  Sure, they're lexicals, 
but you've declared them all to exist in the uppermost scope of your 
program.  Surely they don't all deserve such wide range, do they?



# create a statement in $sth
$dbh = DBI->connect('dbi:mysql:webstore-2','gabala','',  {
   PrintError => 1, ### rapporteer fouten via warn()
   RaiseError =>1  ### rapporteer fouten via die() });


Unless this was a code-wrapping error, you've got a problem here.  Your 
second comment hides the '});' part of your code.


  $dbh = DBI->connect('DSN', 'user', 'pass', {
PrintError => 1,  # comment
RaiseError => 1,  # comment
  });

$sql_een = 'SELECT num FROM nra_produkten'; # prepare a SQL 
query with placeholder $sth=$dbh->prepare($sql_een); 
$sth->execute; $sth->fetchrow_array(); $n = $DBI::rows;


Ugh.  More wrapping errors.

  $sql_een = 'SELECT num FROM nra_produkten';  # NO PLACEHOLDERS!!!
  $sth = $dbh->prepare($sql_een);
  $sth->execute;
  $sth->fetchrow_array;
  $n = $DBI::rows;


for ($i = 1; $i <= $n+1; ++$i ) {


Why $n + 1?  I'd think 1 .. $n is sufficient.

  for my $i (1 .. $n) {


  $sql_twee = 'SELECT sku_srs, aantal FROM nra_slim WHERE num = ?';


I don't speak your native tongue, but have you just named two variables 
$sql_een and $sql_twee, meaning $sql_1 and $sql_2?  If so, that's pretty 
dwaas.  Give you variables more descriptive names!



  $sth=$dbh->prepare($sql_twee);
  $sth->bind_param(1, "$i");


You don't need to quote $i here.


  $sth->execute;


And you could've just done:  $sth->execute($i).  That would've used $i to 
fill in the placeholder.



  foreach (my @result = $sth->fetchrow_array) {


That is certainly incorrect.  I think you just want to do:

  my @result = $sth->fetchrow_array;

and dispense with the loop entirely.  Otherwise, you're running the same 
code TWICE (once for each element in @result).



1989, totaalaantal is 5
1989, totaalaantal is 5
4121, totaalaantal is 1
4121, totaalaantal is 1


Yup.  Tweemaal.

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: What kind of structure to choice

2005-11-03 Thread Jeff &#x27;japhy&#x27; Pinyan

On Nov 3, Gilles said:


These lists will decrease during processing and will normally contain one
element, so to know if a list is definitely treated I need a flag.


Why?  You can find the size of an array like so:

  $size = @array;

In the case of an array reference, it's merely

  $size = @{ $aref };

What are you doing that leads you to believe you'll need a special marker 
of some sort to indicate that an array has been processed?


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: regex issues

2005-11-01 Thread Jeff &#x27;japhy&#x27; Pinyan

On Nov 1, Michael Gargiullo said:


I'm in the process of writing a rather complex parser script to parse
nessus output, can be anything from:

results|192.168.1|192.168.1.131|https (443/tcp)|10330|Security Note|A
web server is running on this port through SSL\n

to

results|192.168.1|192.168.1.131|https (443/tcp)|10863|Security Note|Here

[snip]

accepts TLSv1 connections.\n\n

To make a long story short (to late), I have a mysql table with two
columns; start and end dynamic content.  "Start" marks the beginning of
data specific to this host, and "end", it's opposite.



 my $splito=$dynparsparts[0];
 my $splitt=$dynparsparts[1];
 my @parts=split(/$splito/,$problem);

The line above fails halfway through with the following error:

Quantifier follows nothing in regex; marked by <-- HERE in m/* <-- HERE
seems* to be / at ./import-nessus-scan.pl line 116.


This means $splito (the string you're using as a regex) is a malformed 
pattern.  If you don't want $splito to be interpreted AS a regex, you need 
to quotemeta() it:


  my $splito = quotemeta($dynparsparts[0]);
  ...
  my @parts = split /$splito/, $problem;

or to keep $splito normal, you can just use \Q...\E in the regex:

  my $splito = $dynparsparts[0];
  ...
  my @parts = split /\Q$splito\E/, $problem;

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: finding the first non digit character in a string

2005-10-28 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 28, Bowen, Bruce said:


I see where you can test for a match in a string of data using the If ( )
statement, but can you use it with an 'index' statement?


The index() function is for finding a string in another string.  Patterns 
(regexes) are not allowed.



$DD = "5000|SIHHTEXT"

I've tried $d = index($DD, m/[^\d]/);
   $d = index($DD, /[^\d]/);
   $d = index($DD, [^\d]);


You want the location of the first non-digit?

  my $non_digit = ($DD =~ /\D/) ? $-[0] : -1;  # -1 means not found

(See 'perlvar' for an explanation of $-[0], in the @- array.)


will be or if there will be any cents.  I need to extract the dollar / cents
out from the text.  Any suggestions as to how that can be done?


That's much simpler:

  my ($money) = $DD =~ /^(\d+\.?\d*)/;

That will match the numbers at the beginning of the string, optionally 
allowing for a decimal point (\.) and digits after it.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: help slurping a file-- Solved -- Thanks for responses

2005-10-28 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 28, Renee Halbrook said:


My perl interpreter display does not recognize "\r" for a newline character
for standard out, so it simply printed the same line over on top of the
previous line, making it look like it was only reading one line total.
The slurping was working fine, but the display was not what I anticipating.


That's not Perl's issue.  That's your terminal's issue.  And it's not an 
"issue", because that's what \r is supposed to do.  \r is a carriage 
return, which only means the cursor is brought back to the beginning of 
the line.


(Except maybe on Macs, I don't know.  That's weird.  I don't use a Mac, 
though, so I can't be sure.)


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: search and replace in html text

2005-10-28 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 28, Matthias Leopold said:

i want to search for a pattern in html text and replace only those occurences 
that are not enclosed inside <> (html tag, not perl operator).


It's easiest to use a real HTML parser.  Otherwise, you'll probably get 
false positives and what-not.



$string =~ s/$pattern/test for <> or similar; else replace/ge;



sounds like an easy task but i haven't managed to accomplish it yet.


Parsing HTML (which really is what you're looking to do) always sounds 
easy until you see how it's done correctly.  Then you feel better off 
using a module to do it for you.  (Guilt-free, I might add.)


I'd suggest going to search.cpan.org and looking for HTML:: which should 
yield several matches.  I think HTML::TokeParser (or 
HTML::TokeParser::Simple) would be your best bet here.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: How to decode raw G3 data to plain txt file

2005-10-26 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 26, J.Peng said:


It's the unix 'file' command show:

file 1_zhangcha_01_1000v+40.mbox
1_zhangcha_01_1000v+40.mbox: raw G3 data, byte-padded


According to my /usr/share/misc/file/magic file and Google, "raw G3 data" 
is a raw fax transmission.  As for parsing this format, I have absolutely 
no idea.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: regular expression match problem

2005-10-25 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 25, Pine Yan said:


This is a segment of code to do string search:

my $email = "\pineyan";
my $name = "\\pine";

if($email =~ /($name)/) {
   print "Found my name: $name!\n";
}

and I got the following error when running:

Can't find unicode character property definition via main->i or
i.pl at unicode/Is/i.pl line 0


Perl sees your regex as /(\pine)/, and \pi is a shortcut for \p{i} which 
means "Unicode property 'i'".  To get around this, do:


  if ($email =~ /(\Q$name\E)/) { ... }

The \Q...\E auto-quotes any regex-related characters, so that they're 
matched literally.


P.S. My last name is Pinyan (awfully close to your full name).  It's 
pronounced the same way, I'm told, as "pinyin", the process by which 
Chinese characters are transliterated into English "words".


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Module subclassing (solved)

2005-10-24 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 24, Peter Rabbitson said:


I am trying to subclass Math::Currency to change the default bstr() and
move it to a different function, so my numbers are not formatted by
default (I am unable to just strip existing formatting in a certain
situation).
...
However any arithmetics fails with an "Unknown round mode ''" error.


Crap, it was all written in the POD of Math::BigInt under Subclassing.
Adding the following globals to the class package solved the problem:

our $accuracy = undef;
our $precision = -2;
our $round_mode = 'even';
our $div_scale = 40;


That's poor planning, in my opinion.  Those should be set up as methods of 
the object. :(


The idiom:

  package Class::Subclass;
  use base 'Class';
  1;

should be universal, no?

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Troubles getting a value out of an XML object

2005-10-24 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 24, Dave Adams said:


my $xml = new XML::Simple;
my $data = $xml->XMLin($ResponseDoc);



print Dumper($data->{CreateLock});



$VAR1 = { 'DatabaseId' => 'db1', 'RecordNumber' => '23617', 'Id' => 'test'
};



*Question:* How do I get the value of RecordNumber?


You knew how to get 'CreateLock' from $data, but you don't know how to get 
'RecordNumber' from $data->{CreateLock}?


  my $rec = $data->{CreateLock}->{RecordNumber};

  # or, without the extra ->
  my $rec = $data->{CreateLock}{RecordNumber};

The object is merely a hash of hashrefs (of hashrefs, etc.).

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: map/array performance

2005-10-23 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 23, Frank Bax said:

   my $aval=''; map { $aval=$aval.sprintf("%4d",$aSuit{$a}{$_}); } 
@f_seq;


   my $aval=''; foreach $f (@f_seq) { 
$aval=$aval.sprintf("%4d",$aSuit{$a}{$f}); }


You should be using $aval .= here, instead of $aval = $aval .   And as 
John has shown, join() is even better.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: map/array performance

2005-10-23 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 23, Frank Bax said:


At 02:11 PM 10/23/05, John W. Krahn wrote:


Frank Bax wrote:

> my $aval=''; map { $aval=$aval.sprintf("%4d",$aSuit{$a}{$_}); } 
@f_seq;
> my $bval=''; map { $bval=$bval.sprintf("%4d",$aSuit{$b}{$_}); } 
@f_seq;


You shouldn't use map in void context, you should use a foreach loop 


What is "void context"?


"Void context" means an expression whose return value is being discarded.

  $x = foo();  # scalar context
  @y = foo();  # list context
  foo();   # void context

map() builds a list to be returned, and by not USING its return value 
(that is, calling map() in void context), you're wasting resources.  If 
you do


  map BLOCK LIST

and don't intend on saving the return value of map(), just use a for loop.

  for (LIST) BLOCK

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




getting authors' names right (was Re: Project planning)

2005-10-22 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 23, Alfred Vahau said:

The newly released book "Perl Best Practices" by Dr. Damien Conway may be 
helpful in addition to those recommended already.


Granted, I'm not an author, and I rarely (if ever) see my name misspelled, 
but as a matter of courtesy, I'd really appreciate seeing Damian Conway's 
name spelled properly, and Randal (L.) Schwartz's name spelled properly, 
etc.  I see "Damien" and "Randall" constantly.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: killing a program

2005-10-21 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 21, Christopher Spears said:


I've been working on a script that will allow me to
kill a certain program using the program's PID.



sub findPID {
   if (my $PID = `pgrep PROGRAM`) {
   return $PID;
   } else {
   return 0;
   }
}


pgrep CAN return more than one PID, but it also returns them with newlines 
at the end.


  sub findPID {
if (my @results = `pgrep PROGRAM`) {
  chomp @results;
  return wantarray ? @results : $results[0];
}
else { return }
  }

Then you can do:

  my @pids = findPID();
  # or
  my $pid = findPID();


   kill 1,$fPID;


That's sending it a HUP signal.  Do you expect PROGRAM to die when it 
receives the HUP signal?



   kill 9,$fPID1;


NOW it should die, since 9 is KILL.

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Removing specific lines from a File

2005-10-21 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 20, Rose, Jeff said:


Basically what I am trying to do here is parse through an email file
grab the the basics, from/to/subject put those in a small text tab
separated database in the format of

File NumRecipients From FromIP Subject Spam-Status

and then pass the contents along to spamassassin pm to check the status 
but the email file contains these lines which mess with spamassassins 
filtering which I have to remove in order to get an accurate spam 
score(using the pm not the daemon don't ask me why :-))



P I 19-10-2005 21:35:00    < [EMAIL PROTECTED] >
O T
A domain.com [123.12.123.1]
S SMTP [IP ADDRESS]
R W 19-10-2005 21:35:00   _FY_ [EMAIL PROTECTED]
R W 19-10-2005 21:35:00   _FY_ [EMAIL PROTECTED]
R W 19-10-2005 21:35:00   _FY_ [EMAIL PROTECTED]
R W 19-10-2005 21:35:00   _FY_ [EMAIL PROTECTED]


You're on the right track, but I think you're doing far too much with the 
regex when you should really just split each line on whitespace and deal 
with it like that.


  my @records;

  while () {
my @fields = split;

# sender
if ($fields[0] eq 'P') {
  push @records, [ { SENDER => $fields[-2] } ];  # $fields[-1] is '>'
}

# recipient
elsif ($fields[0] eq 'R') {
  push @{ $records[-1]{RECIPIENT} }, $fields[-1];
}

# SMTP
elsif ($fields[0] eq 'S') {
  ${ $records[-1] }{SMTP} = $fields[-1];
}

# etc.
  }

Now you have an array, @records, whose elements are hash references. 
Here's what it's like:


  @records = (
# email 1
{
  SENDER => '...',
  RECIPIENT => [ '...', '...' ],
      SMTP => '...',
  # whatever other fields you want
},

# email 2
{ ... },
  );

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: HTAB, VTAB in a terminal?

2005-10-20 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 20, Bryan R Harris said:


Curses is a CPAN module, correct?

I have an office-full of users here, most of which will not not have that
module installed on their workstations.  What's the best way to do something
like that?  Can I have my perl script install that module on their machines?
Or can I simply embed that module within my script somehow?


It's not that easy.  Curses is a wrapper around a bunch of C functions and 
what-not.  I don't know what the simplest non-Curses way of controlling a 
terminal is.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: HTAB, VTAB in a terminal?

2005-10-20 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 20, Bryan R Harris said:


I'd like to write a perl script to do very simple bitmap editing.  The part
I can't figure out is how to write text into the terminal at any x,y point.
Is this possible?

e.g. how can I write an "*" at 10 columns over, 5 rows down in the current
terminal?


The Curses module is probably a good starting point.  It gives you control 
over the terminal displays.


Curses!  Foiled again!

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Carriage Return and LineFeeds Question

2005-10-20 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 20, Dave Adams said:


I am having troubles understanding how perl sees carriage returns and line
feeds. I am not sure if it is a windows and unix thing.

*Scenario:* I have a HTML form that uploads a text file (via a POST) to a
unix server who then passes it to a perl cgi. This cgi parses through the
file, puts it in an array and if it encounters a paragraph break, I want it
to insert "^P".


By "paragraph break", do you mean where a person has pressed enter two or 
more times?  There's no special character for that.  It's just a 
consecutive sequence of newlines.



This is my first paragraph.

This is my Second paragraph.




#!/usr/bin/perl -w
use CGI qw(:standard);
use IO::Handle;
use File::Temp qw /tempfile /;
use Text::Wrap;

my $cgiobject = new CGI;

my $upload_dir = "/temp/";

my $filename_txt = $cgiobject->param("UPLOADFILE");

$filename_txt =~ s/.*[\/\\](.*)/$1/;

open (UPLOADFILE, ">$upload_dir/$filename_txt");
binmode UPLOADFILE;
while ( <$upload_filehandle_txt> )


Where'd $upload_filehandle_txt come from?


{
print UPLOADFILE;
}
close UPLOADFILE;



open(UPLOADFILE, "$upload_dir$filename_txt") || die "problem: $!";


You're missing a / between $upload_dir and $filename_txt.  Oh, wait, no 
you're not, because you've put a '/' at the end of $upload_dir.  That's 
bound to lead to confusion.  I would suggest NOT putting /'s at the end of 
directory names when storing them in variables.  "$dir/$file" looks much 
cleaner than "$dir$file".



my @textfile_contents = ;
close UPLOADFILE;


Why didn't you just create this array as you were reading from the 
uploaded file?



##When perl reads in the scalar to push it on to my array, how do I
##get it to replace the paragraph breaks with ^P?


Do you mean "I want to insert a '^P' where there are two or more newlines 
in a row"?  If so, Perl has a convenient short cut for you.


  open UPLOADFILE, "> $upload_dir/$filename_txt"
or die "cannot write to $upload_dir/$filename_txt: $!";
  binmode UPLOADFILE;

  {
# setting $/ to "" (the empty string) means that Perl will read
# a group of lines at a time, ending when it reaches a sequence of
# newlines.  this is called "paragraph" mode.
local $/ = "";

while (<$upload_filehandle_txt>) {
  # we remove any trailing newlines from the end of the paragraph
  # and record if there were any in the $newlines variable
  my $newlines = chomp;

  print UPLOADFILE "$_\n";
  print UPLOADFILE "^P\n" if $newlines;
}
  }

  close UPLOADFILE;

As an example, here's what the contents of the file being uploaded are:

==
This is a small
paragraph.  It is
not much to talk
about.

Here is a "long" paragraph, but I only mean that the
length of its lines are considerably longer.



And here
is the last
paragraph
which I have
made taller
than all the
other ones.
Remember when
paragraphs were
more than two
sentences long?
==

The resulting file on your machine should look like this:

==
This is a small
paragraph.  It is
not much to talk
about.
^P
Here is a "long" paragraph, but I only mean that the
length of its lines are considerably longer.
^P
And here
is the last
paragraph
which I have
made taller
than all the
other ones.
Remember when
paragraphs were
more than two
sentences long?
==

If this is not what you meant, then please be clearer.

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: net::google problem

2005-10-20 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 20, Ing. Branislav Gerzo said:


use strict;
use warnings;
use Net::Google;

[snip]

my $response = $session->response();



I dumped response, and beginning is:
$VAR1 = [
 bless( {


Stop right there!  See the [ at the beginning of the dumped representation 
of $response?  That means $response is an array reference.  It *holds* 
objects, but it isn't an object itself.


  my $response = $session->response();

  for my $r (@$response) {
print $r->estimatedTotalResultsCount, "\n";
  }

It appears there's a mistake in the module's documentation!  Let the 
author know. :)


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: which perldoc ?

2005-10-19 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 19, Randy W. Sims said:


Jeff 'japhy' Pinyan wrote:

On Oct 19, Brian Volk said:

Is there a perldoc that will list the available arguments and there 
meanings

for perl-one-liners?


Yes.  To try and figure out what perldoc to look at for a given topic, do:

  perldoc perldoc


You mean `perldoc perl` ?  ;)


Oy vay.  Yes.  What a gaffe.

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: which perldoc ?

2005-10-19 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 19, Brian Volk said:


Is there a perldoc that will list the available arguments and there meanings
for perl-one-liners?


Yes.  To try and figure out what perldoc to look at for a given topic, do:

  perldoc perldoc

which will show the names of the documentation sections and a brief 
summary of their purpose.  You should see 'perlrun' listed as the one 
describing the command-line options to perl.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: How to build an executable on Windows

2005-10-19 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 19, kathyjjja said:


$ftp = Net::FTP->new("aaa.bbb.org", Debug =>0) or die "Cannot connect to 
aaa.bbb.org: $@";
$ftp->ascii
$ftp->login("login","passwd") or die "Cannot login", $ftp->message;


You missed the semicolon after '$ftp->ascii'.

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: white space between roam and act

2005-10-19 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 19, K.Moeng said:


The problem got solved by if ($msg =~ /roam\s*act/i)
this will allow you to get the whole word without space or take it with
space.

though when you query you have to put the words under " ".


Well, yes.  Running your program as

  perl myprog.pl roam act

is the same as

  perl myprog.plroamact

both of which create TWO arguments, one 'roam', and the other 'act'.

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: white space between roam and act

2005-10-19 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 19, Suvajit Sengupta said:

To ignore the white space in between ROAM and ACT use /x modifier of Perl RE 
to extend your pattern's legibility by permitting  whitespace and comments. 
Hope that will serve your purpose.

Instead of

if ($msg =~ /roam act/i)

Use

if ($msg =~ /roam act/ix)


You're misunderstanding the /x modifier.  Using /x means that all 
whitespace in your regex is ignored, not that all whitespace in the STRING 
is ignored.  Only "roamact" is matched by /roam act/x, since the " " in 
the regex is ignored due to the /x modifier.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Destroying an object

2005-10-18 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 18, Daniel Kasak said:


I've got an OO object that I want to destroy from inside the object itself.

sub destroy_self {
 my $self = shift;
 $self = undef;
}

But this doesn't work. What is the correct way of doing it?


That's because you're not getting rid of the CONTENTS of the object, 
you're merely undefining a variable that holds a reference to the 
contents.


Ordinarily, you can just wait for the object to go out of scope and it 
will be destroyed by Perl.  However, if you want to do it when you want 
to, here's a sneaky way:


  sub kill_object {
undef $_[0];
  }

Call it as

  $obj->kill_object;

and the object will be destroyed.  The reason this works and '$self = 
undef' doesn't is because the elements of @_ act as aliases; if you 
operate on elements of @_ DIRECTLY, you can affect changes in the things 
passed to the function.  Here's a more general example:


  sub trim {
for (@_) { s/^\s+//, s/\s+$// }
  }

This trim() function doesn't *return* trimmed string, it trims the strings 
you sent it:


  @words = ("  this", "  and  ", " that   ");
  trim(@words);
  print "[EMAIL PROTECTED]";  # [this and that]

Of course, you can't send an ACTUAL string to trim(), because you can't 
modify a constant string:


  trim("  hello  ");  # run-time fatal error

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: XML::XPath query

2005-10-14 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 14, H S said:


Could someone help point out how to use scalar within Xpath query? I need to
use $script in the query but it returns nothing.
I am somewhat confused about the interpolation in this case.



use XML::XPath;

my $xp = XML::XPath->new(filename => '/dssweb/prog/navi/links.xml');
my $script = "/cgi-bin/rates/showrate";
my $nodeset = $xp->findnodes('/navigator/nav_group/[EMAIL PROTECTED] = 
"$script"] |
/navigator/nav_group/nav/[EMAIL PROTECTED] = "$script"]');


Variables won't expand in single quotes, and putting double quotes INSIDE 
single quotes won't help either.  You'll have to use double-quotes around 
the entire string and escape whatever shouldn't be interpolated:


  $xp->findnodes("/navigator/nav_group/[EMAIL PROTECTED] = '$script']");

Try that.  I don't see anything explaining the syntax of an XML path, so I 
don't really know what *I'm* doing.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: non-ascii characters?

2005-10-14 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 14, Charles Farinella said:


Does anyone know how I can search for non-ascii characters in a text
file?


By non-ASCII, do you mean characters high-bit ASCII or Unicode?

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Quering Msql database from perl

2005-10-14 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 14, K.Moeng said:


$dsn= "dbi:mysql:db_doom:localhost";


So it's a mysql table, not an Msql table.


$statement = "select msg from t_main where votes = (this is where Iam stuck) 
"greater than others"


This is an SQL question rather than a Perl question, but I will answer it 
for you.  In the future, please determine the appropriate list for your 
questions.


  $statement = q{
SELECT *
FROM some_table
ORDER BY votes DESC
LIMIT 1
  };

That will get you one row back, the row with the highest votes.

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Simple Substition Question

2005-10-13 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 13, Dave Adams said:


my $text= 'hello';
my $text2 = 'bye';
my $text3 =~ s/$text/$text2/g;
print $text3;


What are you expecting to happen?  There is NOTHING in $text3, so trying 
to substitute all "hello"s for "bye"s will result in Perl telling you that 
you're using an uninitialized value ($text3).


Perhaps if you put something in $text3 FIRST, and THEN tried s///'ing it.

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: regd. regular. expression.

2005-10-13 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 13, [EMAIL PROTECTED] said:


my $match = '1ak';
if ($match =~/([a-hj-nprt-wyA-HJ-NPRT-WY])/ ){



But when I am executing its still giving me Ok as output .The Problem is
like this I want to match only character (a-h,j-n,p,r,t-w,y)  including
upper case also or in other words I need Error when it it encounters with
any letter [IOQSXZ].
I know we can do with /[^IOQSXZ]i/.  But the thing is I want to check only
alphabets(except those which I just mentioned) or hypen(-) can be pass
through this test.


The problem is your regex is just saying "does $match have one of these 
characters in it?", where you want to say "make sure $match ONLY has these 
characters in it".


For that, you'd do:

  if ($match =~ /^[a-hj-nprt-wyA-HJ-NPRT-WY]{3}\z/) {
# $match is made up of three of those characters
  }

The \z anchor is like the $ anchor, which mean "end of string", except 
that $ can allow for a newline at the end of the string, whereas \z does 
not.  Perhaps, though, it's overkill, since you're already making sure 
$match only has 3 characters.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Lexical Analysis and Parsing in Perl

2005-10-13 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 13, Tommy Nordgren said:

Do anyone know of tools to help in the implementation of parsers and lexical 
analyzers in Perl?


Do you mean for programming languages or spoken/written languages?

There are plenty of parser modules out there; the most well-known, I would 
say, is Parse::RecDescent.


For written language parsing, those are located in the Lingua:: hierarchy 
on CPAN.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Regex matching problem

2005-10-11 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 11, Juan Manuel Casenave said:


1. First Name:
2. Last Name:
3. Age:



#!/usr/bin/perl
# strip_answers.pl

use strict;
use warnings;

# array to store the stripped results
my @information;

# open a filehandle to output.txt (where the results are stored) in read
mode
open (IN, '<', 'output.txt') or die "Can't open output.txt: $!\n";

# while the filehandle is open, read each line and see if it matches
# the regular expression
while () {

# iterate though the filehandle 25 times (one for each question). I set $i
to 1 as
# the first question begins with the number 1
for (my $i=1; $i<26; $i++) {


Here's where your logic fails.  Your outer while loop is reading a line; 
your inner for loop is doing the same thing 26 times!  In addition, you're 
starting $i at 1, which means your setting $information[1], and never 
$information[0] (which is where arrays start).


How about this:

  my @info;

  while () {
# stop after the 25th line ($. holds line number)
last if $. > 25;

# get the stuff after the :
my $answer = (split /:/, $_, 2)[1];

# set $answer to "-" if $answer doesn't
# have a non-whitespace character in it
$answer = "-" if $answer !~ /\S/;

push @info, $answer;
  }

And then you loop through it like so:

  for my $idx (0 .. $#info) {
print $idx + 1, ": $info[$idx]\n";
  }

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Using Perl Modules

2005-10-11 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 11, Madhur Kashyap said:


Is there a way possible by which I can use a perl module without
installing it on the system? Some sort of equivalent for #include
"/home/madhurk/myClass.h" which exists in C/C++


I take it you mean, "how can I tell Perl to look in a certain place for a 
module?"  By telling it so, with the 'lib' pragma:


  #!/usr/bin/perl

  use lib "/home/japhy/modules";
  use MySpecialModule;  # /home/japhy/modules/MySpecialModule.pm

Read 'perldoc lib'.

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: random #

2005-10-09 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 9, ZHAO, BING said:

is there a single random number generator function in perl? Or do 
you have any idea/algorithm on how to shuffle the elements in an array?


The random number generator is rand(); its documentation is here:

  perldoc -f rand

To shuffle an array, read:

  perldoc -q shuffle

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Skipping blank lines while reading a flat file.

2005-10-07 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 7, Dave Thacker said:


Given this test file.
--start-
this

is

my file
end--
I want to skip the blank lines and just print the lines with text, like this


When you say "blank", do you mean lines with NO characters at all (other 
than the ending newline) or lines that ONLY contain whitespace?



   chomp;
   next if ($_ =~ /^\s+/);


Ok, if the line is ONLY "\n", then if you chomp() it, the line will be the 
empty string, "".  This means it WON'T be matched by /^\s+/.


Your regex doesn't make any sense to me.  It would skip lines that start 
with a space, like " This one".  And what's the use of looking for more 
than one leading space?


I have a couple possible solutions for you.  If you want to skip lines 
that consist of ABSOLUTELY NOTHING other than a newline, then you can do:


  while () {
chomp;
next if $_ eq "";
...
  }

If you want to skip lines that only have whitespace characters, then you 
can do:


  while () {
chomp;
next unless /\S/;
  }

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Sorting hash of hashes

2005-10-06 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 6, Rose, Jeff said:


I have been trying to sort a hash but I cannot figure it out for the
life of me


I've fixed a bit of the formatting below...


my %message = (
  $messageid => {
From=> $from,
To  => $to,
To_Num  => $num,
Sub_IP  => $ip,
Subject => $subject,
  },
  ...
)



foreach (sort {$message{$_}->{'To_Num'}[$a] <=>
$message{$_}->{'To_Num'}[$b]} keys (%message)) {
}



foreach (sort {$message{$a} cmp $message{$b}} keys (%message)) {



foreach (sort {$message{$_}->{'To_Num'}[$a] <=>
$message{$_}->{'To_Num'}[$b]} keys (%message)) {


You keep putting $a and $b all the way at the "end" of the things you're 
sorting.  I don't think you actually know what $a and $b are.  $a and $b 
represent two elements of the list you're sorting -- this means they're 
two keys from %message.  This means the ONLY place it makes sense for them 
to be is $message{$a} and $message{$b}.  To sort my the 'To_Num' field, 
then, you would do:


  sort { $message{$a}{To_Num} <=> $message{$b}{To_Num} } keys %message

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Find and replace problem

2005-10-06 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 6, Nath, Alok (STSD) said:


Here's a simple script that is suppose to read a file and
changes a particular string.


Opening a file for read/write doesn't mean you can do in-place edits just 
like that.  If you want to do simple in-place editing, here's an example:


  {
local @ARGV = ("Test.txt");  # the files to edit
local $^I = '.bak';  # a backup extension (use "" for no backup)

while (<>) {
  
s/Ethernet0\.connectionType(?!_changed)/Ethernet0.connectionType_changed/g;
  print;
}
  }

I made two changes to your code.  First, there's NO good reason to write 
code like:


  if ($str =~ m/this_pattern/) {
$str =~ s/this_pattern/that_string/;
  }

You should just write

  $str =~ s/this_pattern/that_string/;

The other change is that the regex makes sure that it doesn't change 
"Ethernet0.connectionType_changed" to "Ethernet0.connectionType_changed_changed"
by making sure that the "Ethernet0.connectionType" is not followed by 
"_changed" -- that's what the (?!_changed) part of the regex is doing.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Mail::IMAPClient failing [was Re: Please help]

2005-10-05 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 5, [EMAIL PROTECTED] said:


Please help me fix this.


Please choose a more descriptive subject line next time.


When I run it, I get the following message:

Can't call method "select" on an undefined value at ./fof_imap1.pl line 12.


This means that $imap is undefined, which means that the object failed to 
be created.



my $imap = Mail::IMAPClient->new( Server => $imaphost,
 User   => $login,
 Password=> $pass,
 Uid => 1,# optional
);


You should change that to:

  my $imap = Mail::IMAPClient->new(...)
or die "couldn't connect to $imaphost as $login:$pass: $@";

as the documentation for the module suggests.  $@ will hold the reason for 
the failure.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: looking for duplicate array values

2005-10-05 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 5, Charles Farinella said:


Thank you for your help, with some help from someone in our office, here
is our solution, perhaps it will be helpful to someone else.

#!/usr/bin/perl -w

use strict;

my %hash;
my $sku;
my $verbose = 0;
my $numDuplicates = 0;

open( INFILE, 'stock.tab' );


You should make sure this succeeded, for debugging purposes:

  open INFILE, 'stock.tab' or die "can't read stock.tab: $!";


foreach(  ) {


You'd be better off using a while loop, which reads one line at a time, 
instead of a for loop, which reads ALL the lines into memory at once!


  while () {

And then you should chomp $_ before going further; otherwise, the last 
element of @array is going to have a newline at the end of it.



   my @array = split( "\t", $_ );


chomp;
my @array = split /\t/;  # $_ is assumed here


   $sku = $array[0];
   $hash{$sku}++;
# test printout
   print "$sku [$hash{$sku}]\n" if $verbose;


You don't really need $sku here... but you can use it if you wish.

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: looking for duplicate array values

2005-10-05 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 5, Charles Farinella said:


I have a list of 15,000+ items in a tab separated list.  I am putting
all the values into an array, and I need to look through all $array[0]
for duplicates and am not sure how to proceed.  Help?


Whenever you think of frequency, or duplicates, or unique values, think of 
using a hash.


The basic idea is:

  for (LIST) {
$frequency{$_}++;
  }

and now your %frequency hash tells you how many times a particular element 
in the LIST was seen.


  perldoc -q duplicate

Once you've looked that over and come up with some code, show us and 
we'll guide you from there.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Problems with set-request NET::SNMP - retransmited

2005-10-05 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 5, Bobby Jafari said:


1 $result = $session->set_request (
2 -varbindlist => [ "${secPvcBulkModeOid}.${nextVcatIndex}" ,
INTEGER, "1",

[...]

9#  "${secPvcUpdateTimeOid}.${nextVcatIndex}" ,
OCTET_STRING, '# 0x0C 0x01'
10# "${secPvcUpdateTimeOid}.${nextVcatIndex}" ,
OCTET_STRING, "$keyUpdateTime"
11  "${secPvcRowStatusOid}.${nextVcatIndex}" ,
INTEGER, "4" ] );

The line with OCTET_STRING as the data type, is giving me grief.
According to the MIB definition, it should be in the form of  # 0xHH
0xMM Where HH and MM are hour and minutes in Hex format. My guess is
that the set-request is expecting a string but somehow PERL is passing
it as ASCI characters or ...


Try sending it as "# 10 1" and see if it works.  The Net::SNMP docs only 
show ONE example of using OCTET_STRING, and I'm not really sure what its 
rules about using "0x.." are.  If that fails, try "# \x0c \x01", which is 
using actual hexadecimal escape sequences to produce character 10 and 
character 1.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




RE: Extracting Upper case letter from a variable

2005-10-05 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 5, BERTHOLD Jean said:


The part I want to extract is only composed of characters in uppercase.


To be safe (in case there are other uppercase characters) you want to get 
uppercase characters between '_' and '-'.



   ( $ORACLE_SID ) = ( $basename =~ /[A-Z]*/ ) ; <-- I tried that but that 
won't work ...


The reason that fails is because the '*' quantifier means "match zero or 
more".  Even the string "this has no capital letters in it" can be matched 
by the regex /[A-Z]*/.  You want /([A-Z]+)/.  The parentheses I've added 
tell Perl to capture what it matched with [A-Z]+ and return it; that's how 
we put something in $ORACLE_SID other than '1'.


  ($ORACLE_SID) = $basename =~ /_([A-Z]+)-/;

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Split with backslashes?

2005-10-05 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 5, [EMAIL PROTECTED] said:


I would like to split up a string like this

my $cd = $arguments;
@dirs = split(///,$cd); #Split $cd when there occurs a backslash

but it doesn't seem to work. I guess /// must be rewritten in some way,
but how?


'/' is a FORWARD slash.  '\' is a BACK slash.  Here are two solutions to 
your problem:


  my @dirs = split /\//, $cd;  # splitting on forward slashes
  my @dirs = split '/', $cd;   # same thing, less ugly

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Deprecated perl hash reference statement problem

2005-10-04 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 4, Tony Frasketi said:

Both $hash->{$key} and $$hash{$key} work fine and this also works Although I'm 
not sure why ${$hash}{$key}.


The reason ${$hash}{$key} works is because it's a generalization of 
$$hash{$key}.  The rule of thumb is:


1. start with $HASH{key}
2. replace HASH with a block: ${ ... }{key}
3. fill the block with a hash reference: ${$hashref}{key}
4. if the contents are simple enough, braces are not needed

I seem to have a lot of problems with the perl language in that there are so 
many ways to accomplish (express) the same thing A wonderful feature but 
sometimes confusing as hell (to me at least).


Consider reading 'perldoc perlreftut', a tutorial to using references.

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Deprecated perl hash reference statement problem

2005-10-04 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 4, Tony Frasketi said:


sub get_form_data_1 {
 my($hashRef) = @_;
 my($buffer) = "";
 my($key,$value,$pair,@pairs);

 if ($ENV{'REQUEST_METHOD'} eq "GET") {
$buffer = $ENV{'QUERY_STRING'};
 }else {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
 }
 @pairs = split(/&/, $buffer);
 foreach $pair (@pairs) {
my($key, $value) = split(/=/, $pair);
$key   = decodeURL($key);   # Decode the key
$value = decodeURL($value); # Decode the value
%{$hashRef}->{$key} = $value;!# Enter the value in the hash <- 
Deprecated stmt

print "get_form_data_1: Setting $key to [$value]"; #Debug
 }
}


Blech!  Please, please, PLEASE use CGI.pm for your form-parsing needs. 
It's standard and it works, and it handles things like multiple-value 
select-boxes (which get_form_data_1() does not).


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Deprecated perl hash reference statement problem

2005-10-04 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 4, Tony Frasketi said:

I've used the following statement in several instances before and never had 
any errors reported and the programs all seem to work ok.


 %{$hashRef}->{$key} = $value;


%hash->{$key} and @array->[$idx] are syntaces that you should not use. 
The fact that they work is due to intricacies of the Perl parser. 
Similarly, %{$hashref}->{$key} and @{$arrayref}->[$idx] are equally bad.


Use $hash{$key} and $array[$idx] for normal hash and array access.  When 
using hash and array references, you have two choices: $hash->{$key} and 
$array->[$idx], or $$hash{$key} and $$array[$idx].


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: sort with special order

2005-10-03 Thread Jeff &#x27;japhy&#x27; Pinyan

[sorry, PINE has become very confused about who said what]

On Oct 3, Jay Savage said:


On 10/3/05, Bakken, Luke <[EMAIL PROTECTED]> wrote:
JupiterHost.Net wrote:
>> On Oct 3, JupiterHost.Net said:
>>
>>> I have a list of strings that start with an uppercase B, Q, or Z
>>>
>>> I need to sort them so they are in order of Q, B , then Z
>>>
>>> Any ideas or input on how to efficiently do that with sort() or even
>>> map() is most appreciated :) perldoc -f sort|-f map didn't appear to
>>> address this situation :(
>
> Jeff 'japhy' Pinyan wrote:
>
>> I would use map() before and after sort() to "correct" leading
>> characters.
>>
>>   my @sorted =
>> map { tr/123/QBZ/; $_ }
>> sort
>> map { tr/QBZ/123/; $_ }
>> @data;
>
> Xavier Noria wrote:
>
>  > They all go in ASCII relative order except B <-> Q, thus a way to
>  get > it is to handle that special case and delegate to cmp the rest:
>  >
>  > my @sorted = sort {
>  > my $x = substr($a, 0, 1) . substr($b, 0, 1);
>  > $x eq "BQ" || $x eq "QB" ? $b cmp $a : $a cmp $b;
>  > } @array;
>  >
>  > I used the concatenation for clarity, you see the idea anyway if
>  that's > too expensive for your usage.
>
> Brilliant! I'll benchmark those ideas :) Thanks you two!

> Read this for an explanation of Jeff's solution:
>
> http://en.wikipedia.org/wiki/Schwartzian_transform

That will slow you down a little with the capturing and substitution,
but is solves Jeff's problem with capital Q, B, or Z later in the
string. I'm not sure how it stacks up to Xaiver's temp variable and
multiple substr calls. This isn't as simple as it looked.


Actually, I'm using a Guttman-Rosler Transform, not a Schwartzian 
Transform.  I've actually come up with a slightly less "dangerous" 
technique I believe:


  my @sorted =
map substr($_, 1),
sort
map +{qw( Q a  B b  Z c )}->{substr($_, 0, 1)} . $_,
@original;

Reading from the bottom up:

1. we have the original array
2. using an anonymous hash reference ({Q => 'a', B => 'b', Z => 'c'}), we
   select the first character (Q, B, or Z) of the string ($_) and prepend
   the value in the hash corresponding that character (a for Q, b for B,
   or c for Z)
3. we then sort the modified list of words, with all the 'Q' words being
   first (since they are now prepended with 'a'), then all the 'B' words,
   then all the 'Z' words
4. then we extract everything AFTER the first character

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: [SPAM DETECT] Re: sort with special order

2005-10-03 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 3, Xavier Noria said:


On Oct 3, 2005, at 18:16, Jeff 'japhy' Pinyan wrote:


  my @sorted =
map { tr/123/QBZ/; $_ }
sort
map { tr/QBZ/123/; $_ }
@data;


There's a potential gotcha there: since all Qs and Bs are being swapped 
lexicographic order after the first character is also changed. That might not 
be an issue though.


My code relies on the sample data being indicative of the rest of the 
data, meaning there'll only be a capital Q, B, or Z at the beginning of 
the string.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: sort with special order

2005-10-03 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 3, JupiterHost.Net said:


I have a list of strings that start with an uppercase B, Q, or Z

I need to sort them so they are in order of Q, B , then Z

Any ideas or input on how to efficiently do that with sort() or even map() is 
most appreciated :) perldoc -f sort|-f map didn't appear to address this 
situation :(


I would use map() before and after sort() to "correct" leading characters.

  my @sorted =
map { tr/123/QBZ/; $_ }
sort
map { tr/QBZ/123/; $_ }
@data;

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: multiple overlapping matches in a regex

2005-10-03 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 3, Frank Geueke, III said:


while()
{
   my @matches = m/([A-Z])[A-Z]{2,}/;
   print "@matches\n";
}

The input file contains:
HELLo

WoRLD


You have two problems.  First, you're not telling the regex to find all 
instances it can with the /g (for *g*lobal matching) flag.


Second, a regex starts where it left off.  After it has matched "HEL", it 
starts looking at the next "L".  You'll need to use look-aheads to achieve 
the desired effect:


  my @matches = m/([A-Z])(?=[A-Z]{2,})/g;

The (?=...) part means "look ahead (but don't consume) for the following 
pattern ...".


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: use strict, aliases, local

2005-10-02 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 2, Gerard Robin said:


use warnings;
# use strict;

$foo = 26;
@foo = ("here's", "a", "list");

&testsub (*foo);
print ("The value of \$foo is now $foo\n");

sub testsub {
local  (*printarray) = @_;
foreach  $element ( @printarray) {
print ("$element\n");
}
$printarray = 61;
}

it gives the expected result.


Yes, but it's pretty esoteric code.  Why are you doing this?


I can't make this script work with "use strict", always perl complains ?


If you REALLY want to use package variables instead of lexicals, then you 
need to follow the instructions in the 'strict' documentation for 
declaring your global variables:


  use strict;
  our ($foo, @foo);

  $foo = ...;
  @foo = ...;

  testsub(*foo);

  sub testsub {
our ($x, @x);
*x = $_[0];
$x++;
push @x, 100;
  }

But this is really inadvisable.  What is your motivation to do this kind 
of thing?


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Dealing with Uninitialized values in an array and DBI?

2005-10-01 Thread Jeff &#x27;japhy&#x27; Pinyan

On Oct 1, Crayola said:


Use of uninitialized value in concatenation (.) or string at ./audit-logs.pl
line 98.


Which line that you've shown us is line 98?


The code is below.



   my $oth = $oracledbh->prepare ("select * from sys.aud\$ where
timestamp\# > ? ");


While the '$' does need a backslash, the '#' doesn't.  You could just use 
single quotes here anyway:


  $oracledbh->prepare('select * from sys.aud$ where timestamp# > ?');


   $oth->bind_param( 1, $timest, { TYPE => 'SQL_DATE' } );
   $oth->execute ();

   while (my @ary = $oth->fetchrow_array() ) {

   my $rows2 = $mysqldbh->do (\"$sid\", \"${ary[0]}\",
\"${ary[1]}\", \"${ary[2]}\", \"${ary[3]}\", \"${ary[4]}\", \"${ary[5]}\",
\"${ary[6]}\", \"${ary[7]}\", \"${ary[8]}\", \"${ary[9]}\", \"${ary[10]}\",
\"${ary[11]}\", \"${ary[12]}\", \"${ary[13]}\", \"${ary[14]}\",
\"${ary[15]}\", \"${ary[16]}\", \"${ary[17]}\", \"${ary[18]}\",
\"${ary[19]}\", \"${ary[20]}\", \"${ary[21]}\", \"${ary[22]}\",
\"${ary[23]}\", \"${ary[24]}\", \"${ary[25]}\", \"${ary[26]}\",
\"${ary[27]}\", \"${ary[28]}\", \"${ary[29]}\")");


What is that?  WHAT is THAT?  Assuming $mysqldbh is just a database 
handle, you're passing it a reference to an enormous string.  You're doing 
something wrong here.



   my @ary = ();


That's doing nothing -- you're creating a NEW lexical named @ary.  Drop 
the my(), but I'm not even sure it's necessary.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: question about # of files in a directory

2005-09-30 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 28, ZHAO, BING said:

Is there a way to call or maybe get the # of files in a 
directory?


The simplest way is to use opendir() and readdir().

  opendir my($dh), $path or die "can't opendir $path: $!";
  my @files = readdir $dh;
  closedir $dh;

Now you have the files from the directory $path in the array @files.  The 
number of elements is the number of files.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Comparing an array with hash keys

2005-09-30 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 30, Mark Martin said:

I want to compare the elements of an array with the keys in a hash. If 
matches are found, I want to store the values associated with the 
matched keys in an array for future processing :


  my @storage_array = ();
  foreach $item(@original array) { 
if (exists $original_hash{$item}) {

  push(@storage_array, ?? )
}
  }


Your ?? should just be $original_hash{$item}, I expect, although this 
code could be written far more succinctly as a map():


  my @storage_array = map {
exists $original_hash{$_} ? $original_hash{$_} : ()
  } @original_array;

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: generating a wordlist from an array of arrays

2005-09-29 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 29, mark berger said:


hey list. i stuck with gererating a wordlist from a changing
multidimensional array. each entry in the array contains a list with the
possible values.


You want a "cartesian cross product".  And there's a module out there that 
does just that:  Set::CrossProduct.


  http://search.cpan.org/~bdfoy/Set-CrossProduct-1.6/CrossProduct.pm

Sample use:

  use Set::CrossProduct;

  my $iterator = Set::CrossProduct->new(['a','b'],['c'],['d','e','f']);

  my @wordlayout = $iterator->combinations;

@wordlayout will be an array of array references (like ['a','c','f']).  To 
turn them into strings, you could do:


  my @wordlayout = map join("", @$_), $iterator->combinations;


any hints on how to solve this? probably using recursion but i got no
idea about it right now. and sorry for my bad english, hope i can make
my problem understandable.


It would be a fun exercise to do this on your own, and yes, recursion is 
the most obvious way to do it.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: a little help...

2005-09-28 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 28, FamiLink Admin said:

I am trying to read a log file and get a list of how many times an IP 
address get blocked each hour by category PO.  An example line in the log 
with a block is:

-
[2005-09-28 10:05:03 -7:00] 127.0.0.1 71.32.59.249 216.163.137.3 - 
  http://www.playboy.com/ blocked 0 PO

-
What I have kinda works but I am not sure if it is the best practice. 
This is the first time programming in perl and this is what I have so 
far:


Your indentation leaves much to be desired, so I've "fixed" it.


sub Scanlog {
  local($ipb) = @_;


No reason to use 'local'; stick with 'my' here.  But... what is $ipb?  You 
don't use it anywhere!



  open my $slog, "-|", "tail -n 5 $log" or die "Unable to open $log:$!\n";
  open (OUTPUT,">/etc/squid/iplist.txt");
  open (OUTPUT2,">/etc/squid/SuspendIpList.txt");


You should also die if neither of those could be opened:

open(OUTPUT, ">...") or die "can't create /etc/squid/iplist.txt: $!";


  while (<$slog>){ # assigns each line in turn to $_
# use an array slice to select the fields we want
@data = (split ,$_)[1,4,10,5,7];
$hr = (split /:/ ,$data[0])[0];
$ip = "$data[1]";


Those three variables should all be declared with 'my'.  Your line 
assigning to @data has a typo that hasn't effected you, but it might 
eventually.


  my @data = (split)[1,4,10,5,7];  # why out of order?
  my $hr = (split /:/, $data[0])[0];
  my $ip = $data[1];  # no need to quote $data[1] here


if ($flag eq $data[2]) {


Where is $flag coming from?


  if ($hr eq $hour) {


Where is $hour coming from?

Those two if statements can be combined into one, since you don't do 
anything if they aren't both true.


  if ($flag eq $data[2] and $hr eq $hour) {


foreach (/$data[2]/) {
  $matches += 1 ;
}


I have a feeling this could lead to false positives.  How do you know that 
'PO' (or whatever else $data[2] might hold) won't appear in the URL, for 
instance?  Perhaps this should just be


  $matches++;

But where is $matches coming from?!


if ($matches > $blocklimit) {


Where does $blocklimit come from?!


  $ip1 = "$data[1]/32";


Declare that with 'my'.


  print OUTPUT "$matches,", "$hour, ","$ip1, ", "@data","\n";


You could just write that as

  print OUTPUT "$matches, $hour, $data[1]/32 @data\n";


  print OUTPUT2 "$ip1\n";
  $matched = $matches;
  $matches = 0;


Where did $matched come from?


}
  }
}
  }
  close (OUTPUT);
  close (OUTPUT2);
}


You should not use any variables in a function that you did not pass to it 
or create IN it.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Anonymous Reference Question

2005-09-28 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 28, Dave Adams said:


#!/usr/bin/perl -w
use strict;
use Data::Dumper;
my $grades = {"tom"=> 50, "sally"=> 60, "harry" => 70};
print Dumper($grades) ;

And perl gives me this:

$VAR1 = {
 'harry' => 70,
 'sally' => 60,
 'tom' => 50
   };

QUESTION: Does this mean that $grades is an anonymous reference
because perl gives is $VAR1?


No, it's just what Data::Dumper outputs when you don't assign a name to 
the data structure you pass it.  See the Data::Dumper docs.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: eval without warnings

2005-09-27 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 27, Bryan R Harris said:


"2*(3+2)" ==> 10
"2*dog"  ==> "2*dog"
"mysquarefunction(2)" ==> 4
"3*mysquarefunction(2)" ==> 12
"some guy" ==> "some guy"


Here's a solution that works for the cases you've provided:

  sub try_eval {
local $@;
my $warning;
local $SIG{__WARN__} = sub { $warning = 1 };
my $expr = shift;
my $val = eval $expr;
$val = $expr if $@ or $warning;
return $val;
  }

It catches fatal errors (via $@) and non-fatal warnings (via the __WARN__ 
handler).  If there is an error or warning, the expression itself is 
returned; otherwise, the returned value from that expression is returned.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Block Confusion

2005-09-27 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 27, Ryan Frantz said:


# list the processes to hunt for
my @findProcesses = (
 "putty",
 "firefox",
);

foreach my $process (in
$serverObj->InstancesOf("Win32_PerfFormattedData_PerfProc_Process")) {
 foreach my $matchProcess ( @findProcesses ) {
   if ( $process->{Name} =~ /$matchProcess/oi ) {


You're using the /o modifier here, and that's causing your problem.  I'm 
guessing you don't actually know what the /o modifier does.  It tells Perl 
the regex won't change after it's been compiled the first time.  Remove 
the /o modifier and I believe your code will run fine.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




RE: Hash Problem

2005-09-25 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 23, Ryan Frantz said:


From: Jeff 'japhy' Pinyan [mailto:[EMAIL PROTECTED]


foreach my $process (in $sobj->InstancesOf("Win32_LogicalDisk")) {
  next if $ignoreDriveTypes{ $process->{DriveType} };


So this would evaluate to true if $process->{DriveType} matches a key in
the hash?


It would evaluate to whatever $ignoreDriveTypes{$process->{DriveType}} is. 
If $process->{DriveType} is a key of %ignoreDriveTypes, it'd be whatever 
that key's value is.  Otherwise, it returns undef (false).


If, by some bizarre coincidence, 0, '', or undef would be a valid value 
for the key's value in the hash, you would need to use


  next if exists $ignoreDriveTypes{ $process->{DriveType} };

which ignores the actual contents of the key's value, and just checks to 
see if the key is in the hash.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Hash Problem

2005-09-23 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 23, Ryan Frantz said:


I'm working on a small script that checks the free space on local fixed
drives on my system.  Since there other drive types (i.e. floppy,
CD-ROM, network maps) I want to exclude those.  I decided to use a hash
but the script still displays all of the drives on my system.  If I just
use simple 'next' statements, it works fine.  I've placed the block
inside the original foreach and even tried wrapping it around the print
statements to no avail.  What gives?


The problem is you've got another loop, and that's what the 'next' is 
getting you out of.



my %ignoreDriveTypes = (
 'floppy'  => '2',
 'network' => '4',
 'cdROM'   => '5',
);


I would invert this:

  my %ignoreDriveTypes = (
2 => 'floppy',
4 => 'network',
5 => 'cdrom',
  );

Then you can say:


foreach my $process (in $sobj->InstancesOf("Win32_LogicalDisk")) {


next if $ignoreDriveTypes{ $process->{DriveType} };

since the "names" associated with the types are really just satellite 
data.



 foreach my $type ( keys %ignoreDriveTypes ) {
   next if ( $process->{DriveType} == $ignoreDriveTypes{$type} );
 }


Here was the problem.  This 'next' was working on THIS foreach loop, not 
the $process foreach loop.  You'd have had to put a label on the outer 
foreach loop like so:


  PROCESS: foreach my $process (...) {
foreach (...) {
  next PROCESS if ...;
}
...
  }

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Looking for perl scripts to remove ^M

2005-09-23 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 23, Dave Adams said:


Sometimes I get perl scripts that were developed on windows and then
brought over to UNIX and the scripts contain all the pesky
metacharacters of ^M and excessive blank lines.

Does anyone have a simple script to clean these files up or suggestions?


Many unix platforms come with a program called 'dos2unix' or something 
similar.  You don't really need Perl to do this.  A simple 'tr' command 
will work:


  tr -d '\r' '' < file.in > file.out

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: here tag and internal variable

2005-09-23 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 23, Frank Geueke, III said:


text immediately following the variable name.  How do
I tell Perl that the text is not part of the variable
name?  Here's my code:


You wrap the *name* of the variable in braces:

  print "${get_to_index}css/hotspot.css";

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: how do I grep and have the matched single line in perl variable

2005-09-23 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 23, Manish Sapariya said:


I want to have pure perl equivalent of following:

my $out = `my_utility | grep manish`;

which will give me the lines containing manish in the output
of my_utility in variable $out.


You'd use the aptly-named grep() function in Perl to get only those 
elements returned by `my_utility` that have 'manish' in them:


  my @wanted = grep /manish/, `my_utility`;

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: reusing DBI statement handle

2005-09-23 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 23, Manish Sapariya said:


In following db functions I am using returned "statement handle "
and afterword just ignoring it and overwrite it with the newly
returned value.

Does this cause memory leak/ or any kind of resouce leak?
Can I reuse statement handle in such a way? If not, what is
cleaner way to do it?


You're doing it in an acceptable manner.  One suggestion would be to move 
the scope of $sth to JUST the foreach loop, though:



   my $sth;
   ...
   foreach $table (@$rTableListRef) {
$query = "select count(*) from $table";
$sth = perform_query($query);
  ...
   }


might be better written as


   foreach $table (@$rTableListRef) {
$query = "select count(*) from $table";
my $sth = perform_query($query);
  ...
   }


The other thing I've noticed is your use of function prototypes.  Don't 
use them, because they are rarely needed, and your program isn't even 
actually using them at all.  Not to mention this one is written wrong:



sub perform_query($$) {
   my ($query) = @_;
   ...
}


Your prototype SAYS it's getting TWO scalars, but you've only passed one. 
But this is never noticed because you've called the function *before* Perl 
has seen its prototype.


Long story short:  don't use prototypes.

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Is it possible to force a particular order in a hash?

2005-09-22 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 22, Dave Adams said:


I have a hash that I need to use later and display some values in a
particular order.  Perl comes up with its own way of ordering it but I
am wondering if I can instruct perl to have it listed in the way that
I want.


You can use Tie::IxHash (or Tie::IxHash::Easy if you're going to be using 
multidimensional hashes).


Look for either module on CPAN (http://search.cpan.org/).

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Regular expression in varaible

2005-09-22 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 22, Michael Gale said:


Jeff 'japhy' Pinyan wrote:


On Sep 22, Michael Gale said:

I have the following line of code and I believe it is correct, but it is 
not doing what I am expecting it to do:


Config file
[test]
value=^OK$i


The problem is that '^OK$i' as a string turned into a regex is a regex that 
can never match. The '$' is the end-of-string anchor, so there's no way an 
'i' can match after it. Were you hoping the '$i' would be expanded to the 
current value of the $i variable?


I was hoping that $ would mean the end of the string and "i" would mean case 
insensitive :(


To do that, use (?i) instead:

  value=(?i)^OK$

Alternatively, you could just write

  value=^[Oo][Kk]$

but that gets unwieldy as the regex grows in size...

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Reverse If and Normal Else

2005-09-22 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 22, John W. Krahn said:


The if statement modifier is just another way to write a logical and statement:

$ perl -MO=Deparse -e' display_nothing() if $match_type eq q/none/ '
display_nothing() if $match_type eq 'none';
-e syntax OK
$ perl -MO=Deparse -e' $match_type eq q/none/ and display_nothing() '
display_nothing() if $match_type eq 'none';
-e syntax OK
$ perl -MO=Deparse -e' $match_type eq q/none/ && display_nothing() '
display_nothing() if $match_type eq 'none';
-e syntax OK


An important difference, though, is that the if statement modifier takes 
an expression and produces a statement, so you can't store its return 
value somewhere.  That is,


  $result = (do_this() if that());

is a syntax error.  Not so in the case of '&&' and 'and':

  $result = (that() and do_this());
  $result = (that() && do_this());

However, you can turn any statement into an expression by using do { } 
around the statement:


  $result = do { do_this() if that(); };

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Regular expression in varaible

2005-09-22 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 22, Michael Gale said:

I have the following line of code and I believe it is correct, but it is not 
doing what I am expecting it to do:


Config file
[test]
value=^OK$i


The problem is that '^OK$i' as a string turned into a regex is a regex 
that can never match.  The '$' is the end-of-string anchor, so there's no 
way an 'i' can match after it.  Were you hoping the '$i' would be expanded 
to the current value of the $i variable?


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Reverse If and Normal Else

2005-09-22 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 22, Frank Geueke, III said:


display_nothing() if ($match_type eq 'none');
else
{
}

Now I like the reverse if because it takes up one line
instead of four (I like braces on their own lines -
see else).  But this error...

syntax error at
/usr2/login/fjg/hotspot_tracker/search_by_ip_or_mac.cgi
line 70, near "else"

...keeps coming up.  Is there a way to keep the
reverse if and use an else (or something like it) that
anyone knows of?  Thanks.  ~Frank


No, there isn't.  Not with 'else', at least.  You could use a ternary 
(hook) operator:


  ($match_type eq 'none') ?
display_nothing() :
display_something();

is like saying

  if ($match_type eq 'none') {
display_nothing();
  }
  else {
display_something();
  }

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Splitting on =

2005-09-21 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 21, Tommy Nordgren said:

When called with the syntax I've used, it returns a list containing a 
single string.

I'll try without the paranthesis.
Can it be that perl treats the entire source text between left and right 
paranthesis as a regular expression.

If so, I consider this a bug.


There's no bug.  You're doing something wrong.  If you show us a bit more 
code, that demonstrates how you're using the function, we might be able to 
help more.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Package require question again

2005-09-19 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 19, Luinrandir said:


From: "Jeff 'japhy' Pinyan" <[EMAIL PROTECTED]>


   my $g = $main::{$Player{Location} . "::"}{Options};
   my $value = $$g;

or, as one line:

   my $value = ${ $main::{$Player{Location} . "::"}{Options} };



is the double $$ in $$g a mistake or real?


If it was a typo, what on earth would I have written it for?  What would

  $g = ...;
  $value = $g;

do that

  $value = ...;

wouldn't do?

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Package require question again

2005-09-19 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 17, Luinrandir said:


ok I have the program working again.
but how do I get a var from the package?

and the cgi calls the var in the package like this
my $V = $main::{$Player{Location} . "::"}{Options};


What does that return to you?  A glob.  And a glob is like a reference to 
every data type.  To get the scalar variable found in the glob, you have 
to dereference the glob as if it were a scalar:


  my $g = $main::{$Player{Location} . "::"}{Options};
  my $value = $$g;

or, as one line:

  my $value = ${ $main::{$Player{Location} . "::"}{Options} };

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: How to Update Hash to Itself Recursively

2005-09-16 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 16, Wijaya Edward said:


But how come my code below doesn't give
the intended result as below? How can I go about it?


Simply put, because the 'foreach my $line (keys %line)' creates the list 
of keys ahead of time.  Here's one way to achieve your goal:


  my @stack = qw( A B C );
  my @tojoin = qw( W X Y Z );
  my @result;

  # while there is still something in @stack
  while (@stack) {
# take the first element off, put it in $next
# and add it to our @result array
my $next = shift @stack;
push @result, $next;

# STOP processing @stack if this element is 'AYW'
last if $next eq 'AYW';

# for each suffix in @tojoin...
for (@tojoin) {
  # add an element to @stack that is the
  # CURRENT element with the suffix added
  push @stack, "$next$_";
}
  }

You can, of course, use a hash for @result instead of the array like I 
did.


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: keys of hash of hashes

2005-09-16 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 16, Brent Clark said:


foreach my $fileName ( keys %$ref_allTariffData ){

print keys $ref_allTariffData{$fileName};

}


The keys() function takes a hash.  You used it properly the FIRST time, 
but not the second.  And, $hash{$key} means that you have a hash called 
'%hash', whereas $hash->{$key} means that you have a hash reference stored 
in $hash.  Your print() line, therefore, should be:


  print keys %{ $ref_allTariffData->{$filename} };

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: how do I know if it's an array?

2005-09-15 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 14, Ryan Perry said:

I have 2 set of options on a web page.  first some radios (scalar), then some 
check boxes (array).


How will I know if I have an array or not?


If you're using a competant CGI query parser, like CGI.pm that comes with 
Perl, then you can simply do:


  #!/usr/bin/perl

  use CGI;
  $query = CGI->new;

  $which_box = $query->param('some_radio_button_name');
  @colors = $query->param('some_check_box_name');

and it's taken care of for you.

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: strange result with reg exp

2005-09-14 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 14, Christopher Spears said:


In the script I am writing, I use the following
regular expression:

$pass =~ tr/a-z/A-Z/s;

If I give the expression a string like "Occ", then the
expression will convert it to "OC" instead of "OCC"!
What is going on?


It sounds like you're using an operator you don't understand.  tr/// is a 
character translation operator; it has nothing to do with regexes.  The /s 
modifier to tr/// tells it to squash consecutive identical characters into 
one.


I think you just want to use the uc() function, to convert a string to 
uppercase.


  perldoc -f uc

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Why wont this work? Package require question

2005-09-14 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 14, Luinrandir said:


but what if I want to pass a var? then
$glob->($foo,$bar);
?


But I still have no clue as to why this works... esp.
$glob->();

just looked in my book.. am I dereferencing a reference?


Basically, yes.  $glob ends up being a glob, a reference to everything 
with that name (in your case, a reference to everything named 'HTML' in 
the Inn:: or Castle:: or Whatever:: class).  When you do $foo->(...), 
you're saying that $foo holds a reference to a function, and you want to 
call the function.  Since what's in $glob is a glob, a reference to every 
type (scalar, array, hash, etc.), we do $glob->(ARGS).


--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Why wont this work? Package require question

2005-09-14 Thread Jeff &#x27;japhy&#x27; Pinyan

On Sep 14, Luinrandir said:


$Player{Location}="Inn"
require '$Player{Location}.pl'; #no error here, I think.
'$Player{Location}'::HTML(); #error occurs here


The single quotes are wrong.

  $Player{Location} = "Inn";
  require "$Player{Location}.pl";  # loading, for example, Inn.pl

Now you want to call the Inn::HTML() function, right?  You can't easily do 
it if strict is turned on.  Here's one way, though:


  my $glob = $main::{$Player{Location} . "::"}{HTML};
  $glob->();

--
Jeff "japhy" Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




  1   2   3   4   5   6   7   8   9   10   >