How to match characters in different locations within string

D

Dr.Ruud

Ben Morrow schreef:
Dr.Ruud:
Ben Morrow:
Dr.Ruud:
Well, I might want a localized $_ as alias of $_[0] for every sub.
Are there any good reasons against it?

Subs that want to take $_ as a default value.

An exception is obviously made for a call without arguments, because
then there would not be a $_[0].

That leaves subs where $_ is the default for another argument but the
first. Maybe an attribute could be used for those.

You're not getting it.

Or bluntly ignoring it.

The calling conventions *can't* be changed now,
because it breaks back-compatibility. c.f. Perl6.

Can calling conventions be changed for one file only?
With something like: use subs::localize::it.
 
B

Brian McCauley

Gunnar said:
Brian said:
Gunnar said:
sub isWords {
local $_ = $_[0];

This is a bug waiting to happen.

If isWords() is called in a contest where $_ happens to be alaised to
an element of a tied aggregate then nasty things happen when you do
local($_).

I don't understand that. This code:

[without any tied aggregates]
[works] as I would have expected.

Yes, but if you try an example _with_ a tied aggregate odd things
happen.

http://groups.google.com/group/comp..._frm/thread/85eb2aa2c9688c56/4ade11e9618f7b14
If I exchange 'local $_' in the above example for 'local *_', the $_
variable is empty within the isWords() function.

Note to assign the scalar slot of *_ you need to have a scalar ref
expression on the RHS of the assignment. So it would read.

local *_=\$_[0];

Or

local *_=\shift;

I still prefer

for (shift) {
 
B

Brian McCauley

Ben said:
local *_ = \$_[0];

...although I'd not be certain this actually localises all of *_. Brian,
can you help me here?

Experiment show that is does not on 5.8.7.

My recollection is that this may not always have been the case, but I
don't have an earlier Perl to hand to check this.
 
B

Ben Morrow

Quoth "Dr.Ruud said:
Ben Morrow schreef:

Or bluntly ignoring it.
:)


Can calling conventions be changed for one file only?
With something like: use subs::localize::it.

I guess they could. Indeed, if you *really* want to you could write
yourself a source filter to do just that; or set up an attribute
:implicit (or whatever) which wraps the sub...

Ben
 
G

Gunnar Hjalmarsson

Brian said:
Gunnar said:
If I exchange 'local $_' in the above example for 'local *_', the $_
variable is empty within the isWords() function.

Note to assign the scalar slot of *_ you need to have a scalar ref
expression on the RHS of the assignment. So it would read.

local *_=\$_[0];

Or

local *_=\shift;

Aha, the RHS ref made a difference. Thanks both Brian and Ben!

To me, this notation is anything but intuitive. Is is documented anywhere?
 
B

Ben Morrow

Quoth Gunnar Hjalmarsson said:
Brian said:
Gunnar said:
Brian McCauley wrote:
Always use for() or local(*_) to localise changes to $_.

If I exchange 'local $_' in the above example for 'local *_', the $_
variable is empty within the isWords() function.

Note to assign the scalar slot of *_ you need to have a scalar ref
expression on the RHS of the assignment. So it would read.

local *_=\$_[0];

Or

local *_=\shift;

Aha, the RHS ref made a difference. Thanks both Brian and Ben!

To me, this notation is anything but intuitive. Is is documented anywhere?

"Symbol Tables" in perlmod; which is referred to in "Typeglobs and
Filehandles" in perldata; which is referred to (somewhat obliquely) in
perlref; which is where I started looking :). Not therefore perhaps the
most obvious place...

If you are in a position to, I think it's well worth reading *all* the
Perl documentation, carefully, at least once. There's a lot of stuff
in there in... not-so-obvious places.

Ben
 
G

Gunnar Hjalmarsson

Gunnar said:
Brian said:
Note to assign the scalar slot of *_ you need to have a scalar ref
expression on the RHS of the assignment. So it would read.

local *_=\$_[0];

Or
local *_=\shift;

Aha, the RHS ref made a difference. Thanks both Brian and Ben!

To me, this notation is anything but intuitive. Is is documented anywhere?

Seems as if it can be derived from perldoc perlsub...
 
G

Gunnar Hjalmarsson

Ben said:
Quoth Gunnar Hjalmarsson said:
Brian said:
Note to assign the scalar slot of *_ you need to have a scalar ref
expression on the RHS of the assignment. So it would read.

local *_=\$_[0];

Or

local *_=\shift;

Aha, the RHS ref made a difference. Thanks both Brian and Ben!

To me, this notation is anything but intuitive. Is is documented anywhere?

"Symbol Tables" in perlmod; which is referred to in "Typeglobs and
Filehandles" in perldata; which is referred to (somewhat obliquely) in
perlref; which is where I started looking :). Not therefore perhaps the
most obvious place...

I see. The typeglob stuff. Something I've tried to avoid so far.
If you are in a position to, I think it's well worth reading *all* the
Perl documentation, carefully, at least once. There's a lot of stuff
in there in... not-so-obvious places.

Maybe I will do that some day, when I feel desperate enough. ;-)

Thanks again.
 
J

Jürgen Exner

Jack said:
Thank you kindly.

Whom and for what?
Please quote appropriate context -as has been customary for two decades-
such that people have a chance to know what you are talking about.
what does the \b do in the regular expression ??

Did you check the documentation? From "perldoc perlre":
Perl defines the following zero-width assertions:
\b Match a word boundary

jue
 
T

Tad McClellan

Jack said:
Thank you kindly.


Thank who for what?

Please quote some context in followups like everybody else does.

What does the \b do in the regular expression ??


Perl's regular expressions are documented in:

perldoc perlre



Wasn't it you who said this recently:

j> have I read the docs ? Of course I have - why dont you tell me how with
j> a reg. expression to accomplish this - obviously you cant, nor could I
j> for hours. I think maybe you should read the docs and answer the
j> question.

??


Hmmmm.
 
J

Jack

Tad said:
Thank who for what?

Please quote some context in followups like everybody else does.




Perl's regular expressions are documented in:

perldoc perlre



Wasn't it you who said this recently:

j> have I read the docs ? Of course I have - why dont you tell me how with
j> a reg. expression to accomplish this - obviously you cant, nor could I
j> for hours. I think maybe you should read the docs and answer the
j> question.

??


Hmmmm.

Hey Tad, or I should say hey Pal, its not in these docs:
www.troubleshooters.com/codecorn/littperl/perlreg.htm

Why dont you answer the question instead of being a smart ass
 
S

Scott Bryce

Jack said:
Hey Tad, or I should say hey Pal,

Not if you want to get help here.


That isn't the docs. Try here:

http://perldoc.perl.org/perlre.html

Why dont you answer the question instead of being a smart ass

Another Perl noob begging to be killfiled. Really, acting in an immature
manner will not get you the kind of help you are looking for. Tad gave
you a perfectly good answer to your question. Insulting him because you
didn't understand the answer will get you nowhere.
 
J

Jürgen Exner

Jack said:
Hey Tad, or I should say hey Pal, its not in these docs:
www.troubleshooters.com/codecorn/littperl/perlreg.htm

No idea what those "docs" are, but they are not Perl docs. As the author
claims himself 4 times in the first two paragraphs he is talking about PERL,
not about Perl or perl. Other examples of the author's great wisdom:

Complex string comparisons
$string =~ m/sought_text/;
[...]
Complex string replacements
$string =~ tr/originaltext/newtext/;

Indeed, if the author considers the first example to be complex and uses REs
for textual comparison instead of index() and doesn't know that tr()
operates on neither REs nor text but on lists of characters, then I surmise
that indeed he is talking about something but not about Perl.
Why dont you answer the question instead of being a smart ass

Why don't you read the Perl docs as suggest instead of looking at some
documentation for some obscure PERL.

jue
 
U

Uri Guttman

J> Hey Tad, or I should say hey Pal, its not in these docs:
J> www.troubleshooters.com/codecorn/littperl/perlreg.htm

wow, that is one of the worst perl tutes i have seen in a while. i make
a painful hobby of reading them. you are such a winner for thinking that
is a useful web page. more power to you and i hope you get a job coding
in python!

J> Why dont you answer the question instead of being a smart ass

sorry to inform you but tad IS smart and sometimes an ass so you are
correct!! why don't you ask the author of your perl tutorial for help? i
am sure he is not smart and also not an ass as he is so willing to help
newbies with PERL.

have a good coding life,

uri
 
U

Uri Guttman

J> www.troubleshooters.com/codecorn/littperl/perlreg.htm

this snippet says everything i want to say about that tutorial:

my($blankString) = " ";
$blankString = $blankString . $blankString . $blankString . $blankString;
$blankString = $blankString . $blankString . $blankString . $blankString;


you can't get better code than that. yes, this site is so much better
than the perl docs. everyone must read and obey its coding ideas and
style!

uri
 
J

Jürgen Exner

Uri said:
[...]
you can't get better code than that.

Oh yes, you can. The author has many more ideas ;-)
yes, this site is so much better
than the perl docs. everyone must read and obey its coding ideas and
style!

Another highlight:
$string =~ tr/Bill Clinton/Al Gore/; #replace the president with the
vice president
Funny, I've never heard of a vice president named 'Al or leeee' but that's
probably just me. Or maybe it's a Romulan or Klingon VP.

Also worth reading is the section about 'Resolving Doubledots in A
Filepath'.
Obviously the author not only doesn't about the module that does the task
correctly, but he is also oblivious to the fact that /foo/bar/link/.. may
not point to /foo/bar at all.

Or this one:
Many situations can be done either with groups ( ) or character classes
[ ]. Groups are less quirky and they more often yield the results you were
looking for.

Wow, this is like saying many situations can be done with either a wrench or
a screwdriver. What deep insight.

I can only highly recommend that site, that is if you are looking for a good
laugh. But put down you coffee first and don't try to drink while reading or
you will have to clean your keyboard. YOU HAVE BEEN WARNED!

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,189
Messages
2,571,015
Members
47,616
Latest member
gijoji4272

Latest Threads

Top