Help - Search - Member List - Calendar
Full Version: Reg Exp
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Mallik
Dear Friends,

I have the below code.

my $a = ":-:m:-:a:-:l:-:i:-:k"; # Here each letter is separated by :-:
my $del = ':-:'; # Delimeter
my $b;

while ($a ne $b)
{
$a =~ /^$b$del(.?)($del)?/;
my $c = $1;
print "$cn";
$b .= $del . $c;
}

The above code is working fine. But when I change the text in $a like below

$a = ":-:m:-:a:-:l:-::-:k"; # Here I removed the letter l between two '::'s

Now the code is not working properly.

Any help in this is appreciated.

Note: There may be non-alpha numeric chars between ':-:'.

Regards,
Mallik.

John W. Krahn
Mallik wrote:
QUOTE
Dear Friends,

Hello,

QUOTE
I have the below code.

my $a = ":-:m:-:a:-:l:-:i:-:k"; # Here each letter is separated by :-:
my $del = ':-:'; # Delimeter
my $b;

while ($a ne $b)
{
$a =~ /^$b$del(.?)($del)?/;
my $c = $1;
print "$cn";
$b .= $del . $c;
}

The above code is working fine. But when I change the text in $a like below

$a = ":-:m:-:a:-:l:-::-:k";  # Here I removed the letter l between two '::'s

Now the code is not working properly.

Any help in this is appreciated.

Note: There may be non-alpha numeric chars between ':-:'.

Perhaps you need to use split and join:

$a = ':-:m:-:a:-:l:-::-:k';

my $del = ':-:'; # Delimeter

$b = join $del, split $del, $a, -1;



John
--
use Perl;
program
fulfillment

Chris Devers
On Tue, 5 Oct 2004, Mallik wrote:

QUOTE
But I am a new-bie to Perl. Can u give me a example for 'look-ahead'
matches.

30 seconds on Google returned *lots* of documentation.
Here's some of it:

<http://www.perldoc.com/perl5.8.4/pod/perlre.html>
<http://www.perl.com/pub/a/2001/05/01/expressions.html>
<http://www.regular-expressions.info/lookaround.html>

If you *really* want to learn this stuff, get a copy of Jeffrey Friedl's
_Mastering Regular Expressions_. It's a superb book that explains how
regexes work in a variety of languages, including Perl.

You should also get familiar with perldoc -- Perl's documentation
system. You can look at a lot of perldoc documentation at perldoc.com,
but this should also all exist on your computer as well. Try this:

perldoc perlre

Read over the documentation and let us know if you have more questions.


--
Chris Devers

David le Blanc
Your use of (.?)($del)? does not do what you expect. In the case of
:-:a:-: you will find (.?) = 'a' and ($del)? matches a delimiter, BUT
:-::-: you will find (.?) = ":" and ($del)? matches nothing.

Check out 'look-ahead' matches for a way to solve this.



On Tue, 05 Oct 2004 04:03:21 -0700, John W. Krahn <[Email Removed]> wrote:
QUOTE
Mallik wrote:
Dear Friends,

Hello,

I have the below code.

my $a = ":-:m:-:a:-:l:-:i:-:k"; # Here each letter is separated by :-:
my $del = ':-:'; # Delimeter
my $b;

while ($a ne $b)
{
$a =~ /^$b$del(.?)($del)?/;
my $c = $1;
print "$cn";
$b .= $del . $c;
}

The above code is working fine. But when I change the text in $a like below

$a = ":-:m:-:a:-:l:-::-:k";  # Here I removed the letter l between two '::'s

Now the code is not working properly.

Any help in this is appreciated.

Note: There may be non-alpha numeric chars between ':-:'.

Perhaps you need to use split and join:

$a = ':-:m:-:a:-:l:-::-:k';

my $del = ':-:'; # Delimeter

$b = join $del, split $del, $a, -1;

John
--
use Perl;
program
fulfillment



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



Mallik
Hi David,

Thanks for ur suggestion.

But I am a new-bie to Perl. Can u give me a example for 'look-ahead'
matches.

Regards,
Mallik.

-----Original Message-----
From: David le Blanc [mailto:[Email Removed]]
Sent: Tuesday, October 05, 2004 5:57 PM
To: John W. Krahn
Cc: Perl Beginners
Subject: Re: Reg Exp


Your use of (.?)($del)? does not do what you expect. In the case of
:-:a:-: you will find (.?) = 'a' and ($del)? matches a delimiter, BUT
:-::-: you will find (.?) = ":" and ($del)? matches nothing.

Check out 'look-ahead' matches for a way to solve this.



On Tue, 05 Oct 2004 04:03:21 -0700, John W. Krahn <[Email Removed]> wrote:
QUOTE
Mallik wrote:
Dear Friends,

Hello,

I have the below code.

my $a = ":-:m:-:a:-:l:-:i:-:k"; # Here each letter is separated by :-:
my $del = ':-:'; # Delimeter
my $b;

while ($a ne $b)
{
$a =~ /^$b$del(.?)($del)?/;
my $c = $1;
print "$cn";
$b .= $del . $c;
}

The above code is working fine. But when I change the text in $a like
below

$a = ":-:m:-:a:-:l:-::-:k";  # Here I removed the letter l between two
'::'s

Now the code is not working properly.

Any help in this is appreciated.

Note: There may be non-alpha numeric chars between ':-:'.

Perhaps you need to use split and join:

$a = ':-:m:-:a:-:l:-::-:k';

my $del = ':-:'; # Delimeter

$b = join $del, split $del, $a, -1;

John
--
use Perl;
program
fulfillment



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



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

John W. Krahn
[Please do not top-post. TIA]


David le Blanc wrote:
QUOTE

On Tue, 05 Oct 2004 04:03:21 -0700, John W. Krahn <[Email Removed]> wrote:

Mallik wrote:

I have the below code.

my $a = ":-:m:-:a:-:l:-:i:-:k"; # Here each letter is separated by :-:
my $del = ':-:'; # Delimeter
my $b;

while ($a ne $b)
{
$a =~ /^$b$del(.?)($del)?/;
my $c = $1;
print "$cn";
$b .= $del . $c;
}

The above code is working fine. But when I change the text in $a like below

$a = ":-:m:-:a:-:l:-::-:k";  # Here I removed the letter l between two '::'s

Now the code is not working properly.

Any help in this is appreciated.

Note: There may be non-alpha numeric chars between ':-:'.

Perhaps you need to use split and join:

$a = ':-:m:-:a:-:l:-::-:k';

my $del = ':-:'; # Delimeter

$b = join $del, split $del, $a, -1;

Your use of (.?)($del)? does not do what you expect.  In the case of
:-:a:-:  you will find (.?) = 'a' and ($del)? matches a delimiter, BUT
:-::-:    you will find (.?) = ":" and ($del)? matches nothing.

Check out 'look-ahead' matches for a way to solve this.

What do you mean? I am not using (.?)($del)? in my example.


John
--
use Perl;
program
fulfillment

David le Blanc
On Tue, 05 Oct 2004 15:22:25 -0700, John W. Krahn <[Email Removed]> wrote:
QUOTE
[Please do not top-post.  TIA]


David le Blanc wrote:

On Tue, 05 Oct 2004 04:03:21 -0700, John W. Krahn <[Email Removed]> wrote:

Mallik wrote:

I have the below code.

my $a = ":-:m:-:a:-:l:-:i:-:k"; # Here each letter is separated by :-:
my $del = ':-:'; # Delimeter
my $b;

while ($a ne $b)
{
$a =~ /^$b$del(.?)($del)?/;
my $c = $1;
print "$cn";
$b .= $del . $c;
}

The above code is working fine. But when I change the text in $a like below

$a = ":-:m:-:a:-:l:-::-:k";  # Here I removed the letter l between two '::'s

Now the code is not working properly.

Any help in this is appreciated.

Note: There may be non-alpha numeric chars between ':-:'.

Perhaps you need to use split and join:

$a = ':-:m:-:a:-:l:-::-:k';

my $del = ':-:'; # Delimeter

$b = join $del, split $del, $a, -1;

Your use of (.?)($del)? does not do what you expect.  In the case of
:-:a:-:  you will find (.?) = 'a' and ($del)? matches a delimiter, BUT
:-::-:    you will find (.?) = ":" and ($del)? matches nothing.

Check out 'look-ahead' matches for a way to solve this.

What do you mean?  I am not using (.?)($del)? in my example.

Sorry I was replying to Malliks original. My emailer decided I was replying to
the 'thread' and picked the last email in the thread to attach.

I figured out how to fix this now :-)

ciao

QUOTE




John


John Doe
Hello (again) Derek

Am Samstag, 5. Mrz 2005 05.06 schrieb [Email Removed]:
QUOTE
I have the file as attached and I want to take 104 and 356 to get a sum.
Here is my code:


while (<FF>) {
if ( $. > 6 ) {
if ( $_ !~ "ANR*" or $_ !~ "ANS*" ) {
print FFF $_;
}
}
next unless $_ /^[a-zA-Z0-9]$/ {
print $_;
}
}
[...]

I would start with checking the syntax of this code first (using strict and
warnings) to get the syntax right.

Otherwise, you can't test the code :-)

greetings joe

John W. Krahn
[Email Removed] wrote:
QUOTE
I have the file as attached and I want to take 104 and 356 to get a sum.
Here is my code:


while (<FF>) {
if ( $. > 6 ) {
if ( $_ !~ "ANR*" or $_ !~ "ANS*" ) {
print FFF $_;
}
}
next unless $_ /^[a-zA-Z0-9]$/ {
print $_;
}
}


The pseudocode would be:  skip lines only if $_ begins with a letter and
ends with a number or visa versa,  then add 104 and 356.
OR just add 104 and 356 b/c I still need to keep all the entries as seen in
column 0.


my $total;

while ( <FF> ) {
/^s+(d+)$/ and $total += $1;
}

print "Total = $totaln";



John
--
use Perl;
program
fulfillment

#!/usr/bin/perl

## Set pragmas (LC) and modules (UC)

$^W = 1;
use strict;
use strict 'subs';

## Begin Logic

$ENV{"PATH"} = qq(/home/root:/usr/bin:/usr/sbin);

my $w="40";
my $total="0";
my $outfile1 = qq(/home/root/tsm2_clients.out);
my $outfile2 = qq(/home/root/tsm2_clients.plout);
open (FF, "+<$outfile1") || die "could not open file: $outfile1 $!";
open (FFF, "+<$outfile2") || die "could not open file: $outfile2 $!";
system qq(dsmadmc -id=admin -password=st0rm "tsm:select node_name from
nodes > $outfile1"
);
system qq(dsmadmc -id=admin -password=st0rm "tsm:select count(*) node_name
from nodes >> $
outfile1" );
# what happens when more nodes get added?

while (<FF>) {
if ( $. > 6 ) {
if ( $_ !~ "ANR*" or $_ !~ "ANS*" ) {
print FFF $_;
/^s+(d+) $/ and $total +=$1;
}
}
}
print "Total Nodes on TSM1 and TSM2: $totaln";

close (FF) or warn "error closing $outfile1: $!";
close (FFF) or warn "error closing $outfile2: $!";


I tried the code give by John Krahn as hightlighted and this did not work.
Thanks though... any other ideas?





John Doe
<security.departm
[Email Removed]> To
[Email Removed]
03/04/2005 11:37 cc
PM
Subject
Re: reg exp
Please respond to
security.departme
[Email Removed]







Hello (again) Derek

Am Samstag, 5. Mrz 2005 05.06 schrieb [Email Removed]:
QUOTE
I have the file as attached and I want to take 104 and 356 to get a sum.
Here is my code:


while (<FF>) {
if ( $. > 6 ) {
if ( $_ !~ "ANR*" or $_ !~ "ANS*" ) {
print FFF $_;
}
}
next unless $_ /^[a-zA-Z0-9]$/ {
print $_;
}
}
[...]

I would start with checking the syntax of this code first (using strict and

warnings) to get the syntax right.

Otherwise, you can't test the code :-)

greetings joe

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

Charles K. Clarkson
[Email Removed] <mailto:[Email Removed]> wrote:

: #!/usr/bin/perl
:
: ## Set pragmas (LC) and modules (UC)
:
: $^W = 1;

If you are using a recent version of perl this is better.
Read perllexwarn for details.

use warnings;

: use strict;

This statement includes 'vars', 'subs', and 'refs'.


: use strict 'subs';

This is redundant.



: ## Begin Logic
:
: $ENV{"PATH"} = qq(/home/root:/usr/bin:/usr/sbin);
:
: my $w="40";
: my $total="0";

Avoid placing all variables at the beginning of the script.
It tends to lead to unused variables in the script and variables
which are scoped to larger blocks than necessary.

Define them as you go. Also do not quote numbers. Avoid
using variable names like $w which, BTW, is not unused.


: my $outfile1 = qq(/home/root/tsm2_clients.out);
: my $outfile2 = qq(/home/root/tsm2_clients.plout);
: open (FF, "+<$outfile1") || die "could not open file: $outfile1 $!";
: open (FFF, "+<$outfile2") || die "could not open file: $outfile2 $!";
:
: system qq(dsmadmc -id=admin -password=st0rm "tsm:select node_name
: from nodes > $outfile1" );
: system qq(dsmadmc -id=admin -password=st0rm "tsm:select count(*)
: node_name from nodes >> $outfile1" );
: # what happens when more nodes get added?


my $total = 0;

: while (<FF>) {
: if ( $. > 6 ) {
: if ( $_ !~ "ANR*" or $_ !~ "ANS*" ) {

!~ is an operator for binding a regular expression. Do not
use it with double quoted strings. Having no idea what is in
$_, I can't tell you whether a regular expression or a
different operator is needed here.


: print FFF $_;
: /^s+(d+) $/ and $total +=$1;
: }
: }
: }
: print "Total Nodes on TSM1 and TSM2: $totaln";
:
: close (FF) or warn "error closing $outfile1: $!";
: close (FFF) or warn "error closing $outfile2: $!";
:
:
: I tried the code give by John Krahn as hightlighted and this
: did not work. Thanks though... any other ideas?

"this did not work" is a pretty useless complaint. What
happened which you didn't expect? What did you expect to
happen? Where do you suspect a problem?



HTH,

Charles K. Clarkson
--
Mobile Homes Specialist


John Doe
Hi Derek

QUOTE
#!/usr/bin/perl

## Set pragmas (LC) and modules (UC)

$^W = 1;
use strict;
use strict 'subs';

## Begin Logic

$ENV{"PATH"} = qq(/home/root:/usr/bin:/usr/sbin);

my $w="40";
my $total="0";
my $outfile1 = qq(/home/root/tsm2_clients.out);
my $outfile2 = qq(/home/root/tsm2_clients.plout);
open (FF, "+<$outfile1") || die "could not open file: $outfile1 $!";
open (FFF, "+<$outfile2") || die "could not open file: $outfile2 $!";
system qq(dsmadmc -id=admin -password=st0rm "tsm:select node_name from
nodes > $outfile1"
);
system qq(dsmadmc -id=admin -password=st0rm "tsm:select count(*) node_name
from nodes >> $
outfile1" );

I don't know exactly what you want to achieve.

Maybe your code above produces the outputfile "tsm2_clients.plout" mentioned
in the top post (i have no idea about dsmadmc; if this is important, i cant
help you), and now you want to trie to sum up the two numbers at the end of
this file ("tsm2_clients.plout")

If so,

QUOTE
# what happens when more nodes get added?

John Krahn's code (cited under the next code snippet) does exactly that,
independent from the number of nodes/lines in "tsm2_clients.plout".

QUOTE
while (<FF>) {
if ( $. > 6 ) {
if ( $_ !~ "ANR*" or $_ !~ "ANS*" ) {
print FFF $_;
/^s+(d+) $/ and $total +=$1;
}
}
}
print "Total Nodes on TSM1 and TSM2: $totaln";

Still, line 3 in the above code looks strange.

QUOTE
close (FF) or warn "error closing $outfile1: $!";
close (FFF) or warn "error closing $outfile2: $!";


I tried the code give by John Krahn as hightlighted and this did not work.
Thanks though... any other ideas?

I ran John's code:

bash-2.05b$ perl
use strict;
use warnings;
open FF, "<", "tsm2_clients.plout" or die $!;

my $total;
while ( <FF> ) {
/^s+(d+)$/ and $total += $1; ###--###
}
print "Total = $totaln";

close (FF) or warn $!;


# output:

Total = 460


An explanation of the line marked with ###--###:

The regex searches for lines beginning with space(s), followed by at least one
number before the line end. If this matches, the part after "and" is
executed, which sums up the number read into $total.

maybe $total could be initialized while declared, just for the case that the
file is empty (which would produce a warning "Use of uninitialized value in
print...":
my $total=0; # instead of just my $total;


I just saw Charles's anwer with a bunch of good tips I did not mention to keep
short.

greetings joe

I took everyone's advise and understood. thank you! The code ###- - -###
solved it! Now for an explanation:
during the while loop, the $_ contains return code such as:

ANS8000I Server command: 'tsm:select count(*) from nodes'
ANR1699I Resolved TSM to 2 server(s) - issuing command SELECT against
server(s).
ANR1687I Output for command 'SELECT ' issued against server TSMSRV2
follows:

Unnamed[1]
-----------
356
ANR1688I Output for command 'SELECT ' issued against server TSMSRV2
completed.
ANR1687I Output for command 'SELECT ' issued against server TSMSRV1
follows:

Unnamed[1]
-----------
104
ANR1688I Output for command 'SELECT ' issued against server TSMSRV1
completed.
ANR1694I Server TSMSRV1 processed command 'SELECT ' and completed
successfully.
ANR1694I Server TSMSRV2 processed command 'SELECT ' and completed
successfully.
ANR1697I Command 'SELECT ' processed by 2 server(s): 2 successful, 0 with
warnings, and
0 with errors.

ANS8002I Highest return code was 0.


So in my while loop I say if line not like ANS or ANR using operator !~
"ANR*" or "ANS*".
I did try operator ne and this did not remove these lines. I then tried a
reg exp like
while (<FF>) {
$_ = s/^ANS*/ /g
$_ = s/^ANR*/ /g
}

and this did not remove all these garbage lines. I then tried
if (!/^ANS/ or !/^ANR/ )

and these lines were still printed. So my solution was if ( $_ !~ "ANR*"
or $_ !~ "ANS*" ).


I think this code is really cool and seems to be some type of trick? I
looked in my learning perl and programming perl books and did not find this
code as below. I then noticed in another email thread that ##*## was
used. Are these related? Where can I find documentation on these?


An explanation of the line marked with ###--###:

The regex searches for lines beginning with space(s), followed by at least
one
number before the line end. If this matches, the part after "and" is
executed, which sums up the number read into $total.

thank you,
derek

Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams





John Doe
<security.departm
[Email Removed]> To
[Email Removed]
03/05/2005 01:25 cc
PM [Email Removed]
Subject
Re: reg exp
Please respond to
security.departme
[Email Removed]







Hi Derek

QUOTE
#!/usr/bin/perl

## Set pragmas (LC) and modules (UC)

$^W = 1;
use strict;
use strict 'subs';

## Begin Logic

$ENV{"PATH"} = qq(/home/root:/usr/bin:/usr/sbin);

my $w="40";
my $total="0";
my $outfile1 = qq(/home/root/tsm2_clients.out);
my $outfile2 = qq(/home/root/tsm2_clients.plout);
open (FF, "+<$outfile1") || die "could not open file: $outfile1 $!";
open (FFF, "+<$outfile2") || die "could not open file: $outfile2 $!";
system qq(dsmadmc -id=admin -password=st0rm "tsm:select node_name from
nodes > $outfile1"
);
system qq(dsmadmc -id=admin -password=st0rm "tsm:select count(*)
node_name
from nodes >> $
outfile1" );

I don't know exactly what you want to achieve.

Maybe your code above produces the outputfile "tsm2_clients.plout"
mentioned
in the top post (i have no idea about dsmadmc; if this is important, i cant

help you), and now you want to trie to sum up the two numbers at the end of

this file ("tsm2_clients.plout")

If so,

QUOTE
# what happens when more nodes get added?

John Krahn's code (cited under the next code snippet) does exactly that,
independent from the number of nodes/lines in "tsm2_clients.plout".

QUOTE
while (<FF>) {
if ( $. > 6 ) {
if ( $_ !~ "ANR*" or $_ !~ "ANS*" ) {
print FFF $_;
/^s+(d+) $/ and $total +=$1;
}
}
}
print "Total Nodes on TSM1 and TSM2: $totaln";

Still, line 3 in the above code looks strange.

QUOTE
close (FF) or warn "error closing $outfile1: $!";
close (FFF) or warn "error closing $outfile2: $!";


I tried the code give by John Krahn as hightlighted and this did not
work.
Thanks though... any other ideas?

I ran John's code:

bash-2.05b$ perl
use strict;
use warnings;
open FF, "<", "tsm2_clients.plout" or die $!;

my $total;
while ( <FF> ) {
/^s+(d+)$/ and $total += $1; ###--###
}
print "Total = $totaln";

close (FF) or warn $!;


# output:

Total = 460


An explanation of the line marked with ###--###:

The regex searches for lines beginning with space(s), followed by at least
one
number before the line end. If this matches, the part after "and" is
executed, which sums up the number read into $total.

maybe $total could be initialized while declared, just for the case that
the
file is empty (which would produce a warning "Use of uninitialized value in

print...":
my $total=0; # instead of just my $total;


I just saw Charles's anwer with a bunch of good tips I did not mention to
keep
short.

greetings joe

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

John Doe
Hello Derek

Am Sonntag, 6. Mrz 2005 23.10 schrieb [Email Removed]:
QUOTE
I took everyone's advise and understood.  thank you!  The code

...marked with...?

QUOTE
###- - -###  solved it!


QUOTE
[cut alot away]

and these lines were still printed.  So my solution was  if ( $_ !~ "ANR*"
or $_ !~ "ANS*" ).

I think this code is really cool and seems to be some type of trick?  I
looked in my learning perl and programming perl books and did not find this
code as below.

I spent now one and a half hour, nearly getting crazy, over this if-condition
and the use of double quoted regexes... never saw this anywhere...
endless tests, and "the thing" behaved as a regex should...

and finally found this hint in perldoc perlop:

[[
Binding Operators

Binary "=~" binds a scalar expression to a pattern match. [...]

If the right argument is an expression rather than a search pattern,
substitution, or transliteration, it is interpreted as a search pattern at
run time.

Binary "!~" is just like "=~" except the return value is negated in the
logical sense.
]]


QUOTE
I then noticed in another email thread that  ##*##  was
used.  Are these related?  Where can I find documentation on these?

This was likely just a comment
(although I don't know which thread you're talking about)


greetings joe

(thinking that it would be better if I got into the really dirty deep details
of perl first before answering questions of others on this list again)


[nothing new below]

QUOTE
An explanation of the line marked with ###--###:

The regex searches for lines beginning with space(s), followed by at least
one
number before the line end. If this matches, the part after "and" is
executed, which sums up the number read into $total.

thank you,
derek

Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams





John Doe
<security.departm
[Email Removed]>                                              To
[Email Removed]
03/05/2005 01:25                                          cc
PM                        [Email Removed]
Subject
Re: reg exp
Please respond to
security.departme
[Email Removed]







Hi Derek

#!/usr/bin/perl

## Set pragmas (LC) and modules (UC)

$^W = 1;
use strict;
use strict 'subs';

## Begin Logic

$ENV{"PATH"} = qq(/home/root:/usr/bin:/usr/sbin);

my $w="40";
my $total="0";
my $outfile1 = qq(/home/root/tsm2_clients.out);
my $outfile2 = qq(/home/root/tsm2_clients.plout);
open (FF, "+<$outfile1") || die "could not open file: $outfile1 $!";
open (FFF, "+<$outfile2") || die "could not open file: $outfile2 $!";
system qq(dsmadmc -id=admin -password=st0rm "tsm:select node_name from
nodes > $outfile1"
);
system qq(dsmadmc -id=admin -password=st0rm "tsm:select count(*)

node_name

from nodes >> $
outfile1" );

I don't know exactly what you want to achieve.

Maybe your code above produces the outputfile "tsm2_clients.plout"
mentioned
in the top post (i have no idea about dsmadmc; if this is important, i cant

help you), and now you want to trie to sum up the two numbers at the end of

this file ("tsm2_clients.plout")

If so,

# what happens when more nodes get added?

John Krahn's code (cited under the next code snippet) does exactly that,
independent from the number of nodes/lines in "tsm2_clients.plout".

while (<FF>) {
if ( $. > 6 ) {
if ( $_ !~ "ANR*" or $_ !~ "ANS*" ) {
print FFF $_;
/^s+(d+) $/ and $total +=$1;
}
}
}
print "Total Nodes on TSM1 and TSM2: $totaln";

Still, line 3 in the above code looks strange.

close (FF) or warn "error closing $outfile1: $!";
close (FFF) or warn "error closing $outfile2: $!";


I tried the code give by John Krahn as hightlighted and this did not

work.

Thanks though... any other ideas?

I ran John's code:

bash-2.05b$ perl
use strict;
use warnings;
open FF, "<", "tsm2_clients.plout" or die $!;

my $total;
while ( <FF> ) {
/^s+(d+)$/ and $total += $1; ###--###
}
print "Total = $totaln";

close (FF) or warn $!;


# output:

Total = 460


An explanation of the line marked with ###--###:

The regex searches for lines beginning with space(s), followed by at least
one
number before the line end. If this matches, the part after "and" is
executed, which sums up the number read into $total.

maybe $total could be initialized while declared, just for the case that
the
file is empty (which would produce a warning "Use of uninitialized value in

print...":
my $total=0; # instead of just my $total;


I just saw Charles's anwer with a bunch of good tips I did not mention to
keep
short.

greetings joe

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


greetings.

cool beans! What perldoc has ###- -### in it?
....marked with...? for me is not enough of an explanation. sorry.


yes as I read in programming perl as well lead me to this solution. On pg.
93 it states

" =~ binds a string expression to a pattern match, substitution, or
translation. These operations would otherwise search or modify the string
contained in $_. The string you want to bind is on the left while the
operator is put on the right. The return value indicates the success or
failure of the operator on the right, since the binding operator doesn't
really do anything on its own."


thanks, !
derek




John Doe
<security.departm
[Email Removed]> To
[Email Removed]
03/06/2005 07:43 cc
PM [Email Removed]
Subject
Re: reg exp
Please respond to
security.departme
[Email Removed]







Hello Derek

Am Sonntag, 6. Mrz 2005 23.10 schrieb [Email Removed]:
QUOTE
I took everyone's advise and understood.  thank you!  The code

....marked with...?

QUOTE
###- - -###  solved it!


QUOTE
[cut alot away]

and these lines were still printed.  So my solution was  if ( $_ !~
"ANR*"
or $_ !~ "ANS*" ).

I think this code is really cool and seems to be some type of trick?  I
looked in my learning perl and programming perl books and did not find
this
code as below.

I spent now one and a half hour, nearly getting crazy, over this
if-condition
and the use of double quoted regexes... never saw this anywhere...
endless tests, and "the thing" behaved as a regex should...

and finally found this hint in perldoc perlop:

[[
Binding Operators

Binary "=~" binds a scalar expression to a pattern match. [...]

If the right argument is an expression rather than a search pattern,
substitution, or transliteration, it is interpreted as a search pattern at
run time.

Binary "!~" is just like "=~" except the return value is negated in the
logical sense.
]]


QUOTE
I then noticed in another email thread that  ##*##  was
used.  Are these related?  Where can I find documentation on these?

This was likely just a comment
(although I don't know which thread you're talking about)


greetings joe

(thinking that it would be better if I got into the really dirty deep
details
of perl first before answering questions of others on this list again)


[nothing new below]

QUOTE
An explanation of the line marked with ###--###:

The regex searches for lines beginning with space(s), followed by at
least
one
number before the line end. If this matches, the part after "and" is
executed, which sums up the number read into $total.

thank you,
derek

Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams





John Doe
<security.departm
[Email Removed]
To
[Email Removed]
03/05/2005 01:25
cc
PM                        [Email Removed]

Subject
Re: reg exp
Please respond to
security.departme
[Email Removed]







Hi Derek

#!/usr/bin/perl

## Set pragmas (LC) and modules (UC)

$^W = 1;
use strict;
use strict 'subs';

## Begin Logic

$ENV{"PATH"} = qq(/home/root:/usr/bin:/usr/sbin);

my $w="40";
my $total="0";
my $outfile1 = qq(/home/root/tsm2_clients.out);
my $outfile2 = qq(/home/root/tsm2_clients.plout);
open (FF, "+<$outfile1") || die "could not open file: $outfile1 $!";
open (FFF, "+<$outfile2") || die "could not open file: $outfile2 $!";
system qq(dsmadmc -id=admin -password=st0rm "tsm:select node_name from
nodes > $outfile1"
);
system qq(dsmadmc -id=admin -password=st0rm "tsm:select count(*)

node_name

from nodes >> $
outfile1" );

I don't know exactly what you want to achieve.

Maybe your code above produces the outputfile "tsm2_clients.plout"
mentioned
in the top post (i have no idea about dsmadmc; if this is important, i
cant

help you), and now you want to trie to sum up the two numbers at the end
of

this file ("tsm2_clients.plout")

If so,

# what happens when more nodes get added?

John Krahn's code (cited under the next code snippet) does exactly that,
independent from the number of nodes/lines in "tsm2_clients.plout".

while (<FF>) {
if ( $. > 6 ) {
if ( $_ !~ "ANR*" or $_ !~ "ANS*" ) {
print FFF $_;
/^s+(d+) $/ and $total +=$1;
}
}
}
print "Total Nodes on TSM1 and TSM2: $totaln";

Still, line 3 in the above code looks strange.

close (FF) or warn "error closing $outfile1: $!";
close (FFF) or warn "error closing $outfile2: $!";


I tried the code give by John Krahn as hightlighted and this did not

work.

Thanks though... any other ideas?

I ran John's code:

bash-2.05b$ perl
use strict;
use warnings;
open FF, "<", "tsm2_clients.plout" or die $!;

my $total;
while ( <FF> ) {
/^s+(d+)$/ and $total += $1; ###--###
}
print "Total = $totaln";

close (FF) or warn $!;


# output:

Total = 460


An explanation of the line marked with ###--###:

The regex searches for lines beginning with space(s), followed by at
least
one
number before the line end. If this matches, the part after "and" is
executed, which sums up the number read into $total.

maybe $total could be initialized while declared, just for the case that
the
file is empty (which would produce a warning "Use of uninitialized value
in

print...":
my $total=0; # instead of just my $total;


I just saw Charles's anwer with a bunch of good tips I did not mention to
keep
short.

greetings joe

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


John Doe
Hello Derek

Am Montag, 7. Mrz 2005 02.04 schrieb [Email Removed]:

QUOTE
greetings.

cool beans!  What perldoc has ###- -### in it?
...marked with...?    for me is not enough of an explanation.  sorry.

Sorry, i was just confused.

The '#' sign is (at least most of the time) a comment character;
everything between '#' and the line end (including the '#') is a comment,
ignored by perl.
(sorry, I don't know where it's documented)

One can comment code with it, e.g.

:> /^s+(d+)$/ and $total += $1; # $total only changed if regex match

I used it to mark a code line, to refer to it afterwards in the text, e.g.

:> /^s+(d+)$/ and $total += $1; ###--###
:>
:> bla bla an look at the lines marked with ###--### bla bla...

or to comment out code lines (instead of deleting it),

or even both (commenting out and marking at the same time to later refer to it
in some text, e.g.

:> while (<OLD>) {
:>
:> #*# $_=~s{[d+/d+/d+sd+:d+]s}{};
:> $_=~s{delete}{};
:>
:> #*# print NEW $_ if $_ !~ /^<.*>/;
:> print NEW $_ if $_ !~ /^A/;
:>
:> }
:>
:>
:> bla bla bla and I replaced your lines (marked with #*#) by a
:> simpler version bla bla...

(the lines marked with #*# are not executed because they are a commented out,
means: are a comment for perl)

I could have used something like "#%%marked%%" instead of "##--##" of course.

=*
There are cases where '#' is not a comment but taken literaly, e.g. in the
regex

:> /looking for a literal # in this string/

or in

:> my $string=q#this is a string#;

(although it's possible to have (spaces and) '#' as comment in regexes with
the modifier /x, see perldoc perlre, as e.g. in

:> /this
:> is a multiline # this is a comment within the regex
:> regex with comments
:> /x;

meaning the same as

:> /thisisamultilineregexwithcomments/

)


QUOTE
yes as I read in programming perl as well lead me to this solution.  On pg.
93 it states

" =~ binds a string expression to a pattern match, substitution, or
translation.  These operations would otherwise search or modify the string
contained in $_.  The string you want to bind is on the left while the
operator is put on the right.  The return value indicates the success or
failure of the operator on the right, since the binding operator doesn't
really do anything on its own."

greetings joe

>[...]

is this line

:> $_=~s{delete}{};

a named unary call?


Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams





John Doe
<security.departm
[Email Removed]> To
[Email Removed]
03/07/2005 08:05 cc
AM [Email Removed]
Subject
Re: reg exp
Please respond to
security.departme
[Email Removed]







Hello Derek

Am Montag, 7. Mrz 2005 02.04 schrieb [Email Removed]:

QUOTE
greetings.

cool beans!  What perldoc has ###- -### in it?
...marked with...?    for me is not enough of an explanation.  sorry.

Sorry, i was just confused.

The '#' sign is (at least most of the time) a comment character;
everything between '#' and the line end (including the '#') is a comment,
ignored by perl.
(sorry, I don't know where it's documented)

One can comment code with it, e.g.

:> /^s+(d+)$/ and $total += $1; # $total only changed if regex match

I used it to mark a code line, to refer to it afterwards in the text, e..g.

:> /^s+(d+)$/ and $total += $1; ###--###
:>
:> bla bla an look at the lines marked with ###--### bla bla...

or to comment out code lines (instead of deleting it),

or even both (commenting out and marking at the same time to later refer to
it
in some text, e.g.

:> while (<OLD>) {
:>
:> #*# $_=~s{[d+/d+/d+sd+:d+]s}{};
:> $_=~s{delete}{};
:>
:> #*# print NEW $_ if $_ !~ /^<.*>/;
:> print NEW $_ if $_ !~ /^A/;
:>
:> }
:>
:>
:> bla bla bla and I replaced your lines (marked with #*#) by a
:> simpler version bla bla...

(the lines marked with #*# are not executed because they are a commented
out,
means: are a comment for perl)

I could have used something like "#%%marked%%" instead of "##--##" of
course.

=*
There are cases where '#' is not a comment but taken literaly, e.g. in the
regex

:> /looking for a literal # in this string/

or in

:> my $string=q#this is a string#;

(although it's possible to have (spaces and) '#' as comment in regexes with

the modifier /x, see perldoc perlre, as e.g. in

:> /this
:> is a multiline # this is a comment within the regex
:> regex with comments
:> /x;

meaning the same as

:> /thisisamultilineregexwithcomments/

)


QUOTE
yes as I read in programming perl as well lead me to this solution.  On
pg.
93 it states

" =~ binds a string expression to a pattern match, substitution, or
translation.  These operations would otherwise search or modify the
string
contained in $_.  The string you want to bind is on the left while the
operator is put on the right.  The return value indicates the success or
failure of the operator on the right, since the binding operator doesn't
really do anything on its own."

greetings joe

QUOTE
[...]


John Doe
Am Montag, 7. Mrz 2005 19.54 schrieben Sie:
QUOTE
is this line

:>    $_=~s{delete}{};

a named unary call?

Oups?

It's just a bad choosen example; I should not have used a reserved word as
string content (I realized it after having sent my post)

Its simply a regex which deletes the first occurance of the string "delete" in
$_ ;

it's the same as

$_=~s/delete//;

example:

bash-2.05b$ perl

use warnings; use strict;

my $a="this is deletea string";
$a=~s{delete}{};
print $a,"n";


# this prints:

this is a string




greetings joe

> [...]

Will someone provide clues as to how use logfile rotate on multiple files?
I looked at the rotate.pm and did not see anything this the code. I tried
one File => line followed by multipl scalers but that did not work then I
tried multiple File => references and this did not work???

thank you,




Here is my code:

use strict;
use warnings;
use diagnostics;
use MIME::Lite;
use Logfile::Rotate;

$ENV{"PATH"} = qq(/opt/SUNWsamfs/sbin:/usr/bin:/usr/sbin:/usr/local/log);

my $wd=40;
my $tpexports = qq(/usr/local/log/exports);
my $hrtlabtapeszs = qq(/usr/local/log/heartlab_tapeszs.log);
my $fujitapeszs = qq(/usr/local/log/fuji_tapeszs.log);
my $slhtapeszs = qq(/usr/local/log/slh_tapeszs.log);

open (FFFF_, "+>$tpexports") || die "could not open file: $tpexports $!";
open (FFF_, "+<$hrtlabtapeszs") || die "could not open file: $hrtlabtapeszs
$!";
open (FF_, "+<$fujitapeszs") || die "could not open file: $fujitapeszs $!";
#open (F_, "+<$slhtapeszs") || die "could not open file: $slhtapeszs $!";



my $logs = new Logfile::Rotate (File => $tpexports,
File => $hrtlabtapeszs,
Count => 10,
Gzip => '/usr/bin/gzip',
Dir => '/usr/local/log/old',
Flock => 'yes',
Persist => 'yes' );
$logs->rotate();

Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.