loop does select next line

2014-07-05 Thread Sunita Pradhan
I have a set of code for count number of lines and number of words . 

#!/usr/bin/perl

while ($line =  STDIN){
chomp ($line);
$hash{L_c_start}++ if ($line =~ /^C.*/i);
@words = split /\s+/,$line;
foreach $c (keys @words){
   print word $words[$c]\n;
   $hash{W_c_start}++ if ($words[$c] =~ /^C.*/i);
   
} 
}

print $_ :: $hash{$_}\n foreach (keys %hash);
=

I want to use while loop instead of foreach . I tried it but while loop 
does not select  2nd line of STDIN .

-Sunita
  

Re: loop does select next line

2014-07-05 Thread Kent Fredric
On 6 July 2014 02:31, Sunita Pradhan sunita.pradhan.2...@hotmail.com
wrote:

 I have a set of code for count number of lines and number of words .

 #!/usr/bin/perl

 while ($line =  STDIN){
 chomp ($line);
 $hash{L_c_start}++ if ($line =~ /^C.*/i);
 @words = split /\s+/,$line;
 *foreach $c (keys @words){*
print word $words[$c]\n;
$hash{W_c_start}++ if ($words[$c] =~ /^C.*/i);
 }
 }

 print $_ :: $hash{$_}\n foreach (keys %hash);
 =

 I want to use while loop instead of foreach . I tried it but while
 loop does not select  2nd line of STDIN .




I tried it but while loop does not select  2nd line of STDIN .

Can you show us the code after you tried that?

Why do you want to use a while loop instead of foreach?

Remember:

  while(  condition ) {  }

Executes the condition on each iteration, so using

 while( keys @words ) { }

means the loop will continue as long as @words returns a non-zero number of
keys.

This is different from

 for my $i ( keys @words ) { }

Where the code will be evaluated once, and the result iterated.

Though I'm not entirely understanding why you've used the unusual

*foreach $c (keys @words){*
   print word $words[$c]\n;
   $hash{W_c_start}++ if ($words[$c] =~ /^C.*/i);
}

Instead of the more natural

*foreach my $word (@words){*
   print word $word\n;
   $hash{W_c_start}++ if ($word =~ /^C.*/i);
}


Personally, I'd be inclined to structure code as follows if a while loop
was really wanted:

while( @words ){
   my ($word) = shift @words;
   print word $word\n;
   $hash{W_c_start}++ if ($word =~ /^C.*/i);
}

And of course, standard recommendation: your code lacked 'use strict' and
'use warnings', and both are highly recommended.

-- 
Kentnl


Re: Next subnet

2013-05-27 Thread Dr.Ruud

On 26/05/2013 14:40, shawn wilson wrote:


Thank y'all, I got to where I want to be:
https://github.com/ag4ve/geocidr


 ...
 or grep { ! m%[0-9\.\/]+% } @{$opts-{ip}}
 or scalar(@{$opts-{ip}})  1

The '+' in the regexp is superfluous as-is.
(your regexp isn't anchored)


You probably meant it more like:

...
or !@{$opts-{ip}}
or grep m{[^0-9./]}, @{$opts-{ip}}

--
Ruud


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Next subnet

2013-05-27 Thread shawn wilson
On May 27, 2013 1:02 PM, Dr.Ruud rvtol+use...@isolution.nl wrote:

 On 26/05/2013 14:40, shawn wilson wrote:

 Thank y'all, I got to where I want to be:
 https://github.com/ag4ve/geocidr


  ...
  or grep { ! m%[0-9\.\/]+% } @{$opts-{ip}}
  or scalar(@{$opts-{ip}})  1

 The '+' in the regexp is superfluous as-is.
 (your regexp isn't anchored)


 You probably meant it more like:

 ...
 or !@{$opts-{ip}}
 or grep m{[^0-9./]}, @{$opts-{ip}}


You don't want to grep for anything that isn't a number there. I like the
!@arr vs my scalar though. And...

You're right on anchoring - I should. But as I'm lastly matching for an IP
(with a possible subnet, it should probably be more like:
m(^[0-9\./]+$)
Or better:
m(^[0-9\.]+(?:/(?:[0-3])?[0-9])?$)
Or more better:
m(^(?:(?:[0-2])?[0-9]{1,2}\.){3}(?:[0-2])?[0-9]{1,2}(?:/(?:[0-3])?[0-9])?$)

I know there are some edge cases like 256 octets and 32 bit subnets but
that's my 'good enough' IP matching regex (written from a phone, in bed
because I'm too lazy to get up and masochistic enough to do it so I hope I
didn't error any).

I'll make this part better and get ip6 in here.


Re: Next subnet

2013-05-27 Thread Dr.Ruud

On 27/05/2013 23:55, shawn wilson wrote:

On May 27, 2013 1:02 PM, Dr.Ruud rvtol+use...@isolution.nl
mailto:rvtol%2buse...@isolution.nl wrote:
  On 26/05/2013 14:40, shawn wilson wrote:



  Thank y'all, I got to where I want to be:
  https://github.com/ag4ve/geocidr
 
   ...
   or grep { ! m%[0-9\.\/]+% } @{$opts-{ip}}
   or scalar(@{$opts-{ip}})  1
 
  The '+' in the regexp is superfluous as-is.
  (your regexp isn't anchored)
 
 
  You probably meant it more like:
 
  ...
  or !@{$opts-{ip}}
  or grep m{[^0-9./]}, @{$opts-{ip}}
 

You don't want to grep for anything that isn't a number there.


Who is this 'You'? You clearly misunderstand m{[^0-9./]}.



I like the !@arr vs my scalar though. And...
You're right on anchoring - I should. But as I'm lastly matching for an
IP (with a possible subnet, it should probably be more like:
m(^[0-9\./]+$)
Or better:
m(^[0-9\.]+(?:/(?:[0-3])?[0-9])?$)
Or more better:
m(^(?:(?:[0-2])?[0-9]{1,2}\.){3}(?:[0-2])?[0-9]{1,2}(?:/(?:[0-3])?[0-9])?$)


There are many much clearer ways to do that, And be aware of octalness.



I know there are some edge cases like 256 octets and 32 bit subnets but
that's my 'good enough' IP matching regex (written from a phone, in bed
because I'm too lazy to get up and masochistic enough to do it so I hope
I didn't error any).

I'll make this part better and get ip6 in here.


Just be properly lazy, and check Regexp::Common, Regexp::IPv6.

--
Ruud


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Next subnet

2013-05-26 Thread Michael Rasmussen
On Fri, May 24, 2013 at 03:18:35PM -0400, shawn wilson wrote:
 How do I find the next subnet? This should print 192.168.1.0 the
 second time - it errors:

[code deleted]
Why should it? The Net::IP documentation doesn't provide any information about 
actions that cross the subnet boundry.

Having said that, it seems it doesn't allow that operation.  
And in fact, many of the methods don't work after incrementing, which seems 
wrong to me:

#!/usr/bin/perl
use strict;
use warnings;
use Net::IP;

my @method_types = qw ( ip short binip intip mask last_ip prefixlen size iptype 
reverse_ip );

my $ip = Net::IP-new('192.168.0.0/24');# three lines from your code
print Start ip [ . $ip-ip . ]\n;
print start mask [ . $ip-prefixlen . ]\n;

$ip++;
print After incrementing by 1\n;
show_methods($ip);

$ip-set($ip-last_ip) ;
$ip++ ;
print \nAfter incrementing past last_ip\n;
show_methods($ip);


sub show_methods {
my ($ip) = @_;

print now at  . $ip-ip ,$/;

for my $type ( @method_types) {
if( $ip-$type ) {
print $type : , $ip-$type(), $/;
}
else {
print no more $type\n;
}
}
}


__END__

michael@bivy:~/rmme$ ./tpl
Start ip [192.168.0.0]
start mask [24]
After incrementing by 1
now at 192.168.0.1
ip : 192.168.0.1
short : 192
binip : 110010101001
intip : 3232235521
no more mask
last_ip : 192.168.0.255
no more prefixlen
size : 255
iptype : PRIVATE
no more reverse_ip

After incrementing past last_ip
Can't call method ip on an undefined value at ./tpl line 29.
michael@bivy:~/rmme$ 


 

-- 
Michael Rasmussen, Portland Oregon  
  Be Appropriate  Follow Your Curiosity
  Other Adventures: http://www.jamhome.us/ or http://gplus.to/MichaelRpdx
A special random fortune cookie fortune:
Only the mediocre are always at their best.
~ Jean Giraudoux, French Novelist
(rephrased as Only the mediorcre are at their best all the time. 
~ G.M. Ford in Who the hell is Wanda Fuca?)

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Next subnet

2013-05-26 Thread shawn wilson
Thank y'all, I got to where I want to be:
https://github.com/ag4ve/geocidr

On Sun, May 26, 2013 at 8:06 AM, Michael Rasmussen mich...@jamhome.us wrote:
 On Fri, May 24, 2013 at 03:18:35PM -0400, shawn wilson wrote:
 How do I find the next subnet? This should print 192.168.1.0 the
 second time - it errors:

 [code deleted]
 Why should it? The Net::IP documentation doesn't provide any information about
 actions that cross the subnet boundry.

 Having said that, it seems it doesn't allow that operation.
 And in fact, many of the methods don't work after incrementing, which seems 
 wrong to me:

 #!/usr/bin/perl
 use strict;
 use warnings;
 use Net::IP;

 my @method_types = qw ( ip short binip intip mask last_ip prefixlen size 
 iptype reverse_ip );

 my $ip = Net::IP-new('192.168.0.0/24');# three lines from your code
 print Start ip [ . $ip-ip . ]\n;
 print start mask [ . $ip-prefixlen . ]\n;

 $ip++;
 print After incrementing by 1\n;
 show_methods($ip);

 $ip-set($ip-last_ip) ;
 $ip++ ;
 print \nAfter incrementing past last_ip\n;
 show_methods($ip);


 sub show_methods {
 my ($ip) = @_;

 print now at  . $ip-ip ,$/;

 for my $type ( @method_types) {
 if( $ip-$type ) {
 print $type : , $ip-$type(), $/;
 }
 else {
 print no more $type\n;
 }
 }
 }


 __END__

 michael@bivy:~/rmme$ ./tpl
 Start ip [192.168.0.0]
 start mask [24]
 After incrementing by 1
 now at 192.168.0.1
 ip : 192.168.0.1
 short : 192
 binip : 110010101001
 intip : 3232235521
 no more mask
 last_ip : 192.168.0.255
 no more prefixlen
 size : 255
 iptype : PRIVATE
 no more reverse_ip

 After incrementing past last_ip
 Can't call method ip on an undefined value at ./tpl line 29.
 michael@bivy:~/rmme$




 --
 Michael Rasmussen, Portland Oregon
   Be Appropriate  Follow Your Curiosity
   Other Adventures: http://www.jamhome.us/ or http://gplus.to/MichaelRpdx
 A special random fortune cookie fortune:
 Only the mediocre are always at their best.
 ~ Jean Giraudoux, French Novelist
 (rephrased as Only the mediorcre are at their best all the time.
 ~ G.M. Ford in Who the hell is Wanda Fuca?)

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Next subnet

2013-05-25 Thread Dr.Ruud

On 24/05/2013 21:18, shawn wilson wrote:


How do I find the next subnet? This should print 192.168.1.0 the
second time - it errors:
#!/usr/bin/env perl

use strict;
use warnings;

use Net::IP;

my $ip = Net::IP-new('192.168.0.0/24');

print Start ip [ . $ip-ip . ]\n;
print start mask [ . $ip-prefixlen . ]\n;

$ip-set($ip-last_ip);
$ip++;
$ip-set($ip-ip . / . $ip-prefixlen);

print Start ip [ . $ip-ip . ]\n;
print start mask [ . $ip-prefixlen . ]\n;


Or without Net::IP:

perl -Mstrict -wle'
  my $subnet = $ARGV[0];

  my ($ip, $bits) = $subnet =~ m{(\S+)/(\S+)};  # parse

  my $ip_int = unpack N, pack , split /\./, $ip;
  my $step = 1  (32 - $bits);

  #$ip_int = (0x ^ ($step - 1));  # normalize
  $ip_int += $step;  # increment

  my $next_ip = sprintf %vd, pack N, $ip_int;
  print $next_ip/$bits;
' 192.168.0.42/24

192.168.1.42/24

(you can activate the normalization if wanted)

--
Ruud


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Next subnet

2013-05-24 Thread shawn wilson
How do I find the next subnet? This should print 192.168.1.0 the
second time - it errors:
#!/usr/bin/env perl

use strict;
use warnings;

use Net::IP;

my $ip = Net::IP-new('192.168.0.0/24');

print Start ip [ . $ip-ip . ]\n;
print start mask [ . $ip-prefixlen . ]\n;

$ip-set($ip-last_ip);
$ip++;
$ip-set($ip-ip . / . $ip-prefixlen);

print Start ip [ . $ip-ip . ]\n;
print start mask [ . $ip-prefixlen . ]\n;


## ERROR
 % ./t2.pl
Start ip [192.168.0.0]
start mask [24]
Can't call method ip on an undefined value at ./t2.pl line 15.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: foreach and next

2012-04-09 Thread Vyacheslav

My code

my %attr = (
PrintError = 0,
RaiseError = 0
);
my $dbh = DBI-connect($dsn, $user, $pass, \%attr);
unless ($dbh) {
next;
}
my $query = SHOW DATABASES;

I use
unless ($dbh) {
next;
} and this work fine.

Thanks


09.04.2012 01:22, Jim Gibson написал:

At 12:50 AM + 4/9/12, Vyacheslav wrote:

I enabled RaiserError, then script die.
...
my %attr = (
PrintError = 0,
RaiseError = 1
);


Use:

RaiseError = 0

instead so that your script will not raise an exception and die. Then 
check $dbh-err.





--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: foreach and next

2012-04-09 Thread Rob Dixon

On 09/04/2012 14:24, Vyacheslav wrote:

My code

my %attr = (
PrintError = 0,
RaiseError = 0
);
my $dbh = DBI-connect($dsn, $user, $pass, \%attr);
unless ($dbh) {
next;
}
my $query = SHOW DATABASES;

I use
unless ($dbh) {
next;
} and this work fine.

Thanks


09.04.2012 01:22, Jim Gibson написал:

At 12:50 AM + 4/9/12, Vyacheslav wrote:

I enabled RaiserError, then script die.
...
my %attr = (
PrintError = 0,
RaiseError = 1
);


Use:

RaiseError = 0

instead so that your script will not raise an exception and die. Then
check $dbh-err.






Hi Vyacheslav

I suggest you use this instead

my $dbh = DBI-connect($dsn, $user, $pass) or do {
  warn Can't connect to database $db: $DBI::errstr\n;
  next;
};

so that your program shows which database connections have failed and
been skipped.

This is pretty much the same as your original code except that a failure
to connect causes a warning instead of a fatal 'die', and execution
skips to the next database source. Also the warning string shows the
name of the database instead of saying just Can't connect to the DB.

HTH,

Rob

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: foreach and next

2012-04-08 Thread Binish A.R
Enclose DBI operation inside eval
-

#!/usr/bin/perl

use strict;
use warnings;
use DBI;
use DBD::mysql;

foreach $db (hostdb($project)) {

eval {
        my $server = $db;
        my $dbname = information_schema;
        my $port = 3306;
        my $dsn = dbi:mysql:$dbname:$server:$port;
        my $user = user;
        my $pass = pass;

        my $dbh = DBI-connect($dsn, $user, $pass) or die Can't connect to the 
DB: $DBI::errstr\n;
        my $query = SHOW DATABASES;
        my $sth = $dbh-prepare($query);
        $sth-execute;
        my @dbs;
        while ( my $data = $sth-fetchrow_arrayref ) {
                push @dbs, $data-[0];
        }

        $dbh-disconnect;
}; 

if ($@) {
        print host $db - not ok;
} else {
        print host $db - ok;
}

 
img src=http://www.gnome.org/friends/banners/associate.png; alt=Become a 
Friend of GNOME border=0 /



 From: Vyacheslav agapov.sl...@gmail.com
To: beginners@perl.org 
Sent: Sunday, April 8, 2012 11:42 AM
Subject: foreach and next
 
Hello all.
My english bad and i have a problem.

I am connected to databases in a cycle foreach and the script die, if one of 
database is not available.

#!/usr/bin/perl

use strict;
use warnings;
use DBI;
use DBD::mysql;

foreach $db (hostdb($project)) {

my $server = $db;
my $dbname = information_schema;
my $port = 3306;
my $dsn = dbi:mysql:$dbname:$server:$port;
my $user = user;
my $pass = pass;

my $dbh = DBI-connect($dsn, $user, $pass) or die Can't connect to the DB: 
$DBI::errstr\n;
my $query = SHOW DATABASES;
my $sth = $dbh-prepare($query);
$sth-execute;
my @dbs;
while ( my $data = $sth-fetchrow_arrayref ) {
        push @dbs, $data-[0];
}

print host $db - ok;
$dbh-disconnect;

My result

host db1 - ok
host db2 - ok
host db3 - ok
DBI connect('information_schema:db4:3306','user',...) failed: Can't connect to 
MySQL server on 'db4' (111) at ./dbcheck.pl line 53
and script die

How I can pass an error that the cycle has continued execute? I need
host db1 - ok
host db2 - ok
host db3 - ok
...
host db10 - ok

Can I use next operator in this situation?



-- To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/

Re: foreach and next

2012-04-08 Thread Shlomi Fish
Hi Vyacheslav,

On Sun, 08 Apr 2012 06:12:53 +
Vyacheslav agapov.sl...@gmail.com wrote:

 Hello all.
 My english bad and i have a problem.
 
 I am connected to databases in a cycle foreach and the script die, if 
 one of database is not available.
 
 #!/usr/bin/perl
 
 use strict;
 use warnings;
 use DBI;
 use DBD::mysql;
 
 foreach $db (hostdb($project)) {
 
 my $server = $db;
 my $dbname = information_schema;
 my $port = 3306;
 my $dsn = dbi:mysql:$dbname:$server:$port;
 my $user = user;
 my $pass = pass;
 
 my $dbh = DBI-connect($dsn, $user, $pass) or die Can't connect to the 
 DB: $DBI::errstr\n;

Your problem is that you are invoking die ... which throws an exception. See:

https://www.socialtext.net/perl5/exception_handling

Since this exception is not caught (using eval { ... }) it terminates the
entire program. So what you should do instead is handle it gracefully (say
using next):

my $dbh = DBI-connect($dsn, $user, $pass);

if (!$dbh)
{
next DB_HOSTS_LOOP; # And label the loop appropriately.
}

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
Humanity - Parody of Modern Life - http://shlom.in/humanity

Chuck Norris refactors 10 million lines of Perl code before lunch.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: foreach and next

2012-04-08 Thread Vyacheslav

Thanks all.

using eval helped me.

08.04.2012 09:43, Shlomi Fish пишет:

Hi Vyacheslav,

On Sun, 08 Apr 2012 06:12:53 +
Vyacheslavagapov.sl...@gmail.com  wrote:


Hello all.
My english bad and i have a problem.

I am connected to databases in a cycle foreach and the script die, if
one of database is not available.

#!/usr/bin/perl

use strict;
use warnings;
use DBI;
use DBD::mysql;

foreach $db (hostdb($project)) {

my $server = $db;
my $dbname = information_schema;
my $port = 3306;
my $dsn = dbi:mysql:$dbname:$server:$port;
my $user = user;
my $pass = pass;

my $dbh = DBI-connect($dsn, $user, $pass) or die Can't connect to the
DB: $DBI::errstr\n;

Your problem is that you are invoking die ... which throws an exception. See:

https://www.socialtext.net/perl5/exception_handling

Since this exception is not caught (using eval { ... }) it terminates the
entire program. So what you should do instead is handle it gracefully (say
using next):

my $dbh = DBI-connect($dsn, $user, $pass);

if (!$dbh)
{
next DB_HOSTS_LOOP; # And label the loop appropriately.
}

Regards,

Shlomi Fish



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: foreach and next

2012-04-08 Thread Shlomi Fish
Hi Vyacheslav,

On Sun, 08 Apr 2012 15:10:06 +
Vyacheslav agapov.sl...@gmail.com wrote:

 Thanks all.
 
 using eval helped me.
 

The problem with eval in Perl 5 is that it catches any and all thrown exceptions
. I.e: by default, it doesn't do Object-Oriented exceptions like Java, Ruby,
Python and other languages do, though this can be done to a large extent
using some CPAN modules. As a result, by using eval, you risk easily trapping
other more meaningful exceptions, which *should* cause your program to
terminate.

In your case, you're throwing an exception explicitly upon failure, so if you're
not interested in an exception getting thrown, you should just handle the
failure differently.

( I also recall seeing something about excessive exceptions throwing and
trapping being costly in CPU time, but this shouldn't make a difference in this
case. )   

Regards,

Shlomi Fish 

-- 
-
Shlomi Fish   http://www.shlomifish.org/
Why I Love Perl - http://shlom.in/joy-of-perl

“Interesting” has a negative correlation with “successful”.
— Anno on Freenode's #perl

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: foreach and next

2012-04-08 Thread Dr.Ruud

On 2012-04-08 17:10, Vyacheslav wrote:


using eval helped me.


You should not use exceptions for normal code flow.

Read the DBI docs (perldoc DBI).
If a failed connection must be an exception, set RaiseError to true.
But if it isn't an exception, leave it false, and test $dbh-err (or the 
global $DBI::err).


--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: foreach and next

2012-04-08 Thread Vyacheslav

I enabled RaiserError, then script die.
...
my %attr = (
PrintError = 0,
RaiseError = 1
);

my $dbh = DBI-connect($dsn, $user, $pass, \%attr); # or die Can't 
connect to the DB: $DBI::errstr\n;

my $query = SHOW DATABASES;
my $sth = $dbh-prepare($query) or die Can't prepare SQL statement: 
$DBI::errstr\n;;

$sth-execute;
my @dbs;
while ( my $data = $sth-fetchrow_arrayref ) {
push @dbs, $data-[0];
}
...
DBI connect('information_schema:db1:3306','test',...) failed: Can't 
connect to MySQL server on 'db1' (111) at ./dbcheck.pl line 72


I test with $dbh-err, but the script all the same die.

08.04.2012 18:40, Dr.Ruud пишет:

On 2012-04-08 17:10, Vyacheslav wrote:


using eval helped me.


You should not use exceptions for normal code flow.

Read the DBI docs (perldoc DBI).
If a failed connection must be an exception, set RaiseError to true.
But if it isn't an exception, leave it false, and test $dbh-err (or 
the global $DBI::err).




--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: foreach and next

2012-04-08 Thread Jim Gibson

At 12:50 AM + 4/9/12, Vyacheslav wrote:

I enabled RaiserError, then script die.
...
my %attr = (
PrintError = 0,
RaiseError = 1
);


Use:

  RaiseError = 0

instead so that your script will not raise an exception and die. Then 
check $dbh-err.



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




foreach and next

2012-04-07 Thread Vyacheslav

Hello all.
My english bad and i have a problem.

I am connected to databases in a cycle foreach and the script die, if 
one of database is not available.


#!/usr/bin/perl

use strict;
use warnings;
use DBI;
use DBD::mysql;

foreach $db (hostdb($project)) {

my $server = $db;
my $dbname = information_schema;
my $port = 3306;
my $dsn = dbi:mysql:$dbname:$server:$port;
my $user = user;
my $pass = pass;

my $dbh = DBI-connect($dsn, $user, $pass) or die Can't connect to the 
DB: $DBI::errstr\n;

my $query = SHOW DATABASES;
my $sth = $dbh-prepare($query);
$sth-execute;
my @dbs;
while ( my $data = $sth-fetchrow_arrayref ) {
push @dbs, $data-[0];
}

print host $db - ok;
$dbh-disconnect;

My result

host db1 - ok
host db2 - ok
host db3 - ok
DBI connect('information_schema:db4:3306','user',...) failed: Can't 
connect to MySQL server on 'db4' (111) at ./dbcheck.pl line 53

and script die

How I can pass an error that the cycle has continued execute? I need
host db1 - ok
host db2 - ok
host db3 - ok
...
host db10 - ok

Can I use next operator in this situation?



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




How Could I print the next line after a REGEX match?

2011-11-26 Thread Raito Garcia
hi

Well today i have another dude, I have a HTML file like this content:


/td/tr
trtd colspan=2hr/td/tr
trtd
span class=host_infoRemote host information/spanbrtable
align=center border=0 width=60%
tbodytr
td align=leftOperating System : /td
td align=rightWindows 7 Enterprise/td
/tr
tr
td align=leftNetBIOS name : /td
td align=rightGUSR712DPO16125/td
/tr
trtd align=leftDNS name : /td/tr
/tbody/table


My code is this:

#!/usr/bin/perl

#Variables
my $line=;
my $aux=0;

#REGEX

open(H,Report.html) || die No se puede abrir el archivo:$!;
 while($line=H){
chomp($line);
if ($line =~ /td align\=\left\NetBIOS\ name\ :\ \/td/){
print Name:\t;
print $',\n;

}
 }
close(H)

How do you see I try to print the next line after the REGEX match, but I
obtain the next result:

Name:
Name:
Name:
Name:
Name:

I mean, I print nothing :S

does anybody knows how could I print the next line after the regex match? I
want this output:

Name: GUSR712DPO16125

I hope some of you tell me my errors,

thanks so much


Re: How Could I print the next line after a REGEX match?

2011-11-26 Thread Shlomi Fish
Hi Raito,

On Sat, 26 Nov 2011 23:31:06 -0600
Raito Garcia saintar...@gmail.com wrote:

 hi
 
 Well today i have another dude, I have a HTML file like this content:
 
 
 /td/tr
 trtd colspan=2hr/td/tr
 trtd
 span class=host_infoRemote host information/spanbrtable
 align=center border=0 width=60%
 tbodytr
 td align=leftOperating System : /td
 td align=rightWindows 7 Enterprise/td
 /tr
 tr
 td align=leftNetBIOS name : /td
 td align=rightGUSR712DPO16125/td
 /tr
 trtd align=leftDNS name : /td/tr
 /tbody/table
 
 

You shouldn't parse HTML with regexes. See:

* perlbot rindolf: Don't parse or modify html with regular
  expressions! See one of HTML::Parser's subclasses: HTML::TokeParser, 
HTML::TokeParser::Simple, HTML::TreeBuilder(::Xpath)?, HTML::TableExtract etc. 
If your response begins that's overkill. i only want to... you are wrong. 
http://en.wikipedia.org/wiki/Chomsky_hierarchy and http://xrl.us/bf4jh6 for why 
not to use regex on HTML

*
  
http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454

* http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html

* http://perl-begin.org/uses/text-parsing/

Use a parser. Now to answer your question in its context (and to comment on
your code).

 My code is this:
 
 #!/usr/bin/perl
 

Always add use strict; and use warnings; at the top of the code.

 #Variables
 my $line=;
 my $aux=0;

Don't predeclare your variables at the top - declare them when you are using
them. 

 
 #REGEX
 
 open(H,Report.html) || die No se puede abrir el archivo:$!;

1. Use three-args open.

2. Use lexical file handles.

See:

http://perl-begin.org/tutorials/bad-elements/

  while($line=H){
 chomp($line);
 if ($line =~ /td align\=\left\NetBIOS\ name\ :\ \/td/){

You don't have to escape =,  and whitespace. If you have /s in the string,
you can use a different delimiter:

if ($line =~ m{...})

Also it seems you're looking for a substring. For that you can use \Q and \E or
http://perldoc.perl.org/functions/index.html

 print Name:\t;
 print $',\n;

This won't work because $' does not contain the next line - it only contains
the rest of the line after the match. If you want to print the next line, you
can either do:

my $next_line = $html_fh;
chomp($next_line);
print $next_line;

Or alternatively set a global-to-the-loop boolean flag that will be checked and
reset.

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
Star Trek: We, the Living Dead - http://shlom.in/st-wtld

Chuck Norris is his own boss. If you hire him, he’ll tell your boss what to
do.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




next in foreach loop

2010-08-13 Thread Kryten
Hi,

Complete newbie.

Is there any way to use next from within a foreach loop?

All the examples I have seen/read use a while loop to demo.

Thanks,
Stuart


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: next in foreach loop

2010-08-13 Thread Jeff Pang
2010/8/13 Kryten kryte...@googlemail.com:
 Hi,

 Complete newbie.

 Is there any way to use next from within a foreach loop?


Sure.

$ perl -le '
 for (1..10) {
 next if $_ == 5;
 print;
 } '
1
2
3
4
6
7
8
9
10


-- 
Jeff Pang
http://home.arcor.de/pangj/

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: next in foreach loop

2010-08-13 Thread Chas. Owens
On Aug 12, 2010, at 19:08, Kryten kryte...@googlemail.com wrote:

 Hi,
 
 Complete newbie.
 
 Is there any way to use next from within a foreach loop?
 
 All the examples I have seen/read use a while loop to demo.

Yes, next will work on for/foreach, while, and until loops.  So you can say

for my $num (1 .. 10) {
next if $num % 2;
print $num\n;
}

To print the even numbers between 1 and 10 (inclusive).

The next, last, and redo loop control statements will even work on bare blocks:

my $i = 0;
{
print infinite loop: , $i++;
sleep 1;
redo;
}
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: How to find the status, i.e. next run time and last run time, of a task which is run through windows task scheduler !

2010-06-09 Thread C.DeRykus
On Jun 8, 2:18 am, learn.tech...@gmail.com (Amit Saxena) wrote:
 Hi all,

 I want to know how to find the status, i.e. next run time and last run
 time, of a task which is run through windows task scheduler.

 This is required so as to find out instances where a task gets hanged
 after run through windows task scheduler.


Another approach would be separate daemon program
that'd read a config file of tasks to watch and monitor
the process table for them.

Win32::Process::Info  might be useful.

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




How to find the status, i.e. next run time and last run time, of a task which is run through windows task scheduler !

2010-06-08 Thread Amit Saxena
Hi all,

I want to know how to find the status, i.e. next run time and last run
time, of a task which is run through windows task scheduler.

This is required so as to find out instances where a task gets hanged
after run through windows task scheduler.

Thanks  Regards,
Amit Saxena


Re: How to find the status, i.e. next run time and last run time, of a task which is run through windows task scheduler !

2010-06-08 Thread Chas. Owens
On Tue, Jun 8, 2010 at 05:18, Amit Saxena learn.tech...@gmail.com wrote:
 Hi all,

 I want to know how to find the status, i.e. next run time and last run
 time, of a task which is run through windows task scheduler.

 This is required so as to find out instances where a task gets hanged
 after run through windows task scheduler.

 Thanks  Regards,
 Amit Saxena


Take a look at [Win32::TaskScheduler's] [GetNextRunTime][2] and
[GetMostRecentRunTime][3].

If I understand your problem, the UNIXy solution is to create a pid
file (a file that contains the process id of the program) when you
start and remove it when you end.  If a pid file exists when you
start, that means that the lsat process either died prematurely and
needs to be cleaned up after or it is still running.

[1] : http://search.cpan.org/dist/Win32-TaskScheduler/TaskScheduler.pm
[2] : http://tinyurl.com/25rrwcr
[3] : http://tinyurl.com/2b5vtwm


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




eval and next

2009-10-06 Thread Alexander Koenig
Hi all,

I have a Perl program where I use eval to catch errors. As they are Java
errors (via Inline::Java) I want my program to continue and just log the
errors somewhere.

My problem with this is, that I use the eval within a loop and I also
use next in this loop to ignore some special cases. But now, whenever
the next is triggered I get a warning like this:

Exiting eval via next at ad_hoc_stats.pl line 204, GEN6 line 5421.

Googling the message I found this thread on Perlmonks
http://www.perlmonks.org/?node_id=104789

But that does not seem to be about the same problem. I know that next is
executed for the enclosing for loop and not for the eval block. That is
how I want it. I tried naming the for loop to show perl that I know what
I'm doing but the warning persists.

Is there some recommended way of doing this, so that I won't get a
warning every time the next is triggered?

I hope my problem is clear. If not, I could provide some sample code as
well.

bye
Alex

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: how to copy elements into the next array

2009-01-29 Thread Mr. Shawn H. Corey
On Thu, 2009-01-29 at 14:39 +0800, itshardtogetone wrote:
 Hi,
 How do I copy the first 10 elements of @a into @b?
 
 The method that I use is long :-
 my @a = 1..20;
 my @b = ();
 
 my $ctr = 0;
 foreach (@a){
  if ($ctr  10){
  push @b,$_;
  }
  $ctr ++;
 }

See `perldoc perldata` and search for /Slices/


-- 
Just my 0.0002 million dollars worth,
  Shawn

It would appear that we have reached the limits of what it is
 possible to achieve with computer technology, although one should
 be careful with such statements, as they tend to sound pretty silly
 in 5 years.
   --John von Neumann, circa 1960


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




how to copy elements into the next array

2009-01-28 Thread itshardtogetone
Hi,
How do I copy the first 10 elements of @a into @b?

The method that I use is long :-
my @a = 1..20;
my @b = ();

my $ctr = 0;
foreach (@a){
 if ($ctr  10){
 push @b,$_;
 }
 $ctr ++;
}






Thanks.



回复:how to copy elements into the next array

2009-01-28 Thread yantao
try this,

@b[0..9] = @a[0..9];


- 原邮件 -
从: itshardtogetone itshardtoget...@hotmail.com
日期: 星期四, 一月 29日, 2009 下午2:39
主题: how to copy elements into the next array

 Hi,
 How do I copy the first 10 elements of @a into @b?
 
 The method that I use is long :-
 my @a = 1..20;
 my @b = ();
 
 my $ctr = 0;
 foreach (@a){
 if ($ctr  10){
 push @b,$_;
 }
 $ctr ++;
 }
 
 
 
 
 
 
 Thanks.
 


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: how to copy elements into the next array

2009-01-28 Thread John W. Krahn

itshardtogetone wrote:

Hi,


Hello,


How do I copy the first 10 elements of @a into @b?


my @b = @a[ 0 .. 9 ];


John
--
Those people who think they know everything are a great
annoyance to those of us who do.-- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: how to round off a decimal to the next whole number

2008-08-08 Thread Dr.Ruud
[EMAIL PROTECTED] schreef:

 How do I round off a decimal to the next nearest whole digit ,
 example
 0.123 = 1,
 1.23 = 2,
 4.7312 = 5, etc etc.

Define next.

You can use POSIX::ceil(),
but then -1.23 becomes -1 and you might want -2 there?

$ echo -1.23 4.1 5 |perl -MPOSIX -nwle'print ceil($_) for split'
-1
5
5

$ echo -1.23 4.1 5 |perl -MPOSIX -anwle'print $_0 ? floor($_) :
ceil($_) for @F'
-2
5
5

-- 
Affijn, Ruud

Gewoon is een tijger.


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




RE: how to round off a decimal to the next whole number

2008-08-06 Thread Stewart Anderson

 -Original Message-
 From: Anirban Adhikary [mailto:[EMAIL PROTECTED]
 Sent: 06 August 2008 06:51
 To: beginners@perl.org
 Subject: Re: how to round off a decimal to the next whole number
 
 On Wed, Aug 6, 2008 at 11:01 AM, [EMAIL PROTECTED] wrote:
 
  Hi,
  How do I round off a decimal to the next nearest whole digit ,
  example
  0.123 = 1,
  1.23 = 2,
  4.7312 = 5, etc etc.
 
  Right now I can only do the above by extracting the first digit
using
  splice , then add one.
 
  Thanks
 

This is straight from the Perl FAQ.

sub round {
my($number) = shift;
return int($number + .5);
}

Stu



Information in this email including any attachments may be privileged, 
confidential and is intended exclusively for the addressee. The views expressed 
may not be official policy, but the personal views of the originator. If you 
have received it in error, please notify the sender by return e-mail and delete 
it from your system. You should not reproduce, distribute, store, retransmit, 
use or disclose its contents to anyone. Please note we reserve the right to 
monitor all e-mail communication through our internal and external networks. 
SKY and the SKY marks are trade marks of British Sky Broadcasting Group plc and 
are used under licence. British Sky Broadcasting Limited (Registration No. 
2906991), Sky Interactive Limited (Registration No. 3554332), Sky-In-Home 
Service Limited (Registration No. 2067075) and Sky Subscribers Services Limited 
(Registration No. 2340150) are direct or indirect subsidiaries of British Sky 
Broadcasting Group plc (Registration No. 2247735). All of the companies 
mentioned in this paragraph are incorporated in England and Wales and share the 
same registered office at Grant Way, Isleworth, Middlesex TW7 5QD.

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




Re: how to round off a decimal to the next whole number

2008-08-06 Thread Mr. Shawn H. Corey
On Wed, 2008-08-06 at 13:31 +0800, [EMAIL PROTECTED] wrote:
 Hi,
 How do I round off a decimal to the next nearest whole digit ,
 example
 0.123 = 1,
 1.23 = 2,
 4.7312 = 5, etc etc.
 
 Right now I can only do the above by extracting the first digit using splice 
 , then add one.
 
 Thanks
 
 

#!/usr/bin/perl

use POSIX;

my @nbrs = (
  0.123,
  1.23,
  4.7312,
);

for my $n ( @nbrs ){
  print number: $n\n;
  printf \tceil: %d\n, ceil( $n );
  printf \tfloor: %d\n, floor( $n );
  printf \tround: %.0f\n, $n;
}

__END__



-- 
Just my 0.0002 million dollars worth,
  Shawn

Where there's duct tape, there's hope.

Perl is the duct tape of the Internet.
Hassan Schroeder, Sun's first webmaster


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




Re: how to round off a decimal to the next whole number

2008-08-06 Thread Jeff Pang
[EMAIL PROTECTED] 写道:
 Hi,
 How do I round off a decimal to the next nearest whole digit ,
 example
 0.123 = 1,
 1.23 = 2,
 4.7312 = 5, etc etc.

$number = int($number) + 1;
also does the same thing.

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




Re: how to round off a decimal to the next whole number

2008-08-06 Thread Rob Dixon
[EMAIL PROTECTED] wrote:

 How do I round off a decimal to the next nearest whole digit ,
 example
 0.123 = 1,
 1.23 = 2,
 4.7312 = 5, etc etc.
 
 Right now I can only do the above by extracting the first digit using splice 
 , then add one.

You need the ceil() function from the POSIX module. All other solutions here
will fail if the fractional part of the number is zero (for instance 5.000 will
be upgraded to 6, which I presume isn't wanted).

use strict;
use warnings;

use POSIX;

foreach (qw(0.123 1.23 4.7312 5.000)) {
  print ceil($_),   ;
}

**OUTPUT**

1  2  5  5


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




how to round off a decimal to the next whole number

2008-08-05 Thread itshardtogetone

Hi,
How do I round off a decimal to the next nearest whole digit ,
example
0.123 = 1,
1.23 = 2,
4.7312 = 5, etc etc.

Right now I can only do the above by extracting the first digit using splice 
, then add one.


Thanks


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




Re: how to round off a decimal to the next whole number

2008-08-05 Thread Anirban Adhikary
On Wed, Aug 6, 2008 at 11:01 AM, [EMAIL PROTECTED] wrote:

 Hi,
 How do I round off a decimal to the next nearest whole digit ,
 example
 0.123 = 1,
 1.23 = 2,
 4.7312 = 5, etc etc.

 Right now I can only do the above by extracting the first digit using
 splice , then add one.

 Thanks


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


 Just use split function like this

my $val= 0.123;
my($a,$b)=split(/\./,$val);
my $final=($a+1);
print $val=$final.\n;

Thanks  Regards
Anirban Adhikary


$topIter-next()

2008-03-27 Thread Subra
Can some one pls tell me wts the meaning of $topIter-next() ?
I know - is used for hash refs, but dont know when to use -( ) !!!


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




Re: $topIter-next()

2008-03-27 Thread Jenda Krynicky
From: Subra [EMAIL PROTECTED]

 Can some one pls tell me wts the meaning of $topIter-next() ?
 I know - is used for hash refs, but dont know when to use -( ) !!!

- is used for any references. And for method calls. In this case you 
are calling the next() method of the $topIter object.

Jenda
= [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


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




Re: $topIter-next()

2008-03-27 Thread Gunnar Hjalmarsson

Just posted to clpmisc:

 Original Message 
Subject: Re: Operator -()
Date: Thu, 27 Mar 2008 20:35:27 +0100
From: Gunnar Hjalmarsson [EMAIL PROTECTED]
Newsgroups: comp.lang.perl.misc

Subra wrote:

[ exactly the same question as was posted to the beginners list a few
minutes earlier ]

DO NOT DO THAT !!!

--
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/




Next Page gets downloaded in Perl

2007-12-13 Thread Praki
Greetings All

I have a Perl file in which i m doing all the operaions in one file
based on the command line arguments.
login.cgi
.
.
.
$query = new CGI;
$sid = $query-cookie('CGISESSID') || $query-param('CGISESSID') ||
undef;
$submit_value=$query-param(submit);

if ($sid ne ){
print $query-header( -cookie=$cookie );}
else{
print Content-type: text/html;}

if ($sid eq   $submit_value eq ) {
   auth_page( Login Authentication, Login Authentication);
   print Session id: ;
   print $sid;
   print \n Submit value:;
   print $query-param(submit);

   print $FORM{'uid'};
   print $query-header( -cookie=$cookie );
   print_trailer();
 exit(0);
}
elsif ($sid eq   $submit_valu ne ) {
print_header( Login Authentication, Login Authentication);
print_trailer();
exit(0);
}
.
.
.
when i open the for the first time it should go to

if ($sid eq   $submit_value eq )  this condition as both will be
empty. and here i m getting the login information and again i m
calling the same file(login.cgi). now as the sumbit value will not be
empty it has to go the next condtion

elsif ($sid eq   $submit_valu ne ) .i m validating the
credentials here. but the problem here i face is the page info which
is to be displayed in the browser is automaticaly asks for File
Download. when downloaded that file and check by open in the new
window it displays the ouput. i could not understand the problem so
plz can u help me in clearing this problem...

where i m going wrong..
thanks,
Prakash


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




Re: Reading the next line in a file from the current position

2007-11-12 Thread Gunnar Hjalmarsson

Praveena Vittal wrote:

I like to know what does the below represents..

$'


Then why don't you look it up in the docs instead of asking hundreds of 
people to read the docs for you?


--
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/




Re: Reading the next line in a file from the current position

2007-11-12 Thread Praveena Vittal

Thanks for all your comments.



Jeff Pang wrote, On 11/12/2007 12:26 PM:


On Nov 12, 2007 2:48 PM, Praveena Vittal [EMAIL PROTECTED] wrote:
 


Hi,
Thanks for your comments..

I like to know what does the below represents..

$'

   



from `perldoc perlvar':

  $'  The string following whatever was matched by the last success-
  ful pattern match (not counting any matches hidden within a
  BLOCK or eval() enclosed by the current BLOCK).  (Mnemonic: '
  often follows a quoted string.)  Example:

  local $_ = 'abcdefghi';
  /def/;
  print $`:$:$'\n; # prints abc:def:ghi

  This variable is read-only and dynamically scoped to the cur-
  rent BLOCK.

  The use of this variable anywhere in a program imposes a con-
  siderable performance penalty on all regular expression
  matches.  See BUGS.

 




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




Re: Reading the next line in a file from the current position

2007-11-11 Thread Praveena Vittal

Hi,
Thanks for your comments..

I like to know what does the below represents..

$'

Regards,
Praveena
Jeff Pang wrote, On 11/07/2007 06:48 PM:


--- Praveena Vittal [EMAIL PROTECTED] wrote:

 


Hi all,

I like to know how can we read  a line next to the current position in 
a file .


   



Hi,

FD in scalar context will read next line.ie,

open FD,file or die $!;
FD for (1..3);

# now you're in No.3 line,again we say:

scalar FD; # no you're in the No.4 line





 
National Bingo Night. Play along for the chance to win $10,000 every week. Download your gamecard now at Yahoo!7 TV. http://au.blogs.yahoo.com/national-bingo-night/




 




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




Re: Reading the next line in a file from the current position

2007-11-11 Thread Jeff Pang
On Nov 12, 2007 2:48 PM, Praveena Vittal [EMAIL PROTECTED] wrote:
 Hi,
  Thanks for your comments..

  I like to know what does the below represents..

  $'


from `perldoc perlvar':

   $'  The string following whatever was matched by the last success-
   ful pattern match (not counting any matches hidden within a
   BLOCK or eval() enclosed by the current BLOCK).  (Mnemonic: '
   often follows a quoted string.)  Example:

   local $_ = 'abcdefghi';
   /def/;
   print $`:$:$'\n; # prints abc:def:ghi

   This variable is read-only and dynamically scoped to the cur-
   rent BLOCK.

   The use of this variable anywhere in a program imposes a con-
   siderable performance penalty on all regular expression
   matches.  See BUGS.

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




Re: Reading the next line in a file from the current position

2007-11-07 Thread Jeff Pang

--- Praveena Vittal [EMAIL PROTECTED] wrote:

 Hi all,
 
  I like to know how can we read  a line next to the current position in 
 a file .
 

Hi,

FD in scalar context will read next line.ie,

open FD,file or die $!;
FD for (1..3);

# now you're in No.3 line,again we say:

scalar FD; # no you're in the No.4 line





  
National Bingo Night. Play along for the chance to win $10,000 every week. 
Download your gamecard now at Yahoo!7 TV. 
http://au.blogs.yahoo.com/national-bingo-night/



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




Reading the next line in a file from the current position

2007-11-07 Thread Praveena Vittal

Hi all,

I like to know how can we read  a line next to the current position in 
a file .


Regards,
Praveena

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




peek next line in file

2007-09-27 Thread Mahurshi Akilla
Is there a way in perl to peek the next line in the file while keeping
the line pointer the same?

I want to do something like this:

while (INFILE)
{

//do some stuff

//?? peek next line ?? and enter conditional block//

//do some more stuff
}

Of course, there are workarounds, but it would be nice to know if this
can be done.


Mahurshi Akilla


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




Re: peek next line in file

2007-09-27 Thread Xavier Noria

On Sep 27, 2007, at 1:29 AM, Mahurshi Akilla wrote:


Is there a way in perl to peek the next line in the file while keeping
the line pointer the same?

I want to do something like this:

while (INFILE)
{

//do some stuff

//?? peek next line ?? and enter conditional block//

//do some more stuff
}


You could do this:

my $pos = tell $fh;
my $next_line = $fh;
seek $fh, $pos, 0;

-- fxn


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




Re: Help with WWW::Mechanize - Next Question

2006-12-05 Thread Mathew Snyder
Rob Dixon wrote:
 Mathew Snyder wrote:
 With all the help I've received I've been able to get this working. 
 This is my
 text:
 #!/usr/bin/perl

 use warnings;
 use strict;
 use WWW::Mechanize;
 use HTML::TokeParser;

 my $username = 'msnyder';
 my $password = 'xxx';
 my $status   = 'open';

 my $agent = WWW::Mechanize-new();
 $agent-get('https://rt.ops.xxx.com/');

 $agent-submit_form(
 form_name = 'login',
 fields= {
 'user' = $username,
 'pass' = $password,
 }
 );

 $agent-follow_link(text = Tickets);

 $agent-submit_form(
 form_name = 'BuildQuery',
 fields= {
 'ValueOfStatus' = $status,
 'ValueOfActor'  = $username,
 },
 button= 'DoSearch'
 );

 my $data = $agent-content();
 print $data;


 What this will do is return to me HTML source with a list of work
 tickets and
 all pertinent, associated data.  The purpose of setting this up is to
 allow me
 to pull out email addresses of any work ticket created as a result of
 spam.

 For anyone not familiar with Request Tracker from Best Practical
 Solutions, the
 'from' email address on any incoming email received by Request Tracker is
 automatically turned into a user account.  With the amount of spam
 flying around
 the the Net these days those user accounts add up.

 All those spam tickets are assigned to me so I can eliminate them and
 the users
 created as a result of them from our database.  My goal is to parse
 $data to
 pull out all the email addresses which I will then sift through to
 remove any
 legitimate addresses.

 You'll notice I declare the use of HTML::TokeParser.  This leads to my
 next
 question.  Do I need to use that?  Would it be simpler to just parse
 the data
 matching against a regex and put any matches into a file?  I imagine I
 don't
 need to sift through all the HTML tags just to get to the email
 addresses since
 they are fairly easy to spot.
 
 Hi Mathew
 
 Ordinarily I would insist that you use a proper HTML parser, but I see
 no harm in
 searching for email addresses as their format is well defined. Use the
 Email::Address module, like this:
 
 use Email::Address;
 
 my @email = Email::Address-parse($agent-content);
 print $_-address, \n foreach @email;
 
 HTH,
 
 Rob
 
 

I don't know if maybe there is a bug in the Email::Address module or not.  I've
changed nothing other than what you've suggested.  Now I'm gettting a
Segmentation Fault.

Here's my code as it stands now:
#!/usr/bin/perl

use warnings;
use strict;
use WWW::Mechanize;
use Email::Address;

my $user = 'msnyder';
my $pass = 'xxx';
my $status   = 'open';
my $queue= 'Security';

my $agent = WWW::Mechanize-new();
$agent-get('https://rt.ops.xxx.com/');

$agent-submit_form(
form_name = 'login',
fields= {
'user' = $user,
'pass' = $pass,
}
);

$agent-follow_link(text = Tickets);

$agent-submit_form(
form_name = 'BuildQuery',
fields= {
'ValueOfStatus' = $status,
'ValueOfActor'  = $user,
'ValueOfQueue'  = $queue,
},
button= 'DoSearch'
);

my $data = $agent-content();
my @emails = Email::Address-parse($data);

foreach my $email (@emails){
print $email;
};

-- 
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 with WWW::Mechanize - Next Question

2006-12-03 Thread Rob Dixon

Mathew Snyder wrote:

With all the help I've received I've been able to get this working.  This is my
text:
#!/usr/bin/perl

use warnings;
use strict;
use WWW::Mechanize;
use HTML::TokeParser;

my $username = 'msnyder';
my $password = 'xxx';
my $status   = 'open';

my $agent = WWW::Mechanize-new();
$agent-get('https://rt.ops.xxx.com/');

$agent-submit_form(
form_name = 'login',
fields= {
'user' = $username,
'pass' = $password,
}
);

$agent-follow_link(text = Tickets);

$agent-submit_form(
form_name = 'BuildQuery',
fields= {
'ValueOfStatus' = $status,
'ValueOfActor'  = $username,
},
button= 'DoSearch'
);

my $data = $agent-content();
print $data;


What this will do is return to me HTML source with a list of work tickets and
all pertinent, associated data.  The purpose of setting this up is to allow me
to pull out email addresses of any work ticket created as a result of spam.

For anyone not familiar with Request Tracker from Best Practical Solutions, the
'from' email address on any incoming email received by Request Tracker is
automatically turned into a user account.  With the amount of spam flying around
the the Net these days those user accounts add up.

All those spam tickets are assigned to me so I can eliminate them and the users
created as a result of them from our database.  My goal is to parse $data to
pull out all the email addresses which I will then sift through to remove any
legitimate addresses.

You'll notice I declare the use of HTML::TokeParser.  This leads to my next
question.  Do I need to use that?  Would it be simpler to just parse the data
matching against a regex and put any matches into a file?  I imagine I don't
need to sift through all the HTML tags just to get to the email addresses since
they are fairly easy to spot.


Hi Mathew

Ordinarily I would insist that you use a proper HTML parser, but I see no harm 
in
searching for email addresses as their format is well defined. Use the
Email::Address module, like this:

use Email::Address;

my @email = Email::Address-parse($agent-content);
print $_-address, \n foreach @email;

HTH,

Rob


--
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 with WWW::Mechanize - Next Question

2006-12-03 Thread Mathew
Rob Dixon wrote:
 Mathew Snyder wrote:
 With all the help I've received I've been able to get this working. 
 This is my
 text:
 #!/usr/bin/perl

 use warnings;
 use strict;
 use WWW::Mechanize;
 use HTML::TokeParser;

 my $username = 'msnyder';
 my $password = 'xxx';
 my $status   = 'open';

 my $agent = WWW::Mechanize-new();
 $agent-get('https://rt.ops.xxx.com/');

 $agent-submit_form(
 form_name = 'login',
 fields= {
 'user' = $username,
 'pass' = $password,
 }
 );

 $agent-follow_link(text = Tickets);

 $agent-submit_form(
 form_name = 'BuildQuery',
 fields= {
 'ValueOfStatus' = $status,
 'ValueOfActor'  = $username,
 },
 button= 'DoSearch'
 );

 my $data = $agent-content();
 print $data;


 What this will do is return to me HTML source with a list of work
 tickets and
 all pertinent, associated data.  The purpose of setting this up is to
 allow me
 to pull out email addresses of any work ticket created as a result of
 spam.

 For anyone not familiar with Request Tracker from Best Practical
 Solutions, the
 'from' email address on any incoming email received by Request Tracker is
 automatically turned into a user account.  With the amount of spam
 flying around
 the the Net these days those user accounts add up.

 All those spam tickets are assigned to me so I can eliminate them and
 the users
 created as a result of them from our database.  My goal is to parse
 $data to
 pull out all the email addresses which I will then sift through to
 remove any
 legitimate addresses.

 You'll notice I declare the use of HTML::TokeParser.  This leads to my
 next
 question.  Do I need to use that?  Would it be simpler to just parse
 the data
 matching against a regex and put any matches into a file?  I imagine I
 don't
 need to sift through all the HTML tags just to get to the email
 addresses since
 they are fairly easy to spot.
 
 Hi Mathew
 
 Ordinarily I would insist that you use a proper HTML parser, but I see
 no harm in
 searching for email addresses as their format is well defined. Use the
 Email::Address module, like this:
 
 use Email::Address;
 
 my @email = Email::Address-parse($agent-content);
 print $_-address, \n foreach @email;
 
 HTH,
 
 Rob
 

I guess I've still got to get used to the fact that if there's something
I want to do in perl, there's probably a module for it.

Thanks.  That should be exactly what I need.


Mathew


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




Help with WWW::Mechanize - Next Question

2006-12-02 Thread Mathew Snyder
With all the help I've received I've been able to get this working.  This is my
text:
#!/usr/bin/perl

use warnings;
use strict;
use WWW::Mechanize;
use HTML::TokeParser;

my $username = 'msnyder';
my $password = 'xxx';
my $status   = 'open';

my $agent = WWW::Mechanize-new();
$agent-get('https://rt.ops.xxx.com/');

$agent-submit_form(
form_name = 'login',
fields= {
'user' = $username,
'pass' = $password,
}
);

$agent-follow_link(text = Tickets);

$agent-submit_form(
form_name = 'BuildQuery',
fields= {
'ValueOfStatus' = $status,
'ValueOfActor'  = $username,
},
button= 'DoSearch'
);

my $data = $agent-content();
print $data;


What this will do is return to me HTML source with a list of work tickets and
all pertinent, associated data.  The purpose of setting this up is to allow me
to pull out email addresses of any work ticket created as a result of spam.

For anyone not familiar with Request Tracker from Best Practical Solutions, the
'from' email address on any incoming email received by Request Tracker is
automatically turned into a user account.  With the amount of spam flying around
the the Net these days those user accounts add up.

All those spam tickets are assigned to me so I can eliminate them and the users
created as a result of them from our database.  My goal is to parse $data to
pull out all the email addresses which I will then sift through to remove any
legitimate addresses.

You'll notice I declare the use of HTML::TokeParser.  This leads to my next
question.  Do I need to use that?  Would it be simpler to just parse the data
matching against a regex and put any matches into a file?  I imagine I don't
need to sift through all the HTML tags just to get to the email addresses since
they are fairly easy to spot.

Mathew

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




Re: Next

2006-07-06 Thread Rob Dixon

Geetha Weerasooriya wrote:

 Dear all,

 When I was reading a Perl code I found the following line. Can u
 please explain what it means?

 !defined($rt_nearest) or $dh$dist or next;

Hi Geetha

Oh dear, it's not very readable is it! I assume you know what 'next'
does. If not, look at perldoc -f next. The statement uses what is called
the 'short-circuit' behaviour of the or operator. perldoc perlop says
this:

:: Binary || performs a short-circuit logical OR operation. That is,
:: if the left operand is true, the right operand is not even evaluated.
:: Scalar or list context propagates down to the right operand if it is
:: evaluated.

So if $rt_nearest is defined then nothing else happens. If it is
undefined but $dh  $dist then again nothing happens. If both are false
then the next is executed to go to the next iteration of the loop. It's
the same as:

  next unless !defined($rt_nearest) or $dh$dist;

or, more clearly

  next if defined($rt_nearest) and $dist  $dh;

which is how it should have been written. It's now clear that the loop
is looking for the 'nearest' of a list of objects. If an object has
already been found ($rt_nearest is defined) and its distance is less
than that of the current object ($dist  $dh) then go to the next in the
list.

HTH,

Rob

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




Re: Next

2006-07-06 Thread marcos rebelo

basically code done by a hacker, not a software developer

if I'm correct this shall mine

if (not( ! defined($rt_nearest) or $dh$dist)) {
   next;
}


On 7/6/06, Geetha Weerasooriya [EMAIL PROTECTED] wrote:

Dear all,

When I was reading a Perl code I found the following line. Can u please
explain what it means?

!defined($rt_nearest) or $dh$dist or next;

Kind regards,

Geetha






--
Marcos Rebelo
http://oleber.awardspace.com/

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




Re: Next

2006-07-06 Thread Xavier Noria

On Jul 6, 2006, at 6:26, Geetha Weerasooriya wrote:


Dear all,

When I was reading a Perl code I found the following line. Can u  
please

explain what it means?

!defined($rt_nearest) or $dh$dist or next;


It means

  next unless !defined($rt_nearest) or $dh  $dist;

or, equivalently,

  next if defined($rt_nearest) and $dh = $dist;

The trick in the original code is: the or operator returns as soon as  
some of the operands evaluates to true, if any. So if


  !defined($rt_nearest)

holds nothing to its right is evaluated. Otherwise we go for

  $dh  $dist

If it holds we are done, otherwise we evaluate the following operand

  next

which has the secondary effect of jumping to the next iteration  
somewhere. Some people dislike using boolean operators to control  
flow that way, some people don't.


-- fxn




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




Next

2006-07-06 Thread Geetha Weerasooriya
Dear All,
 
Thank you so much for sending me the solution to my problem. It was the
first problem I asked and I am really happy I got very well explained
answers. I understood it well.
 
Thanks again for every one who take time to answer my question.
 
Kind regards,
 
Geetha


Next

2006-07-05 Thread Geetha Weerasooriya
Dear all,
 
When I was reading a Perl code I found the following line. Can u please
explain what it means?
 
!defined($rt_nearest) or $dh$dist or next;
 
Kind regards,
 
Geetha 
 


How to handle \ indicating next line

2005-12-27 Thread Khairul Azmi
Hi all,
I am writing a code that would check line by line content of a line before
processing it. Problem occurs when there are lines that has character \
indicating a new line. For example

preprocessor http_inspect: global \
iis_unicode_map unicode.map 1252

When that happen, my code should be able to grab the whole phrase. My
current code would only capture up to global \. Thanks in advance.

if (open (INFILE,$snort_file)) {

while (INFILE) {
my $line = $_;
if ($line !~ /^[[:space:]]*#.*$/) { # handle lines start with #
if (/\S/) { # handle empty lines
my @field = split;

} elsif ($field[0] eq preprocessor) {
get_preprocessor ($line);
   }


variable declaration, was RE: moving to the next line

2004-12-17 Thread Chris Devers
On Thu, 16 Dec 2004, Charles K. Clarkson wrote:

 Don't declare all your variables at the beginning
 of the script. It works in other languages, but not
 in perl. Declare them as you go.

Out of curiosity, why this rule? 

When taking programming classes in college, it was drummed into us that 
having a data dictionary at the top of a scope was a good habit, and 
it's something that I've generally done with the Perl I've written. 

Several people on this list have discouraged the habit. How come?

I can see the logic in discouraging global variables, but predeclaring 
variables at the top of a scope -- the beginning of a subroutines, and a 
small handful in the main block -- still seems acceptable to me. Indeed, 
waiting to declare until the variable is used is, to my thinking, kind 
of defeating the point of using 'strict': if the declarations are 
scattered all over the place, why bother being strict? 

Perl is an eccentric language to be sure, but why over this? Anyone care 
to explain? 

Thanks :-)



-- 
Chris Devers

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




Re: variable declaration, was RE: moving to the next line

2004-12-17 Thread Jonathan Paton
On Fri, 17 Dec 2004 09:17:05 -0500 (EST), Chris Devers
[EMAIL PROTECTED] wrote:
 On Thu, 16 Dec 2004, Charles K. Clarkson wrote:
 
  Don't declare all your variables at the beginning
  of the script. It works in other languages, but not
  in perl. Declare them as you go.
 
 Out of curiosity, why this rule?
 
 When taking programming classes in college, it was drummed
 into us that having a data dictionary at the top of a scope was
 a good habit, and it's something that I've generally done with
 the Perl I've written.

And what programming language were you learning?  C requires
all variable declarations to come first, for the benefit of the compiler.
[This requirement may not apply to the latest C specification]

In some other languages, like C, C++ and Java you must specify
the exact type of each variable.  This information is useful to gather
in to one place, out of the way of the actual code.

In perl, you have signals ($%@* etc) to specify the type, and my
to declare that it is a new lexical variable.  You don't have to specify
if you have a char etc.

Also in perl, each block is a new lexical scope - where all is fair to
introduce new variables.  E.g.

for my $index (0 .. $last_index) {
my $computed = ...;

...
}

where $index and $computed wouldn't exist outside of the loop.  This
is easier to read than:

my $index;
my $computed;

for $index (0 .. $last_index) {
$computed = ...;

...
}

Where $index and $computed live on, but may never be used again.

Generally, fewer lines of code are easier to manage.  That is until you
start writing obfuscated perl code, but you usually need to be skilled to
do that :-)  Just see my JAPH (below) or search google for others!

 Perl is an eccentric language to be sure, ...

Not as much as the programmers that use it ;-)

Jonathan Paton

-- 
#!perl
$J=' 'x25 ;for (qq 1+10 9+14 5-10 50-9 7+13 2-18 6+13
17+6 02+1 2-10 00+4 00+8 3-13 3+12 01-5 2-10 01+1 03+4
00+4 00+8 1-21 01+1 00+5 01-7 =~/ \S\S \S\S /gx) {m/(
\d+) (.+) /x,, vec$ J,$p +=$2 ,8,= $c+= +$1} warn $J,,

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




Re: variable declaration, was RE: moving to the next line

2004-12-17 Thread Paul Johnson
On Fri, Dec 17, 2004 at 09:17:05AM -0500, Chris Devers wrote:

 On Thu, 16 Dec 2004, Charles K. Clarkson wrote:
 
  Don't declare all your variables at the beginning
  of the script. It works in other languages, but not
  in perl. Declare them as you go.

I suppose that depends on your definition of works.

 Out of curiosity, why this rule? 

Locality of reference.  

 When taking programming classes in college, it was drummed into us that 
 having a data dictionary at the top of a scope was a good habit, and 
 it's something that I've generally done with the Perl I've written. 
 
 Several people on this list have discouraged the habit. How come?
 
 I can see the logic in discouraging global variables, but predeclaring 
 variables at the top of a scope -- the beginning of a subroutines, and a 
 small handful in the main block -- still seems acceptable to me. Indeed, 
 waiting to declare until the variable is used is, to my thinking, kind 
 of defeating the point of using 'strict': if the declarations are 
 scattered all over the place, why bother being strict? 
 
 Perl is an eccentric language to be sure, but why over this? Anyone care 
 to explain? 
 
 Thanks :-)
 
 
 
 -- 
 Chris Devers
 
 -- 
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 http://learn.perl.org/ http://learn.perl.org/first-response
 
 
 

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

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




Re: variable declaration, was RE: moving to the next line

2004-12-17 Thread Chris Devers
On Fri, 17 Dec 2004, Jonathan Paton wrote:

 On Fri, 17 Dec 2004 09:17:05 -0500 (EST), Chris Devers wrote:
 
 And what programming language were you learning? 

C/C++, Java, Visual Basic, Cobol.

 In some other languages, like C, C++ and Java you must specify the 
 exact type of each variable.  This information is useful to gather in 
 to one place, out of the way of the actual code.
 
 In perl, you have signals ($%@* etc) to specify the type, and my to 
 declare that it is a new lexical variable.  You don't have to specify 
 if you have a char etc.

I always thought of grouping typing as incidental to the real point of 
pre-declaring variables. The real point, it seemed to me, was built in 
to the phrase data dictionary: it's a segment of code that maps out 
what pieces of information the following section of code will deal with.
 

 Also in perl, each block is a new lexical scope - where all is fair to
 introduce new variables.  E.g.
 
 for my $index (0 .. $last_index) {
 my $computed = ...;
 
 ...
 }
 
 where $index and $computed wouldn't exist outside of the loop.  This
 is easier to read than:
 
 my $index;
 my $computed;
 
 for $index (0 .. $last_index) {
 $computed = ...;
 
 ...
 }
 
 Where $index and $computed live on, but may never be used again.

*shrug*

I see little readibility difference there.
 
 Generally, fewer lines of code are easier to manage.  

Well, of course. But sometimes spelling things out can be useful too.



-- 
Chris Devers

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




Re: variable declaration, was RE: moving to the next line

2004-12-17 Thread Lawrence Statton
 Out of curiosity, why this rule? 
 
 When taking programming classes in college, it was drummed into us that 
 having a data dictionary at the top of a scope was a good habit, and 
 it's something that I've generally done with the Perl I've written. 
 
 Several people on this list have discouraged the habit. How come?

Because Perl isn't COBOL g

As a Perl programmer, I do tend to declare at first usage, but I
disagree strongly that it defeats the purpose of strict -- in my mind
the single most glowing feature of strictures is that it catches typos
... Especially really COMMON typos that I make (your flubs will vary)

my %option = (.)

Pet_it() if $optoin{fuzzy};

Without strict, I'd be bald as a cue-ball from tearing my hair out
looking for every misspelling of option.  

When I was a younger Perl programmer, I did have the habit of
declaring all my variables near the top of the scope block -- a habit
I carried over from C.  I also wrote a lot of for ( $ii = 0 ; $ii  10
; $ii++ ) and other Bad Habits.

By doing that, I found it tends, through human laziness, to be a
write-only field.  As my code morphs and finally gels, I was finding
that I had a ...

my qw / $this $that $the_other @why_wont_this_work %temp_hash
%temp2_hash %temp3_hash /; 

... that goes on for three lines, of which, in the end, only three
variables were actually used.

Another benefit -- by keeping the declaration close to their actual
instantiation - you make the job of cutting-and-pasting and
rearranging code just a little bit easier.  And making code
housekeeping easier means you're more likely to do it, and that will
save you a LOT of time six months when you come back to this program
and have forgotten what it was for.  

I'm pushing forty.  My boss, who is much younger than me, will say,
Remember that module you wrote that does blahblabhlah?  We need to
add in a feature to do bliz bliz blz ...   And I will honestly reply
.. I vaguely remember writing a module, something like that .. What
does it do?  I *have* to write exceptionally clear code, because
tomorrow I won't remember what I wrote yesterday g

So, here are two of *my* survival skills for aging programmers:  

1.  Time spent typing is not wasted.  Never, *ever* abbreviate.
(there are a few exceptions to this in my book -- there are some
idioms so common, that they're self-apparent).  

2.  Use case in a consistent way to communicate additional
information.  I don't want to open the worm-can of is case
sensitivity morally reprehensible - but - as long as we *have*
it, let's *use* it to our benefit.

For example:

Instance accessor/mutator functions have singular
forms of nouns in lowercase.  

$person-age(29); print $employee-pay_rate;  

Instance methods that perform some change have verb forms in lower
case.  $catalog_item-deliver; $trunk_group-disable;

Class methods have uppercase forms:  Employee-Create(); 

Functions/methods that return a list have plural noun forms:

$division-Employees;  $person-email_addresses.

Well -- enough philosophy for one day ... 

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
Lawrence Statton - [EMAIL PROTECTED] s/aba/c/g
Computer  software  consists of  only  two  components: ones  and
zeros, in roughly equal proportions.   All that is required is to
sort them into the correct order.























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




Re: variable declaration, was RE: moving to the next line

2004-12-17 Thread Paul Johnson
Sorry, I sent the last reply before I had finished it.

On Fri, Dec 17, 2004 at 09:17:05AM -0500, Chris Devers wrote:

 On Thu, 16 Dec 2004, Charles K. Clarkson wrote:
 
  Don't declare all your variables at the beginning
  of the script. It works in other languages, but not
  in perl. Declare them as you go.

I suppose that depends on your definition of works.

 Out of curiosity, why this rule? 

Locality of reference.  If accesses of nearby memory are close in time
we have locality of reference.  We can help to achieve this by ensuring
that use of variables are localised to as small a section of code as
possible.  This has the added and arguably more important advantage of
making the program easier to understand and maintain because it might
only be necessary to understand small sections of it at a time.

 When taking programming classes in college, it was drummed into us that 
 having a data dictionary at the top of a scope was a good habit, and 
 it's something that I've generally done with the Perl I've written. 

Curious.  In some languages this is necessary but I think most people
see it as a deficiency in the language rather than the enforcement of
good style.

 Several people on this list have discouraged the habit. How come?
 
 I can see the logic in discouraging global variables, but predeclaring 
 variables at the top of a scope -- the beginning of a subroutines, and a 
 small handful in the main block -- still seems acceptable to me. Indeed, 
 waiting to declare until the variable is used is, to my thinking, kind 
 of defeating the point of using 'strict': if the declarations are 
 scattered all over the place, why bother being strict? 

strict vars is designed to stop you accidently misspelling a variable
name.  You are right in a way in that if all your uses of a variable are
close together there is less chance that you will misspell one of them.
but using strict vars will still catch you if you do.

 Perl is an eccentric language to be sure, but why over this? Anyone care 
 to explain? 

I don't know that this is specific to Perl.  C++, for example, is
probably best written in this way too.

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

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




Re: variable declaration, was RE: moving to the next line

2004-12-17 Thread Chris Devers
On Fri, 17 Dec 2004, Paul Johnson wrote:

 On Fri, Dec 17, 2004 at 09:17:05AM -0500, Chris Devers wrote:
 
  On Thu, 16 Dec 2004, Charles K. Clarkson wrote:
  
   Don't declare all your variables at the beginning
   of the script. It works in other languages, but not
   in perl. Declare them as you go.
 
 I suppose that depends on your definition of works.
 
  Out of curiosity, why this rule? 
 
 Locality of reference.  

Meaning... declare it where you use it, QED ?

The counterargument to that might be spagetti code, QED.

That is, if you declare everything at the last possible line, then 
there's no clear organization of what data is being tracked in a given 
segment of code. On the other hand, if you cluster the declarations a 
bit -- not globally, but lexically -- then to my thinking things are 
clearer.


This isn't something I feel strongly about one way or the other. I just 
find it striking that the advice given out on this list differs so 
starkly with the practice that I have been taught (and have seen other 
people doing in their code) in the past.



-- 
Chris Devers

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




RE: variable declaration, was RE: moving to the next line

2004-12-17 Thread Charles K. Clarkson
Chris Devers [EMAIL PROTECTED] wrote:

: On Thu, 16 Dec 2004, Charles K. Clarkson wrote:
: 
: : Don't declare all your variables at the beginning
: : of the script. It works in other languages, but not
: : in perl. Declare them as you go.
: 
: Out of curiosity, why this rule?
: 
: When taking programming classes in college, it was drummed into
: us that having a data dictionary at the top of a scope was a
: good habit, and it's something that I've generally done with the
: Perl I've written.

I took one college programming course. We used PL/1 on a
Multics mainframe. Personal Computers didn't yet exist. I don't
recall how variables were declared.  :)


: Several people on this list have discouraged the habit. How
: come?

I discourage it mainly because it creates an opportunity for
global variables and for unused variables to remain declared.


: I can see the logic in discouraging global variables, but
: predeclaring variables at the top of a scope -- the beginning of
: a subroutines, and a small handful in the main block -- still
: seems acceptable to me. Indeed, waiting to declare until the
: variable is used is, to my thinking, kind of defeating the point
: of using 'strict': if the declarations are scattered all over
: the place, why bother being strict?

I read a college text on programming once. It referred to
programming in flow charts and then translating the problem from
the charts to the language of choice. Actually I think there was
an intermediate written language in there also. I doubt anyone
does this with perl.

We tend to write scripts with perl the same way we write
essays for book reports. On this list, I assume we don't have many
beginners which know the basic algorithm and data structure of
their script before opening their text editor.

Beginner perl scripts tend to be written as we go. We start
with basic I/O and logic and then fill in the necessary parts.
This style of scripting means a lot more rewriting before the
product is finished. In fact, we can assume that any script
offered here is a work in progress, not a finished application.

 The rules I tend to enforce during code review are a matter
of personal style and of learned programming habits. I'm fortunate
that few others replying here have the time to review code from
this list, making me an authority. Unfortunately, that means I am
not challenged on my rules.

I agree that a data dictionary is a great resource for a
finished product, but it is also an advanced programming concept.
Every time I review code I chance alienating the author. It has
happened before. The tone which creeps in after a particularly
long script is invariably negative and the archives prove how many
readers dislike that.

When I enforce a rule for code review here on this list I tend
to use a shotgun approach. I may never hear from this person
again. I want to hit as many of those habits which lead to the
worst offenses. Namely global variables, poor data structures,
poor I/O handling, poor algorithm design, etc.

I need to stop those practices which often lead problems down
the road. I have to be short-winded enough to not lose my audience
and long-winded enough not to leave out important ideas.
Hopefully, other readers of my reply will ask questions about
specific topics, as you have done.  

 Intermediate and advanced programmers should probably use any
technique to make their scripts easier to read and to maintain.
Beginners need to work on their basic skills and pre-declaring all
variables at the top of scope usually opposes that effort.

HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328


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




moving to the next line

2004-12-15 Thread Christopher Spears
So everybody wants to know what I have been working
on.  I finally got permission to post this:

#!/usr/bin/perl -w
use strict;

my $Pixarport = 7498;
my $host = lex;
my $Pixarfile = $Pixarport.@.$host;
my @line;
my $line;
my @features_array;
my @handles_array;
my $handles;
my $features;
my $sizeof;
my $counter = 0;
my $answer;
my $string;

LOOKING_LINES: while(@line = `cat clicenses_output`){
foreach $line(@line){
next unless $line =~ /\S/;
$line =~ s/^\s+//;
@features_array = split /\s/, $line;#split $line
along whitespace and place into an array
LOOKING_USERS: if ($features_array[0] eq Users) {
($features = $features_array[2]) =~ s/://;  #assign
second element into $features and remove colon
print features: .$features.\n;
LOOKING_HANDLES: while (@line = `cat
clicenses_output`){
if ($line =~ /Users/) {
@features_array = split /\s/, $line;
#split $line
along whitespace and place into an array
goto LOOKING_USERS;
}
next unless $line =~ /\S/;
$line =~ s/^\s+//;
@handles_array = split /\s/, $line; #split 
$line
along whitespace and place into an array
$sizeof = scalar(@handles_array);
if ($sizeof = 9  $handles_array[0] eq 
manny){
($handles = $handles_array[5]) =~ 
s/\),//;
#assign ninth element into $handles and remove ),
print Do you wish to remove the 
floating
license: .$features. (Y or N)?.\n;
$string = join  , @handles_array;
print $string.\n;
print answer: .$answer;
#if ($answer =~ /^[yY]$/) {
#print lmutil lmremove -c 
.$Pixarfile. -h
.$features. .$host. .$Pixarport.
.$handles.\n;
#}
} 
#}
}
}
}

At this point of time, this script doesn't completely
work.  The input it is supposed to processed looks
like this:

Feature usage info:

Users of PhotoRealistic-RenderMan:  (Total of 15
licenses issued;  Total of 11 licenses in use)

  PhotoRealistic-RenderMan v12.000, vendor: pixard
  floating license

3d cell26 /dev/tty (v12.0) (lex/7498 2106), start
Fri 12/10 18:20
3d cell2 /dev/tty (v12.0) (lex/7498 1126), start
Fri 12/10 18:07
3d cell4 /dev/tty (v12.0) (lex/7498 6023), start
Fri 12/10 18:07
3d cell3 /dev/tty (v12.0) (lex/7498 7207), start
Fri 12/10 18:08
3d cell3 /dev/tty (v12.0) (lex/7498 9307), start
Fri 12/10 18:08
3d cell4 /dev/tty (v12.0) (lex/7498 9504), start
Fri 12/10 18:09
3d cell26 /dev/tty (v12.0) (lex/7498 933), start
Fri 12/10 18:15
3d cell20 /dev/tty (v12.0) (lex/7498 1810), start
Fri 12/10 18:18
3d cell20 /dev/tty (v12.0) (lex/7498 711), start
Fri 12/10 18:18
3d cell25 /dev/tty (v12.0) (lex/7498 4306), start
Fri 12/10 18:20
3d cell5 /dev/tty (v12.0) (lex/7498 3514), start
Fri 12/10 18:07

Users of PRMan-INTEL:  (Total of 15 licenses issued; 
Total of 11 licenses in use)

  PRMan-INTEL v12.000, vendor: pixard
  floating license

3d cell26 /dev/tty (v12.0) (lex/7498 1030), start
Fri 12/10 18:20
3d cell2 /dev/tty (v12.0) (lex/7498 1219), start
Fri 12/10 18:07
3d cell4 /dev/tty (v12.0) (lex/7498 6121), start
Fri 12/10 18:07
3d cell3 /dev/tty (v12.0) (lex/7498 3422), start
Fri 12/10 18:08
3d cell3 /dev/tty (v12.0) (lex/7498 3727), start
Fri 12/10 18:08
3d cell4 /dev/tty (v12.0) (lex/7498 2713), start
Fri 12/10 18:09
3d cell26 /dev/tty (v12.0) (lex/7498 3824), start
Fri 12/10 18:15
3d cell20 /dev/tty (v12.0) (lex/7498 4532), start
Fri 12/10 18:18
3d cell20 /dev/tty (v12.0) (lex/7498 4713), start
Fri 12/10 18:18
3d cell25 /dev/tty (v12.0) (lex/7498 6208), start
Fri 12/10 18:20
3d cell5 /dev/tty (v12.0) (lex/7498 5912), start
Fri 12/10 18:07

Users of PRMan-INTEL-NT:  (Total of 15 licenses
issued;  Total of 0 licenses in use)

Or at least  that is a portion of it.  Basically, I
need to go though and grab the features, which are
license names such as PRMan-INTEL-NT.  Then the script
starts looking for handles until it hits Users again. 
In the line:

 manny mannymac.entityfx.com /dev/tty (v6.000)
(lex/7111 503), start Fri 12/10 12:17

503 is the handle.

Originally, this script took input directly from
STDIN, but now I want to use STDIN to process yes
or no from the user, so I am taking input from `cat
clicenses_output`.  Eventually, the latter command

RE: moving to the next line

2004-12-15 Thread Charles K. Clarkson
Christopher Spears [EMAIL PROTECTED] wrote:

: So everybody wants to know what I have been working
: on.  I finally got permission to post this:
: 
: #!/usr/bin/perl -w
: use strict;
: 
: my $Pixarport = 7498;
: my $host = lex;
: my $Pixarfile = $Pixarport.@.$host;
: my @line;
: my $line;
: my @features_array;
: my @handles_array;
: my $handles;
: my $features;
: my $sizeof;
: my $counter = 0;
: my $answer;
: my $string;

Don't declare all your variables at the beginning
of the script. It works in other languages, but not
in perl. Declare them as you go.

: LOOKING_LINES: while(@line = `cat clicenses_output`){

Stop using line labels. They work in perl, but they
are rarely needed.
[snip]

: This is the line giving me issues:
: 
: LOOKING_HANDLES: while (@line = `cat
: clicenses_output`);
: 
: Basically, after I grab $feature I need to move to the
: next element of the array @line and start looking at
: each element until I hit Users again (in which case I
: grab the feature and the process begins again).  So
: how do I make the big jump to that next element?

Why is this output in a loop? Does `cat clicenses_output`
place a new array in @line each time through? Where does it
break its listing?

HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328










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




Multiple Page Web-forms, without passing earlier pages values to the next one?

2004-08-04 Thread Sanjay Arora
I am using RH9, postgreSQL and perl5. Is it possible to get users to submit multiple 
page web-forms but not to pass values from one form to the next? I would like to store 
each page to the database server as it is submitted. Also, to build in the process to 
resume the web-form submission at a later time e.g say there are twent pages to submit 
and the user stops feeding the forms at the tenth form. I would like to email the user 
a reminder so he can log back in and start from the eleventh form. Can anyone tell me 
if this sort of thing is implemented anywhere, so I can study it. Or, can anyone 
provide me pointers on how to do it or links for the same? What modules would I 
require or what type of logic is generally required.

Any help will be greatly appreciated.

With best regards.
Sanjay.



-- 
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 Makefiles which containing a backslash continuation character where the next line is blank or whitespace

2004-08-04 Thread Ken Wolcott
John;

  Thank you very much for the help...works like a charm...now I'll try
to study it carefully to understand how to use this the next time I need
something like this...

Cool...

Ken Wolcott

On Tue, 2004-08-03 at 20:20, John W. Krahn wrote:
 Ken Wolcott wrote:
  Hi;
 
 Hello,
 
I need to find Makefiles that contain a backslash line continuation
  character followed by a blank (or whitespace only) line.  I tried a
  regular expression first but just couldn't get it right.  I then tried
  comparing by a pair of strings, but that isn't right either.
  
  [snip code]
 
 This will do what you want:
 
 #!/usr/bin/perl
 use strict;
 use diagnostics;
 
 while (  ) {
  my $line_num = $.;
  if ( /\\\s*$/ ) {
  my $line_before;
  if ( ( $line_before =  ) =~ /^\s*$/ ) {
  print $ARGV has a backslash continuation to a following blank line at 
 line $line_num\n;
  }
  else {
  $_ = $line_before;
  redo;
  }
  }
  }
 
 __END__
 
 
 
 John
 -- 
 use Perl;
 program
 fulfillment


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




finding Makefiles which containing a backslash continuation character where the next line is blank or whitespace

2004-08-03 Thread Ken Wolcott
Hi;

  I need to find Makefiles that contain a backslash line continuation
character followed by a blank (or whitespace only) line.  I tried a
regular expression first but just couldn't get it right.  I then tried
comparing by a pair of strings, but that isn't right either.

First try:

#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;

my $valid_string = sample text\\n second line is valid\n;
my $invalid_string = sample text \\n   \n;
if ($invalid_string =~ m|\\\n\W$|) {
print INVALID\n ;
} else {
print valid\n;
}
if ($valid_string =~ m|\\\n\W$|) {
print INVALID\n ;
} else {
print valid\n;
}


Second try:

#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;

foreach my $file (@ARGV) {
open (MAKEFILE, $file) or die ERROR: Unable to open  . $file . 
for reading, $!;
my $line_before = ;
my $linecount;
while (my $line = MAKEFILE) {
chomp $line;
$linecount++;
print DEBUG:  . ($linecount-1) .  line (before)  = | .
$line_before . |\n;
print DEBUG:  . $linecount .  line (current) = | . $line
. |\n\n;
if ($line_before =~ m|\\$|) {
print $file .  has a backslash continuation to a following
blank line at or near line  . $linecount . \n if ($line =~ m|^\W*$|);
} else {
$line_before = $line;
}
}
close (MAKEFILE);
}


The first one reports both strings as valid, so the regex is wrong?

The second one incorrectly reports that all Makefiles have one or more
lines with continuation (\) characters at the end of the line followed
by a blank (or whitespace only) line.

Please help,
Thanks in advance,
Ken Wolcott



-- 
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 Makefiles which containing a backslash continuation character where the next line is blank or whitespace

2004-08-03 Thread John W. Krahn
Ken Wolcott wrote:
Hi;
Hello,
  I need to find Makefiles that contain a backslash line continuation
character followed by a blank (or whitespace only) line.  I tried a
regular expression first but just couldn't get it right.  I then tried
comparing by a pair of strings, but that isn't right either.
[snip code]
This will do what you want:
#!/usr/bin/perl
use strict;
use diagnostics;
while (  ) {
my $line_num = $.;
if ( /\\\s*$/ ) {
my $line_before;
if ( ( $line_before =  ) =~ /^\s*$/ ) {
print $ARGV has a backslash continuation to a following blank line at line 
$line_num\n;
}
else {
$_ = $line_before;
redo;
}
}
}
__END__

John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response



next if question

2004-04-22 Thread rmck
hi,

I have a while statement that does a next if a match is made against a reg exprerssion 
of some numbers. 


data file:
Header
10
20
5201
8001
0
80
3802



#!/bin/perl
use strict;
use warnings;
  
   while(  ) {   #read from stdin one line or record at a 
time.
next if $_ =~ /(20|80|Header|)/;  # =~ means match not equal to.
print ;
  }

results:
bash-2.03$ ./clean.pl data.txt 
  
10
0
bash-2.03$

I would like it to have the results as so:
10
5201
0
8001
3802


How do I force my reg exp to match only 20, and 80. Not 8001 or 5201? 

Thanks
Rob


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




Re: next if question

2004-04-22 Thread James Edward Gray II
On Apr 22, 2004, at 8:54 AM, rmck wrote:

hi,

I have a while statement that does a next if a match is made against a 
reg exprerssion of some numbers.

data file:
Header
10
20
5201
8001
0
80
3802


#!/bin/perl
use strict;
use warnings;
   while(  ) {   #read from stdin one line 
or record at a time.
next if $_ =~ /(20|80|Header|)/;  # =~ means match not equal to.
print ;
  }

results:
bash-2.03$ ./clean.pl data.txt
10
0
bash-2.03$
I would like it to have the results as so:
10
5201
0
8001
3802
How do I force my reg exp to match only 20, and 80. Not 8001 or 5201?
next if m/^(?:Header|[28]0)$/;

Hope that helps.

James

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



Re: next if question

2004-04-22 Thread rmck
Both of these work great (thanks):

next if $_ =~ /(^20$|^80$|^Header$)/;
next if m/^(?:Header|[28]0)$/;

So now my results are:
bash-2.03$ ./clean.pl data.txt 
10
5201
8001
0
3802
bash-2.03$ 

How can you have the parsed info printed? 
Can you still use the $_??


So the goal would be:
bash-2.03$ ./clean.pl data.txt 
10
5201
8001
0
3802
##The Rest##
Header
20
80
bash-2.03$

I thought I could do this:

#!/bin/perl
 use strict;
 use warnings;

   while(  ) {  #read from stdin one line or record at a time
next if $_ =~ /(^20$|^80$|^Header$)/;
print ;
if ($_ == /(^20$|^80$|^Header$)/){
$rest = $_;
print ##The Rest##\n;
print $rest;
 }
}

data.txt:
Header
10
20
5201
8001
0
80
3802

BUT lol... Its not working:
bash-2.03$ ./clean.pl data.txt 
10
5201
8001
0
##The Rest##
0
3802
bash-2.03$

Help if you can. Thanks up front 

Rob

-Original Message-
From: James Edward Gray II [EMAIL PROTECTED]
Sent: Apr 22, 2004 6:59 AM
To: rmck [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Subject: Re: next if question

On Apr 22, 2004, at 8:54 AM, rmck wrote:

 hi,

 I have a while statement that does a next if a match is made against a 
 reg exprerssion of some numbers.


 data file:
 Header
 10
 20
 5201
 8001
 0
 80
 3802



 #!/bin/perl
 use strict;
 use warnings;

while(  ) {   #read from stdin one line 
 or record at a time.
 next if $_ =~ /(20|80|Header|)/;  # =~ means match not equal to.
 print ;
   }

 results:
 bash-2.03$ ./clean.pl data.txt
 10
 0
 bash-2.03$

 I would like it to have the results as so:
 10
 5201
 0
 8001
 3802


 How do I force my reg exp to match only 20, and 80. Not 8001 or 5201?

next if m/^(?:Header|[28]0)$/;

Hope that helps.

James


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




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




Re: next if question

2004-04-22 Thread Jeff 'japhy' Pinyan
On Apr 22, rmck said:

bash-2.03$ ./clean.pl data.txt
10
5201
8001
0
3802
##The Rest##
Header
20
80
bash-2.03$

I thought I could do this:

#!/bin/perl
 use strict;
 use warnings;

   while(  ) {  #read from stdin one line or record at a time
next if $_ =~ /(^20$|^80$|^Header$)/;
print ;
if ($_ == /(^20$|^80$|^Header$)/){

You don't want to use == here, you want to use =~.

$rest = $_;
print ##The Rest##\n;
print $rest;
 }
}

Here's how I'd do it:

  my @rest = ();
  while () {
if (/^(20|80|Header)$/) { push @rest, $_ }
else { print }
  }
  print ## The Rest ##\n;
  print @rest;

-- 
Jeff japhy Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
CPAN ID: PINYAN[Need a programmer?  If you like my work, let me know.]
stu what does y/// stand for?  tenderpuss why, yansliterate of course.


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




Re: more on warn .. is next necessary

2004-03-31 Thread Jenda Krynicky
From: Harry Putnam [EMAIL PROTECTED]
 I wasn't able to really understand perldoc -f warn.
 
 I'm doing
 use File::Find;
 open(FILE,$File::Find::name)or warn blah blah: $!;
 
 Two things I'm unsure of:
 
 1) is the `: $!' meaningfull here?
 2) do I need a `next;' following to make `File::Find' go on to the
 next found file?
 
 If so, how do I let the `next' know that open has failed?
 That is, how do I test exit status of open function?  Is it just as in
 shell programing ($?)? 

open(FILE,$File::Find::name)
or warn blah blah: $! and next;

Jenda
= [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


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




Re: output on stderr from `next'

2004-03-29 Thread Randy W. Sims
Harry Putnam wrote:
I'm getting this output on stderr from a next clause:
  Exiting subroutine via next at ./test_bol.pl line 101.
I wondered why this happens.  Is it considered an error or what?

The script is lengthy so not posting it here but the next does exit a
sub routine.  That is why I put it there.  So how do I quiet perl
down about this?
Use 'return' to exit from a subroutine. Use 'next', 'redo', and 'last' 
to alter the execution path in loop construcs.

Randy.

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



more on warn .. is next necessary

2004-03-29 Thread Harry Putnam
I wasn't able to really understand perldoc -f warn.

I'm doing
use File::Find;
open(FILE,$File::Find::name)or warn blah blah: $!;

Two things I'm unsure of:

1) is the `: $!' meaningfull here?
2) do I need a `next;' following to make `File::Find' go on to the next
found file?

If so, how do I let the `next' know that open has failed?
That is, how do I test exit status of open function?  Is it just as
in shell programing ($?)? 

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




`next LABEL' usage

2004-03-29 Thread Harry Putnam

I'm using a next LABEL inside a File::Find
sub wanted {...} loop

It is further buried in a while loop inside the `sub wanted()'

The while loop is while (FILE) on the most recent found file.  I
want this `next LABEL' to bring on a new file... not a new line in
while loop.

So using the `next LABEL' technique how do I designate the wanted()
subroutine as target?  Something like this:

sub LABEL: wanted {
  open(FILE,File::Find::name);
  while (FILE){
if(something) {
then do something
}else{
   next LABEL;
}
  }
}

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




RE: `next LABEL' usage

2004-03-29 Thread Charles K. Clarkson
Harry Putnam [EMAIL PROTECTED] wrote:
: 
: I'm using a next LABEL inside a File::Find
: sub wanted {...} loop
: 
: It is further buried in a while loop inside the
: `sub wanted()'
: 
: The while loop is while (FILE) on the most recent
: found file.  I want this `next LABEL' to bring on a
: new file... not a new line in while loop.
: 
: So using the `next LABEL' technique how do I
: designate the wanted() subroutine as target?
: Something like this:
: 
: sub LABEL: wanted {

I haven't tested it, but I would think this would fail.

:   open(FILE,File::Find::name);
:   while (FILE){
: if(something) {
: then do something
: }else{
:next LABEL;
: }
:   }
: }

As I understand this, you want to immediately open
a new file found in current file with the current while
loop. Is that correct?

First, we need to know a few things.

Are you wanting to recursively call the entire
wanted() subroutine?

Or do you just want to call the while loop portion
only and continue with the execution after?

Is it alright to clobber the currently open file or
do we need to continue processing it afterward?

Is there any test needed to be certain we are not
opening the same file again?


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328





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




Re: `next LABEL' usage

2004-03-29 Thread Harry Putnam
Charles K. Clarkson [EMAIL PROTECTED] writes:
 Harry Putnam [EMAIL PROTECTED] wrote:

[...]

Wants to exit a while loop  inside a File::Find \wanted sub routine.
Is exiting the while loop sufficient.. or does one need to exit from
the current file being offered by `sub find()'

 : So using the `next LABEL' technique how do I
 : designate the wanted() subroutine as target?
 : Something like this:
 : 
 : sub LABEL: wanted {

 I haven't tested it, but I would think this would fail.

 :   open(FILE,File::Find::name);
 :   while (FILE){
 : if(something) {
 : then do something
 : }else{
 :next LABEL;
 : }
 :   }
 : }

 As I understand this, you want to immediately open
 a new file found in current file with the current while
 loop. Is that correct?

Yes...

 First, we need to know a few things.

 Are you wanting to recursively call the entire
 wanted() subroutine?

I guess not no.  I just want to go on to the next file it has found

 Or do you just want to call the while loop portion
 only and continue with the execution after?

Yes

 Is it alright to clobber the currently open file or
 do we need to continue processing it afterward?

It needs to be clobbered.  This file has been processed to a point
where it has already failed tests that show if this file has what we
want.

 Is there any test needed to be certain we are not
 opening the same file again?

I don't think so, but not really sure of the internals of File::Find.
I was assuming if I threw down the file it hands me.  It would hand
me a new different one.

The files offered by `wanted()' are filtered thru an `if' clause

I left this fact out by accident.
sub wanted {
  if(/^\d+$/){.processing..
 open(FILE),$File::Find::name;
 while(FILE{
if(something){
do something
}else{
  next LABEL;
}
 }
  }
} 

Looking at this simplified diagram... I think I may have answered my
own question..  
I want to exit to just above the first `if' filter.  I guess just
exiting the while loop will do that.
so: LABEL: while(FILE){ processing}

is sufficient?


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




RE: `next LABEL' usage

2004-03-29 Thread Charles K. Clarkson
Harry Putnam [EMAIL PROTECTED] wrote:
: 
: Charles K. Clarkson [EMAIL PROTECTED] writes:
:  Harry Putnam [EMAIL PROTECTED] wrote:
: 
: [...]
: 
: Wants to exit a while loop  inside a File::Find \wanted
: sub routine. Is exiting the while loop sufficient.. or
: does one need to exit from the current file being offered
: by `sub find()'
: 
:  : So using the `next LABEL' technique how do I
:  : designate the wanted() subroutine as target?
:  : Something like this:
:  : 
:  : sub LABEL: wanted {
: 
:  I haven't tested it, but I would think this would fail.
: 
:  :   open(FILE,File::Find::name);
:  :   while (FILE){
:  : if(something) {
:  : then do something
:  : }else{
:  :next LABEL;
:  : }
:  :   }
:  : }
: 
:  As I understand this, you want to immediately open
:  a new file found in current file with the current while
:  loop. Is that correct?
: 
: Yes...
: 
:  First, we need to know a few things.
: 
:  Are you wanting to recursively call the entire
:  wanted() subroutine?
: 
: I guess not no.  I just want to go on to the next file
: it has found

Wait a minute! Hold on there Bucko!

When I first read your response I thought you wanted
to open a new file you found on your own, not one that
File::Find would find itself. If that is all you want then
you need only return from the subroutine. No LABEL needed.

sub wanted {
if( /^\d+$/ ) {

# always check for success on open
open FILE, $File::Find::name or
die qq(Cannot open $File::Find::name: $!);

while( FILE ) {
if( something() ){
# do something

} else {

# close FILE and exit sub
close FILE;
return;
}
}
}
close FILE;
return;
}



File::Find calls wanted() every time it finds a file.
Each time it finds a file it resets $File::Find::name
and a bunch of other variables. There is no need for you
to do anything extra to make it work again and again.

It would be wise not to clobber another file opened
as FILE. To do that you would use something like this:

sub wanted {
# don't clobber open FILE
local *FILE;

if( /^\d+$/ ) {

# always check for success on open
open FILE, $File::Find::name or
die qq(Cannot open $File::Find::name: $!);

while( FILE ) {
if( something() ){
# do something

} else {
# exit sub, FILE closes automatically
return;
}
}
}
# exit sub, FILE closes automatically
return;
}


Or in perl 5.6.1 or later:

sub wanted {

if( /^\d+$/ ) {

# always check for success on open
open my $fh, $File::Find::name or
die qq(Cannot open $File::Find::name: $!);

while( $fh ) {
if( something() ){
# do something

} else {
# exit sub, $fh closes automatically
return;
}
}
}
# exit sub, $fh closes automatically
return;
}


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328












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




Re: more on warn .. is next necessary

2004-03-29 Thread John W. Krahn
Harry Putnam wrote:
 
 I wasn't able to really understand perldoc -f warn.
 
 I'm doing
 use File::Find;
 open(FILE,$File::Find::name)or warn blah blah: $!;
 
 Two things I'm unsure of:
 
 1) is the `: $!' meaningfull here?

Yes.


 2) do I need a `next;' following to make `File::Find' go on to the next
 found file?

Use 'return' to exit from the sub and go on to the next file.


 If so, how do I let the `next' know that open has failed?
 That is, how do I test exit status of open function?

open() returns true on success and undef (false) when it fails.


 Is it just as
 in shell programing ($?)?

No.


John
-- 
use Perl;
program
fulfillment

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




Re: more on warn .. is next necessary

2004-03-29 Thread Harry Putnam
John W. Krahn [EMAIL PROTECTED] writes:

 If so, how do I let the `next' know that open has failed?
 That is, how do I test exit status of open function?

 open() returns true on success and undef (false) when it fails.


 Is it just as
 in shell programing ($?)?

 No.

ok, then how is it done syntacticly.  Wrapping the open in an
`if',  I guess would be one way. 

  if(open(FILE,$file)){
   do a string of things;
  }else{
 issue warning message;
 return;
  }

Above would be one test for undefinedness...

 But what if for one reason or
another I wanted to just test to see if the open() had failed. 

What would that sort of test look like?


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




Re: `next LABEL' usage

2004-03-29 Thread Harry Putnam
Charles K. Clarkson [EMAIL PROTECTED] writes:

 HTH,

Definitely and thanks for the examples.  I think I was making this
more complicated that it needed to be.  It's slowly sinking in what
all a `return' can do.

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




Re: more on warn .. is next necessary

2004-03-29 Thread Wiggins d'Anconia
Harry Putnam wrote:
John W. Krahn [EMAIL PROTECTED] writes:


If so, how do I let the `next' know that open has failed?
That is, how do I test exit status of open function?
open() returns true on success and undef (false) when it fails.



Is it just as
in shell programing ($?)?
No.


ok, then how is it done syntacticly.  Wrapping the open in an
`if',  I guess would be one way. 

  if(open(FILE,$file)){
   do a string of things;
  }else{
 issue warning message;
 return;
  }
Above would be one test for undefinedness...

 But what if for one reason or
another I wanted to just test to see if the open() had failed. 

What would that sort of test look like?


open(FILE, $file) or die Failed open: $!;

if you don't mind dieing, or I often prefer using 'unless' then I don't 
need to worry about a dangling else, so similar to what you have above,

unless (open(FILE, $file)) {
  warn Failed open: $!;
  return;
}
# code continues here on success

For your reference,

perldoc perlopentut
perldoc -f open
Also note that $? is,

The status returned by the last pipe close, backtick () command, 
successful call to wait() or waitpid(), or from the system() operator.

perldoc perlop

for more about $?.

http://danconia.org

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



Re: more on warn .. is next necessary

2004-03-29 Thread Harry Putnam
Wiggins d'Anconia [EMAIL PROTECTED] writes:


[...]

 if you don't mind dieing, or I often prefer using 'unless' then I
 don't need to worry about a dangling else, so similar to what you have
 above,

In this case I do.

 unless (open(FILE, $file)) {
warn Failed open: $!;
return;
 }

Ah thanks, yes that look like what I was after thanks

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




Re: `next LABEL' usage

2004-03-29 Thread R. Joseph Newton
Harry Putnam wrote:

 Charles K. Clarkson [EMAIL PROTECTED] writes:

  HTH,

 Definitely and thanks for the examples.  I think I was making this
 more complicated that it needed to be.  It's slowly sinking in what
 all a `return' can do.

Hi Harry,

Glad Charles got you squared away.  I have to say it again, though--you will
make much more progress by focusing on a plain-language description of what you
are trying to accomplish, than by thinking in code.  Or, if you must express
your ideas directly in code, run them in the command line compiler first, to get
the immediately available sanity check, before posting.

The problem with saying I want to do something like this... then showing
guesswork code, is that the code you showed already does whatever it does, or
nothing at all if sytax errors preclude compilation.  How do we know, by looking
at code alone, whether the effect, if any, achieved by the code, is the effect
desired?  Did you notice how quickly the issue resolved once Charles figured out
what you wanted--in words?

Joseph


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




output on stderr from `next'

2004-03-27 Thread Harry Putnam
I'm getting this output on stderr from a next clause:
  Exiting subroutine via next at ./test_bol.pl line 101.

I wondered why this happens.  Is it considered an error or what?

The script is lengthy so not posting it here but the next does exit a
sub routine.  That is why I put it there.  So how do I quiet perl
down about this?

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




Re: output on stderr from `next'

2004-03-27 Thread Randy W. Sims
Harry Putnam wrote:
I'm getting this output on stderr from a next clause:
  Exiting subroutine via next at ./test_bol.pl line 101.
I wondered why this happens.  Is it considered an error or what?

The script is lengthy so not posting it here but the next does exit a
sub routine.  That is why I put it there.  So how do I quiet perl
down about this?
Use 'return' to exit from a subroutine. Use 'next', 'redo', 'last', and 
'goto' to alter the execution path in loop constructs; they must appear 
/inside/ the block owned by the loop construct or within a sub-block.

Randy.

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



Re: output on stderr from `next'

2004-03-27 Thread Paul Johnson
On Sat, Mar 27, 2004 at 03:24:13PM -0500, Randy W. Sims wrote:

 Harry Putnam wrote:
 I'm getting this output on stderr from a next clause:
   Exiting subroutine via next at ./test_bol.pl line 101.
 
 I wondered why this happens.  Is it considered an error or what?
 
 The script is lengthy so not posting it here but the next does exit a
 sub routine.  That is why I put it there.  So how do I quiet perl
 down about this?
 
 
 Use 'return' to exit from a subroutine. Use 'next', 'redo', 'last', and 
 'goto' to alter the execution path in loop constructs; they must appear 
 /inside/ the block owned by the loop construct or within a sub-block.

There are sometimes reasons to exit in an unconventional manner.  You
are looking for:

  no warnings exiting;

Each warning perl gives should be found in perldiag, along with its
category, should you wish to quieten it.

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

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




Re: output on stderr from `next'

2004-03-27 Thread Harry Putnam
Randy W. Sims [EMAIL PROTECTED] writes:

 Use 'return' to exit from a subroutine. Use 'next', 'redo', 'last',
 and 'goto' to alter the execution path in loop constructs; they must
 appear /inside/ the block owned by the loop construct or within a
 sub-block.

Ahh, ok thanks.  Return turns out to be a better choice, does the job
and no warnings.

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




next if statement....

2004-03-18 Thread Greg Schiedler
OK I'm trying to modify the code below to recognize an additional next if statement.  
I have
included a snip of the file that the code uses an input.

Greg

next if /Acct-Session-Id = /;This statment works!

# I added this statement.  I don't understand the /'s in the statement.  And my guess 
is that
I need to modify the statement to properly work with an IP over a number or string.

next if /NAS-IP-Address = 192.168.0.1/;  This statement does not work.

next if /User-Name = $userskp/;  This statement works!

Can anyone tell me why my simple addition will not work as I think it should.

Greg :-)


Code Snipit
...
...
require 'usrmon.cf';

if (-M $file  .04) {

  $date= (localtime)[3];

  dbmopen(%hash, $dbfile, 0600);

  open (FILE, $file) || die;
  open (FILEBK, $filebk) || die;
  while (FILE) {
$/ = '';
next if /Acct-Session-Id = /;
next if /NAS-IP-Address = 192.168.0.1/;
next if /User-Name = $userskp/;
print FILEBK;

if (/Acct-Status-Type = Stop/ || /Acct-Status-Type = Start/) {
  if (/User-Name = ([^]+)/){
$key= $1;
  }
  if (/Acct-Status-Type = Stop/) {
if (/Acct-Session-Time = (\d+)/) {
  $used{$key}+= $1;
  $uses{$key}++;
}
  }
}
  }
  close(FILE);
  open (FILE, $file);
  print FILE \n;



RADIUS Log Snip
...
... Ascend-PreSession-Time = 27
CVX-Modem-Begin-Modulation = V.90
CVX-Modem-Error-Correction = V.42
CVX-Modem-Data-Compression = V.42bis
Timestamp = 1075939364
Timestamp = 1075939362

Wed Feb  4 16:03:11 2004
Acct-Status-Type = Start
User-Name = [EMAIL PROTECTED]
Acct-Session-Id = 062A
Framed-IP-Address = 10.0.0.2
NAS-IP-Address = 192.168.0.1
Timestamp = 1075939456
Acct-Delay-Time = 0
Timestamp = 1075939391

Wed Feb  4 16:04:00 2004
Acct-Status-Type = Stop
User-Name = [EMAIL PROTECTED]
Acct-Session-Id = 062A
Acct-Session-Time = 49
Framed-IP-Address = 10.0.0.2
NAS-IP-Address = 192.168.0.1
Timestamp = 1075939505
Acct-Delay-Time = 0
Timestamp = 1075939440

Wed Feb  4 16:04:21 2004
Acct-Status-Type = Start
NAS-Identifier = cvx00.domain.com
...
...
End RADIUS Log Snipit

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




Re: next if statement....

2004-03-18 Thread Christopher G Tantalo
The slashes are similar to a reg-ex expression on $_ in your example. 
So basically it checks the line to see if that exists and does 
something.  In your case, for the ip to work, all you would have to do 
is remove your quotes so the line matches, since the file doesnt have 
quotes around the ip address.
So, your line:

next if /NAS-IP-Address = 192.168.0.1/;

change to

next if /NAS-IP-Address = 192.168.0.1/;

and it will skip to the next line if that line ever comes up.

--
---
Just Your Friendly Neighborhood
_SPIDEY_
Greg Schiedler wrote:

OK I'm trying to modify the code below to recognize an additional next if statement.  
I have
included a snip of the file that the code uses an input.
Greg

next if /Acct-Session-Id = /;This statment works!

# I added this statement.  I don't understand the /'s in the statement.  And my guess 
is that
I need to modify the statement to properly work with an IP over a number or string.
next if /NAS-IP-Address = 192.168.0.1/;  This statement does not work.

next if /User-Name = $userskp/;  This statement works!

Can anyone tell me why my simple addition will not work as I think it should.

Greg :-)

Code Snipit
...
...
require 'usrmon.cf';
if (-M $file  .04) {

 $date= (localtime)[3];

 dbmopen(%hash, $dbfile, 0600);

 open (FILE, $file) || die;
 open (FILEBK, $filebk) || die;
 while (FILE) {
   $/ = '';
   next if /Acct-Session-Id = /;
   next if /NAS-IP-Address = 192.168.0.1/;
   next if /User-Name = $userskp/;
   print FILEBK;
   if (/Acct-Status-Type = Stop/ || /Acct-Status-Type = Start/) {
 if (/User-Name = ([^]+)/){
   $key= $1;
 }
 if (/Acct-Status-Type = Stop/) {
   if (/Acct-Session-Time = (\d+)/) {
 $used{$key}+= $1;
 $uses{$key}++;
   }
 }
   }
 }
 close(FILE);
 open (FILE, $file);
 print FILE \n;


RADIUS Log Snip
...
... Ascend-PreSession-Time = 27
   CVX-Modem-Begin-Modulation = V.90
   CVX-Modem-Error-Correction = V.42
   CVX-Modem-Data-Compression = V.42bis
   Timestamp = 1075939364
   Timestamp = 1075939362
Wed Feb  4 16:03:11 2004
   Acct-Status-Type = Start
   User-Name = [EMAIL PROTECTED]
   Acct-Session-Id = 062A
   Framed-IP-Address = 10.0.0.2
   NAS-IP-Address = 192.168.0.1
   Timestamp = 1075939456
   Acct-Delay-Time = 0
   Timestamp = 1075939391
Wed Feb  4 16:04:00 2004
   Acct-Status-Type = Stop
   User-Name = [EMAIL PROTECTED]
   Acct-Session-Id = 062A
   Acct-Session-Time = 49
   Framed-IP-Address = 10.0.0.2
   NAS-IP-Address = 192.168.0.1
   Timestamp = 1075939505
   Acct-Delay-Time = 0
   Timestamp = 1075939440
Wed Feb  4 16:04:21 2004
   Acct-Status-Type = Start
   NAS-Identifier = cvx00.domain.com
...
...
End RADIUS Log Snipit
 



-
The information contained in this message may be privileged, confidential, and protected from disclosure. If the reader of this message is not the intended recipient, or any employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that any dissemination, distribution, or copying of this communication is strictly prohibited. If you have received this communication in error, please notify us immediately by replying to the message and deleting it from your computer. 

Thank you. Paychex, Inc.

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



Re: next if statement....

2004-03-18 Thread Paul Archer
12:15pm, Greg Schiedler wrote:

 OK I'm trying to modify the code below to recognize an additional next if statement. 
  I have
 included a snip of the file that the code uses an input.

 Greg

 next if /Acct-Session-Id = /;This statment works!


A 'next if /./' is a short form for:

if ($_ =~ m//) {
next;
}

So... whatever is in between the forward slashes is the pattern you are
looking for within the string contained in your default variable ($_).


A single equals sign is *assignment*, where a double equal sign is a test of
numeric equality (and 'eq' is a test to see if two strings are equal).

HTH,

Paul

 # I added this statement.  I don't understand the /'s in the statement.  And my 
 guess is that
 I need to modify the statement to properly work with an IP over a number or string.

 next if /NAS-IP-Address = 192.168.0.1/;  This statement does not work.

 next if /User-Name = $userskp/;  This statement works!

 Can anyone tell me why my simple addition will not work as I think it should.

 Greg :-)


 Code Snipit
 ...
 ...
 require 'usrmon.cf';

 if (-M $file  .04) {

   $date= (localtime)[3];

   dbmopen(%hash, $dbfile, 0600);

   open (FILE, $file) || die;
   open (FILEBK, $filebk) || die;
   while (FILE) {
 $/ = '';
 next if /Acct-Session-Id = /;
 next if /NAS-IP-Address = 192.168.0.1/;
 next if /User-Name = $userskp/;
 print FILEBK;

 if (/Acct-Status-Type = Stop/ || /Acct-Status-Type = Start/) {
   if (/User-Name = ([^]+)/){
 $key= $1;
   }
   if (/Acct-Status-Type = Stop/) {
 if (/Acct-Session-Time = (\d+)/) {
   $used{$key}+= $1;
   $uses{$key}++;
 }
   }
 }
   }
   close(FILE);
   open (FILE, $file);
   print FILE \n;



 RADIUS Log Snip
 ...
 ... Ascend-PreSession-Time = 27
 CVX-Modem-Begin-Modulation = V.90
 CVX-Modem-Error-Correction = V.42
 CVX-Modem-Data-Compression = V.42bis
 Timestamp = 1075939364
 Timestamp = 1075939362

 Wed Feb  4 16:03:11 2004
 Acct-Status-Type = Start
 User-Name = [EMAIL PROTECTED]
 Acct-Session-Id = 062A
 Framed-IP-Address = 10.0.0.2
 NAS-IP-Address = 192.168.0.1
 Timestamp = 1075939456
 Acct-Delay-Time = 0
 Timestamp = 1075939391

 Wed Feb  4 16:04:00 2004
 Acct-Status-Type = Stop
 User-Name = [EMAIL PROTECTED]
 Acct-Session-Id = 062A
 Acct-Session-Time = 49
 Framed-IP-Address = 10.0.0.2
 NAS-IP-Address = 192.168.0.1
 Timestamp = 1075939505
 Acct-Delay-Time = 0
 Timestamp = 1075939440

 Wed Feb  4 16:04:21 2004
 Acct-Status-Type = Start
 NAS-Identifier = cvx00.domain.com
 ...
 ...
 End RADIUS Log Snipit

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



-
Welcome to downtown Coolsville--population: us.
-

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




RE: next if statement....

2004-03-18 Thread Ed Christian
Paul Archer wrote:
 12:15pm, Greg Schiedler wrote:
 
 OK I'm trying to modify the code below to recognize an additional
 next if statement.  I have included a snip of the file that the code
 uses an input. 
 
 Greg
 
 next if /Acct-Session-Id = /;This statment works!
 
 
 A 'next if /./' is a short form for:
 
 if ($_ =~ m//) {
   next;
 }
 
 So... whatever is in between the forward slashes is the pattern you
 are looking for within the string contained in your default variable
 ($_). 
 
 
 A single equals sign is *assignment*, where a double equal sign is a
 test of numeric equality (and 'eq' is a test to see if two strings
 are equal). 
 

Note, you're not using the equals as an assignment here. You're matching
on the literal = in the string. 

 HTH,
 
 Paul
 
 # I added this statement.  I don't understand the /'s in the
 statement.  And my guess is that I need to modify the statement to
 properly work with an IP over a number or string. 
 
 next if /NAS-IP-Address = 192.168.0.1/;  This statement does not
 work. 
 

In your log, the line to be matched is:

NAS-IP-Address = 192.168.0.1

No quotes. It should match if you pull the quotes out of your regex
statement, though I'd also suggest adding a \ before the .'s to ensure
you match a period and not a wild character... 


 next if /User-Name = $userskp/;  This statement works!
 
 Can anyone tell me why my simple addition will not work as I think
 it should. 
 
 Greg :-)
 
 
 Code Snipit
 ...
 ...
 require 'usrmon.cf';
 
 if (-M $file  .04) {
 
   $date= (localtime)[3];
 
   dbmopen(%hash, $dbfile, 0600);
 
   open (FILE, $file) || die;
   open (FILEBK, $filebk) || die;
   while (FILE) {
 $/ = '';
 next if /Acct-Session-Id = /;
 next if /NAS-IP-Address = 192.168.0.1/;
 next if /User-Name = $userskp/;
 print FILEBK;
 
 if (/Acct-Status-Type = Stop/ || /Acct-Status-Type = Start/) {
   if (/User-Name = ([^]+)/){
 $key= $1;
   }
   if (/Acct-Status-Type = Stop/) {
 if (/Acct-Session-Time = (\d+)/) {
   $used{$key}+= $1;
   $uses{$key}++;
 }
   }
 }
   }
   close(FILE);
   open (FILE, $file);
   print FILE \n;
 
 
 
 RADIUS Log Snip
 ...
 ... Ascend-PreSession-Time = 27
 CVX-Modem-Begin-Modulation = V.90
 CVX-Modem-Error-Correction = V.42
 CVX-Modem-Data-Compression = V.42bis
 Timestamp = 1075939364
 Timestamp = 1075939362
 
 Wed Feb  4 16:03:11 2004
 Acct-Status-Type = Start
 User-Name = [EMAIL PROTECTED]
 Acct-Session-Id = 062A
 Framed-IP-Address = 10.0.0.2
 NAS-IP-Address = 192.168.0.1
 Timestamp = 1075939456
 Acct-Delay-Time = 0
 Timestamp = 1075939391
 
 Wed Feb  4 16:04:00 2004
 Acct-Status-Type = Stop
 User-Name = [EMAIL PROTECTED]
 Acct-Session-Id = 062A
 Acct-Session-Time = 49
 Framed-IP-Address = 10.0.0.2
 NAS-IP-Address = 192.168.0.1
 Timestamp = 1075939505
 Acct-Delay-Time = 0
 Timestamp = 1075939440
 
 Wed Feb  4 16:04:21 2004
 Acct-Status-Type = Start
 NAS-Identifier = cvx00.domain.com
 ...
 ...
 End RADIUS Log Snipit
 
 --
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 http://learn.perl.org/ http://learn.perl.org/first-response
 
 
 
 -
 Welcome to downtown Coolsville--population: us.
 -
 
 --
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 http://learn.perl.org/ http://learn.perl.org/first-response

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