I have:
$temp = 'with 855 and 990';
How do I return true or false (ideally through a regular expression)
the existence of both 'with' and 'and' inside the string ? I have
messed around with it for a while and cant figure it out, assistance
would be great. Also how does it change if I test for 3 particular
words as opposed to 2..?
Thank you,
Jack
> How do I return true or false (ideally through a regular expression)
> the existence of both 'with' and 'and' inside the string ?
Hello, welcome to Perl and to CLPMIsc. As you seem new to both, you
are probably not aware that this particular newsgroup has posting
guidelines; you may find these at
http://www.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
> I have messed around with it for a while and cant figure it out
Are you just guessing at a solution, or have you read the docs for
regular expressions (perldoc perlre)?
> assistance would be great.
Sure, we'll be glad to help you fix you code (but you probably won't
find someone here to write it for you). Just show us what you've done
so far. This gives us a frame of reference to what level of
understanding you have so we can adapt our answer accordingly.
sub isWords {
local $_ = $_[0];
/\bwith\b/ && /\band\b/ ? 1 : 0;
}
print "True\n" if isWords($temp);
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
> sub isWords {
> local $_ = $_[0];
That is a copy, but I have often wondered why a localized $_ alias of
$_[0] isn't implicit for a sub.
Is there a clear way to make $_ a localized alias of $_[0]?
With "clear" I mean much clearer than an enclosing "for ( $_[0] ){}"
would.
--
Affijn, Ruud
"Gewoon is een tijger."
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($_).
Always use for() or local(*_) to localise changes to $_.
I find that perfectly clear. It's just a mindset thing I guess.
I don't understand that. This code:
my $temp = 'with 855 and 990';
sub isWords {
local $_ = $_[0];
/\bwith\b/ && /\band\b/ ? 1 : 0;
}
for ('hello') {
print "True\n" if isWords($temp);
print "$_\n";
}
outputs:
True
hello
as I would have expected.
> 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.
Other example:
for my $i (0 .. $#subst)
{
for my $s ($_ )
{
for my $a ($subst[$i])
{
for my $f ($a->[0])
{
for my $t ($a->[1])
{
...
} } } } }
which makes me think about
for my $i (0 .. $#subst)
{
alias ($s, $a, $f, $t) ($_, $subst[$i], $a->[0], $a->[1])
{
...
have I read the docs ? Of course I have - why dont you tell me how with
a reg. expression to accomplish this - obviously you cant, nor could I
for hours. I think maybe you should read the docs and answer the
question. How about it.
There is no way, but you can use a local reference to the array:
local $_ = \@_;
print $_->[0];
That's the closest I can find to what you want.
doesn't test what Brian was talking about. :) I can't remember the
details, but if you just localize a tied $_ there is some confusion
about the tie magic (IIRC it is not restored correctly at the end of the
scope, but I may be wrong).
> > 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.
... so you need to replace
local $_ = ...;
with
local *_;
$_ = ...;
or
for (...) {
:)
Ben
--
Like all men in Babylon I have been a proconsul; like all, a slave ... During
one lunar year, I have been declared invisible; I shrieked and was not heard,
I stole my bread and was not decapitated.
~ benm...@tiscali.co.uk ~ Jorge Luis Borges, 'The Babylon Lottery'
OK. After "use Data::Alias" one can do:
alias local $_ = $_[0];
or alternatively:
alias local ($_) = @_;
but I haven't investigated "it-tied" yet.
Well, I might want a localized $_ as alias of $_[0] for every sub. Are
there any good reasons against it?
But also see the line with the arrow below.
#!/usr/bin/perl
use strict ;
use warnings ;
use Data::Alias ;
sub demo
{
print "demo-0: \$_<$_>, \@_<@_>\n" ;
alias local $_ = $_[0]; # <----
print "demo-1: \$_<$_>, \@_<@_>\n" ;
$_ = '3rd' ;
print "demo-2: \$_<$_>, \@_<@_>\n" ;
}
my $x ;
print "\n" ;
$_ = '1st' ;
$x = '2nd' ;
print "\$_<$_>\n" ;
print "\$x<$x>\n" ;
demo $x, 'y', 'z' ;
print "\$x<$x>\n" ;
print "\$_<$_>\n" ;
print "\n" ;
demo $_, 8, 9 ;
print "\$_<$_>\n" ;
print "\n" ;
__END__
That prints:
$_<1st>
$x<2nd>
demo-0: $_<1st>, @_<2nd y z>
demo-1: $_<2nd>, @_<2nd y z>
demo-2: $_<3rd>, @_<3rd y z>
$x<3rd>
$_<1st>
demo-0: $_<1st>, @_<1st 8 9>
demo-1: $_<1st>, @_<1st 8 9>
demo-2: $_<3rd>, @_<3rd 8 9>
$_<3rd>
Looks to me to behave as should be expected.
Ahhhh, although I object to posts with the tone of the previous one, as
the OP asking for a solution you should be a little more grateful and
polite. No one HAS to give you a solution, so any answers are a favour
and you should see them that way.
Lionel.
$ perl -le'
$temp = q[with 855 and 990];
print $temp =~ /(?=.*\bwith\b)(?=.*\band\b)/ ? q[TRUE] : q[FALSE];
'
TRUE
> Also how does it change if I test for 3 particular
> words as opposed to 2..?
$ perl -le'
$temp = q[with 855 and 990];
print $temp =~ /(?=.*\bwith\b)(?=.*\band\b)(?=.*\b855\b)/ ? q[TRUE] : q[FALSE];
'
TRUE
John
--
use Perl;
program
fulfillment
Subs that want to take $_ as a default value.
--
The Earth is degenerating these days. Bribery and corruption abound.
Children no longer mind their parents, every man wants to write a book,
and it is evident that the end of the world is fast approaching.
Assyrian stone tablet, c.2800 BC benm...@tiscali.co.uk
I see. Guess it's the expression "tied aggregate" I don't understand, then.
If someone could post an applicable example, it would be much appreciated.
>>>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.
>
> ... so you need to replace
>
> local $_ = ...;
>
> with
>
> local *_;
> $_ = ...;
Well, that seems not to be enough in this case. Since I want to copy
$_[0] to $_, I also need a temporary variable:
sub isWords {
my $tmp = $_[0];
local *_;
$_ = $tmp;
/\bwith\b/ && /\band\b/ ? 1 : 0;
}
> or
>
> for (...) {
sub isWords {
for ( $_[0] ) {
return 1 if /\bwith\b/ && /\band\b/;
}
}
Less clumsy, it seems...
>> 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.
sub my_split : respect_it
{
@_ > 1 and alias local $_ = $_[1] ;
...
You're not getting it. The calling conventions *can't* be changed now,
because it breaks back-compatibility. c.f. Perl6.
Ben
--
'Deserve [death]? I daresay he did. Many live that deserve death. And some die
that deserve life. Can you give it to them? Then do not be too eager to deal
out death in judgement. For even the very wise cannot see all ends.'
benm...@tiscali.co.uk
Brian means when $_ is aliased (by for, or whatever) to an element of a
tied array or hash.
> >>If I exchange 'local $_' in the above example for 'local *_', the $_
> >>variable is empty within the isWords() function.
> >
> > ... so you need to replace
> >
> > local $_ = ...;
> >
> > with
> >
> > local *_;
> > $_ = ...;
>
> Well, that seems not to be enough in this case. Since I want to copy
> $_[0] to $_, I also need a temporary variable:
>
> sub isWords {
> my $tmp = $_[0];
> local *_;
> $_ = $tmp;
> /\bwith\b/ && /\band\b/ ? 1 : 0;
> }
Whoops! Didn't spot that... try
local *_ = \$_[0];
...although I'd not be certain this actually localises all of *_. Brian,
can you help me here?
>>>> 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.
[without any tied aggregates]
> [works] as I would have expected.
Yes, but if you try an example _with_ a tied aggregate odd things
happen.
> > 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;
I still prefer
for (shift) {
> 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.
:)
> > 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.
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
--
Every twenty-four hours about 34k children die from the effects of poverty.
Meanwhile, the latest estimate is that 2800 people died on 9/11, so it's like
that image, that ghastly, grey-billowing, double-barrelled fall, repeated
twelve times every day. Full of children. [Iain Banks] benm...@tiscali.co.uk
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...
"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
--
All persons, living or dead, are entirely coincidental.
benm...@tiscali.co.uk Kurt Vonnegut
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.
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
> 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.
--
Tad McClellan SGML consulting
ta...@augustmail.com Perl programming
Fort Worth, Texas
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
> Hey Tad, or I should say hey Pal,
Not if you want to get help here.
> its not in these docs:
> www.troubleshooters.com/codecorn/littperl/perlreg.htm
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.
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
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
--
Uri Guttman ------ u...@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
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!
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