Re: Filtering contetn in a variable

2008-07-10 Thread luke devon
Thanks for every one who tried to help me. but all were unsuccessful and I 
would like to submit my tries for your consideration. 

This is how its done.
$ip = substr($ip, 0, (length($ip)-2));





- Original Message 
From: Gunnar Hjalmarsson <[EMAIL PROTECTED]>
To: beginners@perl.org
Sent: Wednesday, July 9, 2008 20:33:45
Subject: Re: Filtering contetn in a variable

[ Please stop top-posting!!! ]

luke devon wrote:
> Li, Jialin wrote:
>> luke devon wrote:
>>> I am storing IP in to a varable ,  $ip="172.22.8.10 \-";
>>> but i wanted to filter out only the ip .
>> 
>> i am not so sure what the input is, this simple regex should solve your
>> problem:
>> 
>> ($ip_filtered) = /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
> 
> Thnks for the reply,
> I am receiving IP value as 192.168.10.5/ -
> From here , i wanted to filtered out the exact ip. i tried your 
> example , but it didnt work.

How would we be able to correct your code if you don't show it to us???

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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

Send instant messages to your online friends http://uk.messenger.yahoo.com 

combo box+ Perl+JS

2008-07-10 Thread elavazhagan perl
Hello wisdoms,
Please have a glance on my following querry and attached reference files.
I need to make three combo box which are related to each other.When the user
choose the Continents,the specified countries should be displayed in the
pull down menu.For each countries we have facilities in the database,that
facilities should be displayed in the third pull down menu.

I Have the script for the first two combo boxes.No issues...When i tried to
link the third box through OnChange JavaScript function,there is null values
in the second box itself. we tried to bring the solution through the
following Perl scripts .

*ISSUES:*
Now when the user choose a country,the final country in the continent is
displayed as selected ie the following country override the previous
one...Example..the Turkey country is displayed as selected country what
ever we choose in the Europe Region

Please guys I searched a lot in the Perl as well as JavaScripts
Forums.Let ur valuable suggestion may bring a better solution to
this issue..Help meThanks.


##

my %Regions = (
 Europe =>[
   'Belgium',
   'Denmark',
   'France',
   'Germany',
   'Great Britain',
   'Hungary',
   'Portugal',
   'Russia',
   'Spain',
   'Sweden',
   'Turkey',
 ],
 Asia => [
   'Australia',
   'China',
   'India',
   'Malaysia',
   'NewZealand',
   'Philippines',
   'South Africa',
   'Taiwan',
   'Vietnam',
 ],
 North => [
   'U.S.',
   'Canada',
   'Mexico',
 ],

 South => [
   'Argentina',
   'Brazil',
   'Venezuela',
 ],
);

 foreach my $region ( keys %Regions ) { if($region eq 'Europe')
{$country_code="$Regions{$region}[0]"; my
$selected="selected";}else{$selected="";}}
print qq ~ Belgium
~;foreach my $region ( keys %Regions ) { if($region eq 'Europe')
{$country_code="$Regions{$region}[1]"; my
$selected="selected";}else{$selected="";}}
print qq ~ Denmark
~;foreach my $region ( keys %Regions ) { if($region eq 'Europe')
{$country_code="$Regions{$region}[2]"; my
$selected="selected";}else{$selected="";}}
print qq ~ France
~;foreach my $region ( keys %Regions ) { if($region eq 'Europe')
{$country_code="$Regions{$region}[3]"; my
$selected="selected";}else{$selected="";}}
print qq ~ Germany
~;foreach my $region ( keys %Regions ) { if($region eq 'Europe')
{$country_code="$Regions{$region}[4]"; my
$selected="selected";}else{$selected="";}}
print qq ~ Great Britain
~;foreach my $region ( keys %Regions ) { if($region eq 'Europe')
{$country_code="$Regions{$region}[5]"; my
$selected="selected";}else{$selected="";}}
print qq ~ Hungary
~;foreach my $region ( keys %Regions ) { if($region eq 'Europe')
{$country_code="$Regions{$region}[6]"; my
$selected="selected";}else{$selected="";}}
print qq ~ Portugal
~;foreach my $region ( keys %Regions ) { if($region eq 'Europe')
{$country_code="$Regions{$region}[7]"; my
$selected="selected";}else{$selected="";}}
print qq ~ Russia
~;foreach my $region ( keys %Regions ) { if($region eq 'Europe')
{$country_code="$Regions{$region}[8]"; my
$selected="selected";}else{$selected="";}}
print qq ~ Spain
~;foreach my $region ( keys %Regions ) { if($region eq 'Europe')
{$country_code="$Regions{$region}[9]"; my
$selected="selected";}else{$selected="";}}
print qq ~ Sweden
~;foreach my $region ( keys %Regions ) { if($region eq 'Europe')
{$country_code="$Regions{$region}[10]"; my
$selected="selected";}else{$selected="";}}
print qq ~ Turkey
##

 var countryLists = new Array(4) 
 countryLists["empty"] = ["Select a Country"]; 
 countryLists["North America"] = ["Canada", "United States", "Mexico"]; 
 countryLists["South America"] = ["Brazil", "Argentina", "Venezuela"]; 
 countryLists["Asia Pacific/Africa"] = 
["Australia","China","India","Malaysia","New Zealand","Philippines","South 
Africa","Taiwan","Vietnam"]; 
 countryLists["Europe"]= ["Belgium","Denmark","France","Germany","Great 
Britain","Hungary","Portugal","Russia","Spain","Sweden","Turkey"]; 
  
 function countryChange(selectObj) { 
 // get the index of the selected option 
 var idx = selectObj.selectedIndex; 
 // get the value of the selected option 
 var which = selectObj.options[idx].value; 
 // use the selected option value to retrieve the list of items from the 
countryLists array 
 cList = countryLists[which]; 
 // get the country select element via its known id 
 var cSelect = document.getElementById("country"); 
 // remove the current options from the country select 
 var len=cSelect.options.length; 
 while (cSelect.options.length > 0) { 
 cSelect.remove(0); 
 } 
 var newOption; 
 // create new options 
 for (var i=0; i

Re: Can't locate ...

2008-07-10 Thread Ganesh Babu N
Download the module to the system and install it manually.

On 7/10/08, protoplasm <[EMAIL PROTECTED]> wrote:
> Somehow I screwed up my `Perl` and have no idea how to get out of this
> mess. I attempt to run a program and get the following:
>
> @naiad ~/workspace/aestTest] ./aestTest --CbcDec
> Can't locate Mac/Files.pm in @INC (@INC contains: /opt/local/lib/
> perl5/5.8.8/darwin-2level /opt/local/lib/perl5/5.8.8 /opt/local/lib/
> perl5/site_perl/5.8.8/darwin-2level /opt/local/lib/perl5/site_perl/
> 5.8.8 /opt/local/lib/perl5/site_perl /opt/local/lib/perl5/vendor_perl/
> 5.8.8/darwin-2level /opt/local/lib/perl5/vendor_perl/5.8.8 /opt/local/
> lib/perl5/vendor_perl .) at /opt/local/lib/perl5/site_perl/5.8.8/File/
> HomeDir/Darwin.pm line 34.
>
> Next, I try to start cpan to install Mac::Files:
>
> @naiad ~/workspace/aestTest] cpan
> CPAN: File::HomeDir loaded ok (v0.80)
> Can't locate Mac/Files.pm in @INC (@INC contains: /opt/local/lib/
> perl5/5.8.8/darwin-2level /opt/local/lib/perl5/5.8.8 /opt/local/lib/
> perl5/site_perl/5.8.8/darwin-2level /opt/local/lib/perl5/site_perl/
> 5.8.8 /opt/local/lib/perl5/site_perl /opt/local/lib/perl5/vendor_perl/
> 5.8.8/darwin-2level /opt/local/lib/perl5/vendor_perl/5.8.8 /opt/local/
> lib/perl5/vendor_perl /Users/sprotsman/workspace/aestTest) at /opt/
> local/lib/perl5/site_perl/5.8.8/File/HomeDir/Darwin.pm line 58.
>
> Uhg! I'm running Mac OS 10.5.4. Any help is much appreciated.
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> http://learn.perl.org/
>
>
>

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




RE: Count of number of rows returned in delete / update sql in Perl DBI ?

2008-07-10 Thread Ow Mun Heng
This is what I use

eval {
print "Executing DELETE\n" if ($verbose);
my $del_rows = $dbh_pg->do($pg_query1) or die "prepare failed
DBI::errstr";
if ($del_rows != 0)
{
  print "Number of rows deleted: " . $del_rows . "\n";
} else {
  $del_rows = 0;
}


-Original Message-
From: Amit Saxena [mailto:[EMAIL PROTECTED] 
Sent: Thursday, July 10, 2008 7:45 PM
To: Perl Beginners
Subject: Count of number of rows returned in delete / update sql in Perl DBI
?

Hi all,

I want to know how to get the count of number of rows returned in delete /
update sql in Perl DBI ?

I have ready some articles on the internet and it states that the "execute"
function returns the number of rows affected though it's not confirmed to
work for all the database.

Is there any solution which will surely work on all databases ?

Thanks & Regards,
Amit Saxena

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




Re: program fails to write some entries in log

2008-07-10 Thread icarus
On Jul 10, 8:36 am, [EMAIL PROTECTED] (John W. Krahn) wrote:
> icarus wrote:
> > what this does: it classifies a file based on its modification date.
>
> > example: xfile1 is dated July 9, 2008.  If it doesn't exist, the
> > program creates a
> > directory structure 2008/July/09 and places xfile1 there.
> > Then it creates a log with the steps done.
>
> > So...in the system the result is 2008/July/09/xfile1
> > The log entry looks like "xfile1 => 2008/July/09"
>
> > problem: When I send an INT signal (Control+C) from the keyboard for
> > example,
> > it exists nicely, it finishes whatever file it needed to finish, and
> > it classifies the file
> > properly.  However, it misses some entries on the log file.  Out of
> > 4,000 files if you INT the program twice or three times it won't write
> > about 2 or 3 entries on the log.
>
> It sounds like you are "Suffering from 
> Buffering"http://perl.plover.com/FAQs/Buffering.html
>
> What you need to do is open the log file once at the beginning of the
> program and then apply autoflush to the log filehandle.
>
> > How can I not lose an entry on the log file? if you have other
> > suggestions to make this code
> > more efficient please let me know. Thanks in advance.
>
> Perhaps something like this (UNTESTED):
>
> #!/usr/bin/perl
> use warnings;
> use strict;
> use File::Copy 'move';
> use File::Path 'mkpath';
> use POSIX 'strftime';
>
> my @target_directories = (
>  '/full/path/to/test1',
>  '/full/path/to/test2',
>  );
> my $files_moved = 'test.log';
> my $condition   = 0;
>
> open my $LOG, '>>', $files_moved or die "Cannot open '$files_moved' $!";
> select $LOG;
> $| = 1; #flush buffers on $files_moved_fh
>
> my @all_signals = qw( STOP KILL ABRT QUIT TERM INT TSTP HUP );
> sub signal_handler {
>  my $signal = shift;
>  $condition = 1;
>  }
> for my $list ( @all_signals ) {
>  $SIG{ $list } = \&signal_handler;
>  }
>
> for my $xdir ( @target_directories ) {
>  chdir $xdir or die "Cannot chdir '$xdir' $!";
>  opendir my $source_dir, '.' or die "Cannot open '$xdir' $!";
>
>  while ( my $xfile = readdir $source_dir ) {
>
>  # skip directories including . and ..
>  next if -d $xfile;
>
>  #extract year, month, day when $xfile was last modified
>  # eg 2008/July/9
>  my $dir = strftime '%Y/%b/%-d', localtime( ( lstat $xfile )[9] )
>
>  if ( ! -e $dir ) {
>  mkpath [ $dir ], 0, 0750 or die "Cannot mkpath '$dir' $!";
>  }
>  move $xfile, $dir or die "Cannot move '$xfile' $!";
>  # print to the autoflushed log file now
>  print "$xfile => $dir\n";
>  exit if $condition;
>  }
>  }
>
> __END__
>
> John
> --
> Perl isn't a toolbox, but a small machine shop where you
> can special-order certain sorts of tools at low cost and
> in short order.-- Larry Wall


John, you da man!!

That tip you gave me was right on the money.
Yes, I was suffering from buffering.  I really appreciate your help.
Thanks.


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




Re: using Expect.pm

2008-07-10 Thread Richard Lee





was able to make it work but not sure if I am doing the right thing.

use warnings;
use strict;

use Expect;

my $exp = new Expect;
my $password = 'abc123';
my $user = 'userX';
my $host = '10.3.3.1';

# typical regex pattern for end of /bin/sh prompt:
my $shell_prompt = qr/[\$\#]\s*$/;

my $login =
 "/usr/bin/ssh [EMAIL PROTECTED]";

$exp->spawn($login)
or die
   "Can't login to $host as $user: $!\n";

## Enables internal debugging
#$exp->exp_internal( 1 );

$exp->log_file("./log_this.txt");

$exp->expect(5, '-re', '(RSA)');
my @matchlist = $exp->matchlist;

$exp->send("yes\n")
if (  ( $exp->matchlist)[0] );

$exp->expect(5, "password: ");
$exp->send("$password\n");

$exp->expect(2, -re => $shell_prompt);
$exp->send("cat /tmp/file1\n");
my @result = $exp->expect(5, -re => $shell_prompt);

my $ltedge_now = $result[1];
print "\n\n\n $ltedge_now\n";

$exp->send("exit\n");
$exp->soft_close();
$exp->log_file(undef);

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




Re: program fails to write some entries in log

2008-07-10 Thread Rob Dixon

John W. Krahn wrote:
> Rob Dixon wrote:
>> John W. Krahn wrote:
>>>  #extract year, month, day when $xfile was last modified
>>>  # eg 2008/July/9
>>>  my $dir = strftime '%Y/%b/%-d', localtime( ( lstat $xfile )[9] )
>> I hoped that was what I was looking for, but the format %- creates nothing 
>> and
>> the 'd' is simply.
> 
> It works here:
> 
> $ perl -MPOSIX -le'print strftime q[%Y/%b/%d * %Y/%b/%-d], localtime 
> '
> 1971/Oct/05 * 1971/Oct/5

C:\WINDOWS.0>perl -MPOSIX -le"print strftime q[%Y/%b/%d * %Y/%b/%-d], localtime
"
1971/Oct/06 * 1971/Oct/d

C:\WINDOWS.0>perl -v

This is perl, v5.8.8 built for MSWin32-x86-multi-thread
(with 25 registered patches, see perl -V for more detail)

Copyright 1987-2006, Larry Wall

Binary build 817 [257965] provided by ActiveState http://www.ActiveState.com
Built Mar 20 2006 17:54:25

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

>>>  if ( ! -e $dir ) {
>>>  mkpath [ $dir ], 0, 0750 or die "Cannot mkpath '$dir' $!";
>>>  }
>> There is no problem with calling mkpath on an already-existing directory. 
>> OTOH
>> it is documented to throw a fatal error if the path cannot be created so 
>> there
>> is no point in checking the return value (which is the number of directories
>> created by the call).
> 
> perldoc File::Path
> [ snip ]
>  ERROR HANDLING
> 
>  If "mkpath" or "rmtree" encounter an error, a diagnostic message
>  will be printed to "STDERR" via "carp" (for non-fatal errors), or
>  via "croak" (for fatal errors).
> 
> So I guess neither of us are correct.   :-)

Also from perldoc File::Path:

If a system error prevents a directory from being created, then the
"mkpath" function throws a fatal error with "Carp::croak". This error
can be trapped with an "eval" block

Now I read that as meaning, "If a directory cannot be created", as I'm not sure
what a 'system error' might be. I know it carps on Windows if I try to create a
directory on a non-existent volume, like 'Q:\mydir'. But there's no such thing
as an invalid path on Unix as far as I can think.

Rob

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




Re: program fails to write some entries in log

2008-07-10 Thread John W. Krahn

Rob Dixon wrote:

John W. Krahn wrote:

 #extract year, month, day when $xfile was last modified
 # eg 2008/July/9
 my $dir = strftime '%Y/%b/%-d', localtime( ( lstat $xfile )[9] )


I hoped that was what I was looking for, but the format %- creates nothing and
the 'd' is simply.


It works here:

$ perl -MPOSIX -le'print strftime q[%Y/%b/%d * %Y/%b/%-d], localtime 
'

1971/Oct/05 * 1971/Oct/5



 if ( ! -e $dir ) {
 mkpath [ $dir ], 0, 0750 or die "Cannot mkpath '$dir' $!";
 }


There is no problem with calling mkpath on an already-existing directory. OTOH
it is documented to throw a fatal error if the path cannot be created so there
is no point in checking the return value (which is the number of directories
created by the call).


perldoc File::Path
[ snip ]
ERROR HANDLING

If "mkpath" or "rmtree" encounter an error, a diagnostic message
will be printed to "STDERR" via "carp" (for non-fatal errors), or
via "croak" (for fatal errors).

So I guess neither of us are correct.   :-)



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

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




Re: program fails to write some entries in log

2008-07-10 Thread Rob Dixon

John W. Krahn wrote:
> 
>  #extract year, month, day when $xfile was last modified
>  # eg 2008/July/9
>  my $dir = strftime '%Y/%b/%-d', localtime( ( lstat $xfile )[9] )

I hoped that was what I was looking for, but the format %- creates nothing and
the 'd' is simply.

> 
>  if ( ! -e $dir ) {
>  mkpath [ $dir ], 0, 0750 or die "Cannot mkpath '$dir' $!";
>  }

There is no problem with calling mkpath on an already-existing directory. OTOH
it is documented to throw a fatal error if the path cannot be created so there
is no point in checking the return value (which is the number of directories
created by the call).

Rob

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




Re: program fails to write some entries in log

2008-07-10 Thread John W. Krahn

icarus wrote:

what this does: it classifies a file based on its modification date.

example: xfile1 is dated July 9, 2008.  If it doesn't exist, the
program creates a
directory structure 2008/July/09 and places xfile1 there.
Then it creates a log with the steps done.

So...in the system the result is 2008/July/09/xfile1
The log entry looks like "xfile1 => 2008/July/09"

problem: When I send an INT signal (Control+C) from the keyboard for
example,
it exists nicely, it finishes whatever file it needed to finish, and
it classifies the file
properly.  However, it misses some entries on the log file.  Out of
4,000 files if you INT the program twice or three times it won't write
about 2 or 3 entries on the log.


It sounds like you are "Suffering from Buffering" 
http://perl.plover.com/FAQs/Buffering.html


What you need to do is open the log file once at the beginning of the 
program and then apply autoflush to the log filehandle.



How can I not lose an entry on the log file? if you have other
suggestions to make this code
more efficient please let me know. Thanks in advance.


Perhaps something like this (UNTESTED):

#!/usr/bin/perl
use warnings;
use strict;
use File::Copy 'move';
use File::Path 'mkpath';
use POSIX 'strftime';

my @target_directories = (
'/full/path/to/test1',
'/full/path/to/test2',
);
my $files_moved = 'test.log';
my $condition   = 0;

open my $LOG, '>>', $files_moved or die "Cannot open '$files_moved' $!";
select $LOG;
$| = 1; #flush buffers on $files_moved_fh

my @all_signals = qw( STOP KILL ABRT QUIT TERM INT TSTP HUP );
sub signal_handler {
my $signal = shift;
$condition = 1;
}
for my $list ( @all_signals ) {
$SIG{ $list } = \&signal_handler;
}


for my $xdir ( @target_directories ) {
chdir $xdir or die "Cannot chdir '$xdir' $!";
opendir my $source_dir, '.' or die "Cannot open '$xdir' $!";

while ( my $xfile = readdir $source_dir ) {

# skip directories including . and ..
next if -d $xfile;

#extract year, month, day when $xfile was last modified
# eg 2008/July/9
my $dir = strftime '%Y/%b/%-d', localtime( ( lstat $xfile )[9] )

if ( ! -e $dir ) {
mkpath [ $dir ], 0, 0750 or die "Cannot mkpath '$dir' $!";
}
move $xfile, $dir or die "Cannot move '$xfile' $!";
# print to the autoflushed log file now
print "$xfile => $dir\n";
exit if $condition;
}
}

__END__



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

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




Re: program fails to write some entries in log

2008-07-10 Thread Rob Dixon

icarus wrote:
> what this does: it classifies a file based on its modification date.
> 
> example: xfile1 is dated July 9, 2008.  If it doesn't exist, the
> program creates a
> directory structure 2008/July/09 and places xfile1 there.
> Then it creates a log with the steps done.
> 
> So...in the system the result is 2008/July/09/xfile1
> The log entry looks like "xfile1 => 2008/July/09"
> 
> problem: When I send an INT signal (Control+C) from the keyboard for
> example,
> it exists nicely, it finishes whatever file it needed to finish, and
> it classifies the file
> properly.  However, it misses some entries on the log file.  Out of
> 4,000 files if you INT the program twice or three times it won't write
> about 2 or 3 entries on the log.
> 
> How can I not lose an entry on the log file? if you have other
> suggestions to make this code
> more efficient please let me know. Thanks in advance.
> 
> Code:
> 
> #!/usr/bin/perl
> use warnings;
> use strict;
> use File::Copy;
> use File::Path;
> use File::Find;
> use Time::localtime;
> no warnings 'File::Find';
> 
> 
> my @target_directories = ("/full/path/to/test1", "/full/path/to/
> test2");
> my $files_moved = "test.log";
> my $files_moved_fh;
> my $source_dir_handler;
> my $signal;
> my $list;
> my $condition = 1;
> 
> 
> $| = 1; #flush buffers
> 
> my @months =
> ("Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec");
> my @all_signals = qw (STOP KILL ABRT QUIT TERM INT TSTP HUP);
> 
> sub signal_handler {
> $signal = shift;
> $condition = 0;
> }
> 
> 
> find( { wanted => \&process, no_chdir => 0 }, @target_directories );
> 
> sub process {
> 
> foreach $list(@all_signals){
> $SIG{$list} = \&signal_handler;
> }
> 
> while ( $condition == 1 ) {
> foreach my $xdir (@target_directories){
>   opendir ($source_dir_handler, $xdir) or die $!;
>   chdir $xdir;
> 
>   while (defined (my $xfile = readdir 
> ($source_dir_handler))){
> 
>   #skip . and .. files and directories
>   next if ($xfile =~ m/^\./);  #skip dot 
> files . and ..
>   next if -d $xfile;
> 
>   #extract year, month, day when $_ was last 
> modified
>   my $calendar = 
> localtime((stat($xfile))[9]);
>   my $dd = $calendar->mday;
>   my $mm = $calendar->mon;
>   my $ = $calendar->year+1900;
>   my $dir = $ . '/' . $months[$mm] . 
> '/' . $dd;  #eg 2008/July/
> 9
> 
>   if (! -d $dir and ! -e $dir ){
>   mkpath([$dir], 0, 0750) or die 
> $!;
>   move ($xfile, $dir) or die $!;
>   open ($files_moved_fh, ">>", 
> $files_moved) or die $!;
>   print $files_moved_fh "$xfile => 
> $dir\n";
>   close $files_moved_fh;
>   }
>   else {
>   move ($xfile, $dir) or die $!;
>   open ($files_moved_fh, ">>", 
> $files_moved) or die $!;
>   print $files_moved_fh "$xfile => 
> $dir\n";
>   close $files_moved_fh;
>   }
> 
>   }
> 
>   }
>$condition = 0;
>   }
>  }


You're misunderstanding the way File::Find works and doing all of its work for
it inside process()!

File::Find calls the wanted routine for every file and directory it finds at or
below the paths specified, but you've written process() so that it looks through
the directories again itself.

Because File::Find it meant for traversing entire directory trees, and you're
only interested in the contents of a single directory, I would forget about
using the module.

I can't see straight away why you're missing entries from the log file, but I
suggest you fix the bugs first. The program below should do the trick.

I've used the strftime function from the POSIX module to format the destination
path (it's a standard module so won't need installing) and you may as well call
mkpath whether or not the directory already exists - there's no harm done if it
has nothing to do.

HTH,

Rob



use warnings;
use strict;

use File::Copy;
use File::Path;
use POSIX qw/strftime/;

my @target_directories = qw(C:/ /full/path/to/test1  /full/path/to/test2);
my $files_moved = 'test.log';
my $signal;
my $signalled;

$| = 1; #flush buffers

my @all_signals = qw (STOP KILL ABRT QUIT TERM INT TSTP HUP);

sub signal_handler {
  $signal = shift;
  $signalled++;
}

foreach my

Re: Array indexing question

2008-07-10 Thread Brad Baxter
On Jul 10, 5:59 am, [EMAIL PROTECTED] (Anirban Adhikary)
wrote:
> Dear list
> I want to capture the output of w and then I want to do some job as per the
> o/p of w command in my linux system. So i have written the code as follows
>
> use strict;
> use warnings;
>
> open (LS, "w|") or die "can't open w: $!";

Success for pipes should be checked when you close them.

> my @arr = ;
> close (LS);
> shift @arr;
> shift @arr;
> my($one,$two,$three,$four,$five,$six,$seven,$eight);
>
> foreach my $el(@arr)
>  {
>   ($one,$two,$three,$four,$five,$six,$seven,$eight) = split(/ /,$el);

Change that to split " ".  When you split / / you get fields in
between
each space.

--
Brad


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




program fails to write some entries in log

2008-07-10 Thread icarus
what this does: it classifies a file based on its modification date.

example: xfile1 is dated July 9, 2008.  If it doesn't exist, the
program creates a
directory structure 2008/July/09 and places xfile1 there.
Then it creates a log with the steps done.

So...in the system the result is 2008/July/09/xfile1
The log entry looks like "xfile1 => 2008/July/09"

problem: When I send an INT signal (Control+C) from the keyboard for
example,
it exists nicely, it finishes whatever file it needed to finish, and
it classifies the file
properly.  However, it misses some entries on the log file.  Out of
4,000 files if you INT the program twice or three times it won't write
about 2 or 3 entries on the log.

How can I not lose an entry on the log file? if you have other
suggestions to make this code
more efficient please let me know. Thanks in advance.

Code:

#!/usr/bin/perl
use warnings;
use strict;
use File::Copy;
use File::Path;
use File::Find;
use Time::localtime;
no warnings 'File::Find';


my @target_directories = ("/full/path/to/test1", "/full/path/to/
test2");
my $files_moved = "test.log";
my $files_moved_fh;
my $source_dir_handler;
my $signal;
my $list;
my $condition = 1;


$| = 1; #flush buffers

my @months =
("Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec");
my @all_signals = qw (STOP KILL ABRT QUIT TERM INT TSTP HUP);

sub signal_handler {
$signal = shift;
$condition = 0;
}


find( { wanted => \&process, no_chdir => 0 }, @target_directories );

sub process {

foreach $list(@all_signals){
$SIG{$list} = \&signal_handler;
}

while ( $condition == 1 ) {
foreach my $xdir (@target_directories){
opendir ($source_dir_handler, $xdir) or die $!;
chdir $xdir;

while (defined (my $xfile = readdir 
($source_dir_handler))){

#skip . and .. files and directories
next if ($xfile =~ m/^\./);  #skip dot 
files . and ..
next if -d $xfile;

#extract year, month, day when $_ was last 
modified
my $calendar = 
localtime((stat($xfile))[9]);
my $dd = $calendar->mday;
my $mm = $calendar->mon;
my $ = $calendar->year+1900;
my $dir = $ . '/' . $months[$mm] . 
'/' . $dd;  #eg 2008/July/
9

if (! -d $dir and ! -e $dir ){
mkpath([$dir], 0, 0750) or die 
$!;
move ($xfile, $dir) or die $!;
open ($files_moved_fh, ">>", 
$files_moved) or die $!;
print $files_moved_fh "$xfile => 
$dir\n";
close $files_moved_fh;
}
else {
move ($xfile, $dir) or die $!;
open ($files_moved_fh, ">>", 
$files_moved) or die $!;
print $files_moved_fh "$xfile => 
$dir\n";
close $files_moved_fh;
}

}

}
 $condition = 0;
  }
 }


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




Can't locate ...

2008-07-10 Thread protoplasm
Somehow I screwed up my `Perl` and have no idea how to get out of this
mess. I attempt to run a program and get the following:

@naiad ~/workspace/aestTest] ./aestTest --CbcDec
Can't locate Mac/Files.pm in @INC (@INC contains: /opt/local/lib/
perl5/5.8.8/darwin-2level /opt/local/lib/perl5/5.8.8 /opt/local/lib/
perl5/site_perl/5.8.8/darwin-2level /opt/local/lib/perl5/site_perl/
5.8.8 /opt/local/lib/perl5/site_perl /opt/local/lib/perl5/vendor_perl/
5.8.8/darwin-2level /opt/local/lib/perl5/vendor_perl/5.8.8 /opt/local/
lib/perl5/vendor_perl .) at /opt/local/lib/perl5/site_perl/5.8.8/File/
HomeDir/Darwin.pm line 34.

Next, I try to start cpan to install Mac::Files:

@naiad ~/workspace/aestTest] cpan
CPAN: File::HomeDir loaded ok (v0.80)
Can't locate Mac/Files.pm in @INC (@INC contains: /opt/local/lib/
perl5/5.8.8/darwin-2level /opt/local/lib/perl5/5.8.8 /opt/local/lib/
perl5/site_perl/5.8.8/darwin-2level /opt/local/lib/perl5/site_perl/
5.8.8 /opt/local/lib/perl5/site_perl /opt/local/lib/perl5/vendor_perl/
5.8.8/darwin-2level /opt/local/lib/perl5/vendor_perl/5.8.8 /opt/local/
lib/perl5/vendor_perl /Users/sprotsman/workspace/aestTest) at /opt/
local/lib/perl5/site_perl/5.8.8/File/HomeDir/Darwin.pm line 58.

Uhg! I'm running Mac OS 10.5.4. Any help is much appreciated.


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




Re: Array indexing question

2008-07-10 Thread Stephen Kratzer
On Thursday 10 July 2008 10:33:31 Jay Savage wrote:
> On Thu, Jul 10, 2008 at 8:48 AM, Stephen Kratzer <[EMAIL PROTECTED]> wrote:
> > Anirban,
> >
> > The output of 'w' is delimited by whitespace, not necessarily a single
> > space. Try passing the pattern '\w+' to split. Something like this:
>
> I think you meant "the '\s+' pattern".
>
> -- j
> --
> This email and attachment(s): [ ] blogable; [ x ] ask first; [ ]
> private and confidential
>
> daggerquill [at] gmail [dot] com
> http://www.tuaw.com http://www.downloadsquad.com http://www.engatiki.org
>
> values of β will give rise to dom!

Right you are. Thinking one thing and typing another.

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




Re: Count of number of rows returned in delete / update sql in Perl DBI ?

2008-07-10 Thread Amit Saxena
Hi !

I also referred to the CPAN page before posting this query.

It's already working for me with Oracle 10g on Perl 5.8.4 on RHEL.

Though somewhere on the web, I read a post by some perl developer that
execute for him is not returning the number of
rows affected.

That's why, I asked the query.

Regards,
Amit Saxena

On Thu, Jul 10, 2008 at 8:12 PM, Rob Dixon <[EMAIL PROTECTED]> wrote:

>
> Amit Saxena wrote:
> > Hi all,
> >
> > I want to know how to get the count of number of rows returned in delete
> /
> > update sql in Perl DBI ?
> >
> > I have ready some articles on the internet and it states that the
> "execute"
> > function returns the number of rows affected though it's not confirmed to
> > work for all the database.
> >
> > Is there any solution which will surely work on all databases ?
>
> I suggest that you simply test to make sure that it works on your database.
> 'execute' will return -1 if the number of rows affected is unknown.
>
> Rob
>


Re: Using a Perl script in AIX / Unix

2008-07-10 Thread Rob Dixon

John W. Krahn wrote:
> Rob Dixon wrote:
>> Rob McGinness wrote:
>>> die unless chdir
>>> "/cert/ImpactServer-5_4/cl9/ctrl_sfm9/sfm9_sched/archives";
>>>
>>> die unless opendir DIR, ".";
>> I suggest you write something like the program below.
>>
>>
>> #!/usr/bin/perl
>> use strict;
>> use warnings;
>>
>> my $dir = "/cert/ImpactServer-5_4/cl9/ctrl_sfm9/sfm9_sched/archives";
>>
>> my @files = do {
>>   opendir my $dh, $dir or die $!;
>>   grep /^Completed\.archive\.\d{1,4}$/, readdir $dh;
>> };
>>
>> @files = sort grep -M > 7, @files;
> 
> The OP chdir()ed to the directory in question so the files were in the 
> current directory, however your code does not so the -M test will not 
> work unless you prepend the directory name to the file name:
> 
> @files = sort grep -M "$dir/$_" > 7, @files;
> 
> 
> Also, why not put both tests in the first grep:
> 
> my @files = do {
>opendir my $dh, $dir or die "Cannot open '$dir' $!";
>grep /^Completed\.archive\.\d{1,4}$/
> && -M "$dir/$_" > 7, readdir $dh;
> };
> 
> 
>> foreach (@files) {
>>   system 'compress', '-v', $_;
>> }
> 
> You should verify that system worked correctly:
> 
> foreach (@files) {
>0 == system 'compress', '-v', $_ or die "system 'compress' failed: $?";
> }

Thanks John

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




Re: Count of number of rows returned in delete / update sql in Perl DBI ?

2008-07-10 Thread Rob Dixon

Amit Saxena wrote:
> Hi all,
> 
> I want to know how to get the count of number of rows returned in delete /
> update sql in Perl DBI ?
> 
> I have ready some articles on the internet and it states that the "execute"
> function returns the number of rows affected though it's not confirmed to
> work for all the database.
> 
> Is there any solution which will surely work on all databases ?

I suggest that you simply test to make sure that it works on your database.
'execute' will return -1 if the number of rows affected is unknown.

Rob

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




Re: Array indexing question

2008-07-10 Thread Rob Dixon

Stephen Kratzer wrote:
> On Thursday 10 July 2008 05:59:36 Anirban Adhikary wrote:
>> Dear list
>> I want to capture the output of w and then I want to do some job as per the
>> o/p of w command in my linux system. So i have written the code as follows
>>
>> use strict;
>> use warnings;
>>
>> open (LS, "w|") or die "can't open w: $!";
>> my @arr = ;
>> close (LS);
>> shift @arr;
>> shift @arr;
>> my($one,$two,$three,$four,$five,$six,$seven,$eight);
>>
>> foreach my $el(@arr)
>>  {
>>   ($one,$two,$three,$four,$five,$six,$seven,$eight) = split(/ /,$el);
>>print $five."\n";
>>  }
>> but the problem is as per the o/p of w command I want to extract the fifth
>> field but by using the above code I am not able to do this. The o/p of who
>> command in my system is as follows
>>
>> USER TTY  FROM  LOGIN@   IDLE   JCPU   PCPU WHAT
>> bighosh  pts/1bighosh.in.conne 13:48   38:03   0.16s  0.06s sshd:
>> bighosh [priv]
>> nchatter pts/2nchatterjee.in.c 10:31   15:38   0.68s  0.65s -bash
>> anadhika pts/3anadhikary.in.co 15:100.00s  0.04s  0.00s w
>> suraypts/4suray1.in.connec 12:36   46:43   0.04s  0.04s -bash
>> mergepts/5sasarkar.in.conn 12:005:44   0.11s  0.11s -bash
>> dpghosh  pts/6dpghosh.in.conne 12:091:32   0.07s  0.01s sshd:
>> dpghosh [priv]
>> anadhika pts/7anadhikary.in.co 13:50   10.00s  0.12s  0.12s -bash
>>
>> I need to extract the idle column. But using my code I am getting output
>> like this
>>
>>
>> pts/4
>> pts/5
>>
>> Where I am making the mistake please rectify.
>>
>> Thanks & Regards in advance
>> Anirban Adhikary
> 
> Anirban,
> 
> The output of 'w' is delimited by whitespace, not necessarily a single space. 
> Try passing the pattern '\w+' to split. Something like this:

I think you mean /\s+/.

> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> 
> open (LS, "w|") or die "can't open w: $!";
> 
> ;
> ;
> 
> while () {
> print ((split)[4], "\n");
> }

split with no parameters defaults to a separator pattern of ' ', i.e. a literal
space, which is a special case. It has the same effect as splitting on /\s+/ but
also discards any empty leading field in the case where the object string starts
with whitespace, and is exactly what is wanted here.

Rob

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




Re: Array indexing question

2008-07-10 Thread Jay Savage
On Thu, Jul 10, 2008 at 8:48 AM, Stephen Kratzer <[EMAIL PROTECTED]> wrote:
>
> Anirban,
>
> The output of 'w' is delimited by whitespace, not necessarily a single space.
> Try passing the pattern '\w+' to split. Something like this:
>

I think you meant "the '\s+' pattern".

-- j
--
This email and attachment(s): [ ] blogable; [ x ] ask first; [ ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com http://www.downloadsquad.com http://www.engatiki.org

values of β will give rise to dom!


Re: Array indexing question

2008-07-10 Thread Jay Savage
On Thu, Jul 10, 2008 at 5:59 AM, Anirban Adhikary
<[EMAIL PROTECTED]> wrote:
> Dear list
> I want to capture the output of w and then I want to do some job as per the
> o/p of w command in my linux system. So i have written the code as follows
>
> open (LS, "w|") or die "can't open w: $!";
> my @arr = ;
> close (LS);


Hi Anriban,

There are actually a couple of things you need to look at, here.

First, Perl has the backtick operator for exactly this situation:

 my @array = `w`;

See perlop for details


> shift @arr;
> shift @arr;


You may want to look at splice(), here, for simplicity's sake, or just
use an array slice later.


> my($one,$two,$three,$four,$five,$six,$seven,$eight);
>


This is going to get you in troble at some point. Because you declare
the variables outside the loop, they won't be reset inside the loop,
and if for some reason, you get malformed data in one line of the
system call, you'll get the stored result from the previous successful
pass. put the my declatation inside the loop to reset the variables on
each pass.


> foreach my $el(@arr)
>  {
>  ($one,$two,$three,$four,$five,$six,$seven,$eight) = split(/ /,$el);
>   print $five."\n";


You don't really need all those variables, though, so why type them
out? Just use an array:

foreach my $el (@rr) {
my @entry = split /\s+/, $el;
print $entry[4];
}

Better yet, just use a list slice to skip the temporary variable
entirely (this is where you can use an array slice to cut out those
shifts, too):

foreach my $el (@arr[2,-1]) {
print(split /\s+/, $el)[4]
 }


> but the problem is as per the o/p of w command I want to extract the fifth
> field but by using the above code I am not able to do this. The o/p of who
> command in my system is as follows
>
> USER TTY  FROM  LOGIN@   IDLE   JCPU   PCPU WHAT
> bighosh  pts/1bighosh.in.conne 13:48   38:03   0.16s  0.06s sshd:
[snip]
>
> I need to extract the idle column. But using my code I am getting output
> like this
>
>
> pts/4
> pts/5
>
> Where I am making the mistake please rectify.
>


As Stephen pointed out, is that the w command returns output with
multiple spaces between columns, and you are splitting on every space,
so you need 'split /\s+/', not 'split / /'

HTH,

-- jay
--
This email and attachment(s): [ ] blogable; [ x ] ask first; [ ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com http://www.downloadsquad.com http://www.engatiki.org

values of β will give rise to dom!


Re: Count of number of rows returned in delete / update sql in Perl DBI ?

2008-07-10 Thread yitzle
On Thu, Jul 10, 2008 at 7:44 AM, Amit Saxena <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> I want to know how to get the count of number of rows returned in delete /
> update sql in Perl DBI ?
>
> I have ready some articles on the internet and it states that the "execute"
> function returns the number of rows affected though it's not confirmed to
> work for all the database.
>
> Is there any solution which will surely work on all databases ?
>
> Thanks & Regards,
> Amit Saxena
>

The CPAN page says execute() returns the number of rows affected...
I'm not sure why that would be doubted.
You can always do a "SELECT COUNT" query first to get the count of
rows that match the given condition.

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




Re: Array indexing question

2008-07-10 Thread Stephen Kratzer
On Thursday 10 July 2008 05:59:36 Anirban Adhikary wrote:
> Dear list
> I want to capture the output of w and then I want to do some job as per the
> o/p of w command in my linux system. So i have written the code as follows
>
> use strict;
> use warnings;
>
> open (LS, "w|") or die "can't open w: $!";
> my @arr = ;
> close (LS);
> shift @arr;
> shift @arr;
> my($one,$two,$three,$four,$five,$six,$seven,$eight);
>
> foreach my $el(@arr)
>  {
>   ($one,$two,$three,$four,$five,$six,$seven,$eight) = split(/ /,$el);
>print $five."\n";
>  }
> but the problem is as per the o/p of w command I want to extract the fifth
> field but by using the above code I am not able to do this. The o/p of who
> command in my system is as follows
>
> USER TTY  FROM  LOGIN@   IDLE   JCPU   PCPU WHAT
> bighosh  pts/1bighosh.in.conne 13:48   38:03   0.16s  0.06s sshd:
> bighosh [priv]
> nchatter pts/2nchatterjee.in.c 10:31   15:38   0.68s  0.65s -bash
> anadhika pts/3anadhikary.in.co 15:100.00s  0.04s  0.00s w
> suraypts/4suray1.in.connec 12:36   46:43   0.04s  0.04s -bash
> mergepts/5sasarkar.in.conn 12:005:44   0.11s  0.11s -bash
> dpghosh  pts/6dpghosh.in.conne 12:091:32   0.07s  0.01s sshd:
> dpghosh [priv]
> anadhika pts/7anadhikary.in.co 13:50   10.00s  0.12s  0.12s -bash
>
> I need to extract the idle column. But using my code I am getting output
> like this
>
>
> pts/4
> pts/5
>
> Where I am making the mistake please rectify.
>
> Thanks & Regards in advance
> Anirban Adhikary

Anirban,

The output of 'w' is delimited by whitespace, not necessarily a single space. 
Try passing the pattern '\w+' to split. Something like this:

#!/usr/bin/perl

use strict;
use warnings;

open (LS, "w|") or die "can't open w: $!";

;
;

while () {
print ((split)[4], "\n");
}

Stephen Kratzer
Network Engineer
CTI Networks, Inc.

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




Count of number of rows returned in delete / update sql in Perl DBI ?

2008-07-10 Thread Amit Saxena
Hi all,

I want to know how to get the count of number of rows returned in delete /
update sql in Perl DBI ?

I have ready some articles on the internet and it states that the "execute"
function returns the number of rows affected though it's not confirmed to
work for all the database.

Is there any solution which will surely work on all databases ?

Thanks & Regards,
Amit Saxena


Array indexing question

2008-07-10 Thread Anirban Adhikary
Dear list
I want to capture the output of w and then I want to do some job as per the
o/p of w command in my linux system. So i have written the code as follows

use strict;
use warnings;

open (LS, "w|") or die "can't open w: $!";
my @arr = ;
close (LS);
shift @arr;
shift @arr;
my($one,$two,$three,$four,$five,$six,$seven,$eight);

foreach my $el(@arr)
 {
  ($one,$two,$three,$four,$five,$six,$seven,$eight) = split(/ /,$el);
   print $five."\n";
 }
but the problem is as per the o/p of w command I want to extract the fifth
field but by using the above code I am not able to do this. The o/p of who
command in my system is as follows

USER TTY  FROM  LOGIN@   IDLE   JCPU   PCPU WHAT
bighosh  pts/1bighosh.in.conne 13:48   38:03   0.16s  0.06s sshd:
bighosh [priv]
nchatter pts/2nchatterjee.in.c 10:31   15:38   0.68s  0.65s -bash
anadhika pts/3anadhikary.in.co 15:100.00s  0.04s  0.00s w
suraypts/4suray1.in.connec 12:36   46:43   0.04s  0.04s -bash
mergepts/5sasarkar.in.conn 12:005:44   0.11s  0.11s -bash
dpghosh  pts/6dpghosh.in.conne 12:091:32   0.07s  0.01s sshd:
dpghosh [priv]
anadhika pts/7anadhikary.in.co 13:50   10.00s  0.12s  0.12s -bash

I need to extract the idle column. But using my code I am getting output
like this


pts/4
pts/5

Where I am making the mistake please rectify.

Thanks & Regards in advance
Anirban Adhikary