Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Trying to understand this piece of code
_
Hi

Can someone please tell me what the following code is tryingto do 


      if ( /CR List/ .. /\(2\)/ ) {
         if ( /CR List/ )  { print " $'\n"; }
         elsif ( /\(2\)/ ) {print " $` \n" ;}
else              { print "            $_\n";}
        }

I know that it is trying to print whatever is there between CR List and (2).
 But i am not able guess more than that. 

Regards
Sudhindra



Report this thread to moderator Post Follow-up to this message
Old Post
Sudhindra K S
08-03-04 02:02 PM


Re: Trying to understand this piece of code
From: "sudhindra k s" <sudhindra_k_s@rediffmail.com>
> Can someone please tell me what the following code is trying to do
>
>       if ( /CR List/ .. /\(2\)/ ) {
>          if ( /CR List/ )  { print " $'\n"; }
>          elsif ( /\(2\)/ ) {print " $` \n" ;}
>          else              { print "            $_\n";}
>         }
>
> I know that it is trying to print whatever is there between CR List
> and (2). But i am not able guess more than that.

The magic is in the .. operator. It returns false until the first
operand evaluates to true, then it returns true until after the
second operand evaluates to true, then again false until the first
becomes true ...

Let me give you a simpler example:

foreach my $var (1,2,3,4,5,6,7,8,9) {
if ($var == 3 .. $var == 6) {
print "$var\n";
}
}


The operator works like the following code:

my $_status = 1;
foreach my $var (1,2,3,4,5,6,7,8,9) {
if ($_status == 1) {
if ($var == 3) {
$_status = 2;
print "$var\n";
}
} else {
print "$var\n";
if ($var == 6) {
$_status = 1;
}
}
}

HTH, Jenda
===== Jenda@Krynicky.cz === 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


Report this thread to moderator Post Follow-up to this message
Old Post
Jenda Krynicky
08-03-04 02:02 PM


RE: Trying to understand this piece of code
sudhindra k s wrote:
> Hi
>
> Can someone please tell me what the following code is trying to do
>
>
>       if ( /CR List/ .. /\(2\)/ ) {
>          if ( /CR List/ )  { print " $'\n"; }
>          elsif ( /\(2\)/ ) {print " $` \n" ;}
>          else              { print "            $_\n";}
>         }
>
> I know that it is trying to print whatever is there between CR List
> and (2). But i am not able guess more than that.

The $' and $` are builtin Perl variables. They are documented in

perldoc perlvar

$' is the text following the last successful match (i.e. the stuff on the
line to the right of "CR List"). $` is the text preceeding the last
successful match (the stuff on the line to the left of "(2)")

Report this thread to moderator Post Follow-up to this message
Old Post
Bob Showalter
08-03-04 02:02 PM


RE: Trying to understand this piece of code
On Mon, 2 Aug 2004, Bob Showalter wrote:

> sudhindra k s wrote: 
>
> The $' and $` are builtin Perl variables. They are documented in
>
>   perldoc perlvar
>
> $' is the text following the last successful match (i.e. the stuff on
> the line to the right of "CR List"). $` is the text preceeding the
> last successful match (the stuff on the line to the left of "(2)")

And according to several reliable sources of documentation, notably
_Mastering Regular Expressions_, they should *never be used*.

The presence of these variables anywhere in your code changes the
pattern matching strategy that Perl uses in such a way that performance
is much, much worse than normal in most cases.

The whys and hows of this are complicated -- it's worth reading _MRE_.
The short version is that you should almost always replace code that
uses $`, $&, or $' with functionally equivalent code that gets the same
result without using these variables. For example, this code could be:

if ( /CR List(.*)/ .. /(.*)\(2\)/ ) {
if ( /CR List/ )  { print " $1\n"; }
elsif ( /\(2\)/ ) { print " $1 \n" ;}
else              { print "            $_\n";}
}

...or something to that effect.



Of course, it could be that more recent versions of Perl have fixed this
problem and I'm not aware of it. If so, please correct me :-)



--
Chris Devers

Report this thread to moderator Post Follow-up to this message
Old Post
Chris Devers
08-03-04 02:02 PM


RE: Trying to understand this piece of code
From: Chris Devers <cdevers@pobox.com>
> On Mon, 2 Aug 2004, Bob Showalter wrote: 
> The whys and hows of this are complicated -- it's worth reading _MRE_.
> The short version is that you should almost always replace code that
> uses $`, $&, or $' with functionally equivalent code that gets the
> same result without using these variables. For example, this code
> could be:
>
>         if ( /CR List(.*)/ .. /(.*)\(2\)/ ) {
>            if ( /CR List/ )  { print " $1\n"; }
>            elsif ( /\(2\)/ ) { print " $1 \n" ;}
>            else              { print "            $_\n";}
>         }
>
> ...or something to that effect.

There should be

if ( /CR List(.*)/ .. /(.*?)\(2\)/ ) {

Otherwise you might get different results.
The original finds the first (2) in the line, your version the last
one.

> Of course, it could be that more recent versions of Perl have fixed
> this problem and I'm not aware of it. If so, please correct me :-)

I believe the penalty is still there.

Of course in a one-off script that contains no other regexps it may
not matter, but I'd still recommend forgetting these variables even
exist.

Jenda
===== Jenda@Krynicky.cz === 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


Report this thread to moderator Post Follow-up to this message
Old Post
Jenda Krynicky
08-03-04 02:02 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:39 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.