Ask a Question related to PERL Miscellaneous, Design and Development.
-
Alexandra #1
"between" function equivalent in Perl?
Hello All-
I'm attempting to extract a substring of characters from an alphanumeric
text string. I've read a lot of Perl documentation on the 'index' and
'substring' functions; however, I cannot find a regular expression (or an
example of one) that is the equivalent of a "between" function in Perl. Perl
is very good at string manipulation so I assume there must be a way to do
this. Here's what I'm trying to do:
$mystring = 'aaa @ bbbb @ c @ dd @ eeeeee @ FFFFF=xxxx @ ggg @ h'
The goal is to extract the 'xxxx' substring between the '=' sign and the
next whitespace character. (There is no fixed length for the 'xxxx'
substring.)
Rather than use a series of clunky index and substring calls, does anyone
have a better suggestion? If anyone can recommend a good Perl language
reference website (or book) that has some excellent examples of regular
expressions, that would be helpful too.
Thanks in advance,
Alexandra
Alexandra Guest
-
"Devel::DProf" on a PERL script uses "Test::More"
Dear all, I encountered a problem while run profiling on a script which uses "Test::More". May I ask whether anybody have some idea or wrap... -
Is there an equivalent for "alter session set NLS_DATE_FORMAT = 'dd-mm-yyyy'" in Informix?
Hello everybody, does anybody know, if there exists an alternative for "alter session set NLS_DATE_FORMAT = 'dd-mm-yyyy'" in Informix? ... -
Equivalent of "friend" or package-level access?
I have a group of related classes, and I would like them to have certain methods which can only be called by classes within the group. In C++ I... -
How do you simulate "." or "source" in a perl script ?
On 7 Aug 2003 08:21:27 -0700 c_j_marshall@hotmail.com (Chris Marshall) wrote: Shell::Source perhaps this CPAN module will do it for you. ... -
frontpage "shared borders" equivalent in Dw
Hi, I'm trying out Dreamweaver MX and I'm adapting to the new environment after using Frontpage. Basically, I wanted to know if there's an... -
Matija Papec #2
Re: "between" function equivalent in Perl?
X-Ftn-To: Alexandra
"Alexandra" <scorpiorising@ftml.net> wrote:my($match) = $mystring =~ /=(\S+)/;>example of one) that is the equivalent of a "between" function in Perl. Perl
>is very good at string manipulation so I assume there must be a way to do
>this. Here's what I'm trying to do:
>
>$mystring = 'aaa @ bbbb @ c @ dd @ eeeeee @ FFFFF=xxxx @ ggg @ h'
>
>The goal is to extract the 'xxxx' substring between the '=' sign and the
>next whitespace character. (There is no fixed length for the 'xxxx'
>substring.)
I've heard that "Mastering regular expression" is very good.>Rather than use a series of clunky index and substring calls, does anyone
>have a better suggestion? If anyone can recommend a good Perl language
>reference website (or book) that has some excellent examples of regular
>expressions, that would be helpful too.
--
Matija
Matija Papec Guest
-
Brian Wakem #3
Re: "between" function equivalent in Perl?
"Alexandra" <scorpiorising@ftml.net> wrote in message
news:bjl1dk$e7n$1@lumberjack.rand.org...Perl> Hello All-
>
> I'm attempting to extract a substring of characters from an alphanumeric
> text string. I've read a lot of Perl documentation on the 'index' and
> 'substring' functions; however, I cannot find a regular expression (or an
> example of one) that is the equivalent of a "between" function in Perl.> is very good at string manipulation so I assume there must be a way to do
> this. Here's what I'm trying to do:
>
> $mystring = 'aaa @ bbbb @ c @ dd @ eeeeee @ FFFFF=xxxx @ ggg @ h'
>
> The goal is to extract the 'xxxx' substring between the '=' sign and the
> next whitespace character. (There is no fixed length for the 'xxxx'
> substring.)
>
> Rather than use a series of clunky index and substring calls, does anyone
> have a better suggestion? If anyone can recommend a good Perl language
> reference website (or book) that has some excellent examples of regular
> expressions, that would be helpful too.
print $mystring =~ m/=([^\s]+)/;
--
Brian Wakem
Brian Wakem Guest
-
Anno Siegel #4
Re: "between" function equivalent in Perl?
Alexandra <scorpiorising@ftml.net> wrote in comp.lang.perl.misc:
The notion of "between" can very well be expressed in regular expressions.> Hello All-
>
> I'm attempting to extract a substring of characters from an alphanumeric
> text string. I've read a lot of Perl documentation on the 'index' and
> 'substring' functions; however, I cannot find a regular expression (or an
> example of one) that is the equivalent of a "between" function in Perl. Perl
> is very good at string manipulation so I assume there must be a way to do
> this. Here's what I'm trying to do:
>
> $mystring = 'aaa @ bbbb @ c @ dd @ eeeeee @ FFFFF=xxxx @ ggg @ h'
>
> The goal is to extract the 'xxxx' substring between the '=' sign and the
> next whitespace character. (There is no fixed length for the 'xxxx'
> substring.)
If $from and $to are regular expressions, the expression /$from(.*)$to/
catches everything between the first match of $from and the last
match of $to. If you want to delimit from the first match of $from
to the first match of $to after that point, use a non-greedy pattern in the
middle: /from(.*?)$to/. In your case it doesn't matter:
my $from = qr/=/; # a "="
my $to = qr/\s/; # any white space
my ( $between) = $mystring =~ /$from(.*?)$to/;
print "$between\n";
That prints "xxxx".
Anno
Anno Siegel Guest
-
David Oswald #5
Re: "between" function equivalent in Perl?
"Alexandra" <scorpiorising@ftml.net> wrote in message
news:bjl1dk$e7n$1@lumberjack.rand.org...Perl> Hello All-
>
> I'm attempting to extract a substring of characters from an alphanumeric
> text string. I've read a lot of Perl documentation on the 'index' and
> 'substring' functions; however, I cannot find a regular expression (or an
> example of one) that is the equivalent of a "between" function in Perl.You read a "lot" of Perl documentation, and regular expression discussion,> is very good at string manipulation so I assume there must be a way to do
> this. Here's what I'm trying to do:
and didn't find anything therein that inspired a solution?
Assuming there is only one occurrence per string, and that there are no> $mystring = 'aaa @ bbbb @ c @ dd @ eeeeee @ FFFFF=xxxx @ ggg @ h'
>
> The goal is to extract the 'xxxx' substring between the '=' sign and the
> next whitespace character. (There is no fixed length for the 'xxxx'
> substring.)
newlines in the string, this works.
my $matched = $mystring =~ m/=(.*?)\s/;
If there is more than one occurrence per string, and equals may also be
embedded in the portion of the string you're attempting to extract, it
becomes more complicated. Assuming that the space character is unique in
that it is always a delimiter, then you probably should make your life a
little easier by first splitting on space, just so that you don't have to
write a regexp that handles embedded equals differently from
string-initiated equals signs, and stuff like that.
Have you tried the perldocs, specifically 'perldoc perlbook' for book> Rather than use a series of clunky index and substring calls, does anyone
> have a better suggestion? If anyone can recommend a good Perl language
> reference website (or book) that has some excellent examples of regular
> expressions, that would be helpful too.
suggestions? The bible is the Camel book (Programming Perl), and the sunday
school lesson manual is Learning Perl (the Llama book).
David Oswald Guest
-
Alexandra #6
Re: "between" function equivalent in Perl?
"David Oswald" wrote:
> "Alexandra" wrote:an> > Hello All-
> >
> > I'm attempting to extract a substring of characters from an alphanumeric
> > text string. I've read a lot of Perl documentation on the 'index' and
> > 'substring' functions; however, I cannot find a regular expression (ordo> Perl> > example of one) that is the equivalent of a "between" function in Perl.> > is very good at string manipulation so I assume there must be a way toActually, it inspired me to take up hieroglyphics.>> > this. Here's what I'm trying to do:
> You read a "lot" of Perl documentation, and regular expression discussion,
> and didn't find anything therein that inspired a solution?
This also evaluated to 1. I had tried this statement with and without the>> > $mystring = 'aaa @ bbbb @ c @ dd @ eeeeee @ FFFFF=xxxx @ ggg @ h'
> >
> > The goal is to extract the 'xxxx' substring between the '=' sign and the
> > next whitespace character. (There is no fixed length for the 'xxxx'
> > substring.)
> Assuming there is only one occurrence per string, and that there are no
> newlines in the string, this works.
> my $matched = $mystring =~ m/=(.*?)\s/;
leading 'm' and came up with the same result. I'm wondering if the
expression is evaluating at all or if Perl is just seeing it as a true or
valid expression. When I do type in something incorrect I do get a Server
Error. So the expression must be evaluating on some level. Any insight or
further suggestions would be appreciated.
There is only one occurrence of the '=' per string. I'm not sure what> If there is more than one occurrence per string, and equals may also be
> embedded in the portion of the string you're attempting to extract, it
> becomes more complicated. Assuming that the space character is unique in
> that it is always a delimiter, then you probably should make your life a
> little easier by first splitting on space, just so that you don't have to
> write a regexp that handles embedded equals differently from
> string-initiated equals signs, and stuff like that.
embedded means in Perl terms. (Does that mean the '=' is not preceded by a
word boundary or space?) I looked in the "Programming Perl" reference and
didn't find any index or glossary references for embedded or
string-initiated, and I don't have enough sense of Perl yet to distinguish
this intuitively. The '=' in the string does not have any leading or
trailing spaces; it is smashed between two characters.
I've played with the index and substr functions to work with a smaller
initial value for $mystring, but I still get the same Boolean result. I will
try playing with splitting to see if I can get it to work that way, but I
suspect it may return the same result.
anyone> > Rather than use a series of clunky index and substring calls, doessunday>> > have a better suggestion? If anyone can recommend a good Perl language
> > reference website (or book) that has some excellent examples of regular
> > expressions, that would be helpful too.
> Have you tried the perldocs, specifically 'perldoc perlbook' for book
> suggestions? The bible is the Camel book (Programming Perl), and theWe do have Learning Perl and Programming Perl. I find them excellent> school lesson manual is Learning Perl (the Llama book).
references but slow to digest. There are simply not enough examples for me
to work backwards and easily "decode" the language explanations. When
something clicks (from the examples provided) and I re-read that section in
the O'Reilly references, the explanations then make perfect sense. <g> I
suppose it will just mean sheer time and effort in seeking more examples out
on the web as well as helpful hints from groups such as this one.
Alexandra
Alexandra Guest
-
Alexandra #7
Re: "between" function equivalent in Perl?
"Anno Siegel" wrote:> Alexandra wrote in comp.lang.perl.misc:the>> > I'm attempting to extract a substring of characters from an alphanumeric
> > text string. [...] Here's what I'm trying to do:
> >
> > $mystring = 'aaa @ bbbb @ c @ dd @ eeeeee @ FFFFF=xxxx @ ggg @ h'
> >
> > The goal is to extract the 'xxxx' substring between the '=' sign and the
> > next whitespace character. (There is no fixed length for the 'xxxx'
> > substring.)
> The notion of "between" can very well be expressed in regular expressions.
> If $from and $to are regular expressions, the expression /$from(.*)$to/
> catches everything between the first match of $from and the last
> match of $to. If you want to delimit from the first match of $from
> to the first match of $to after that point, use a non-greedy pattern inIf only it would... <g>. For some reason, I'm receiving a 1 as the return> middle: /from(.*?)$to/. In your case it doesn't matter:
>
> my $from = qr/=/; # a "="
> my $to = qr/\s/; # any white space
> my ( $between) = $mystring =~ /$from(.*?)$to/;
> print "$between\n";
>
> That prints "xxxx".
value. I did some debugging to ensure the $to and $from values are correct
and that the string is a valid string value. I also tried using other valid
character values in the $to and $from fields to see if the '=' was the
culprit and still the expression evaluated to 1.
Alexandra
Alexandra Guest
-
Alexandra #8
Re: "between" function equivalent in Perl?
"Brian Wakem" wrote:
> "Alexandra" wrote:anyone> > this. Here's what I'm trying to do:
> >
> > $mystring = 'aaa @ bbbb @ c @ dd @ eeeeee @ FFFFF=xxxx @ ggg @ h'
> >
> > The goal is to extract the 'xxxx' substring between the '=' sign and the
> > next whitespace character. (There is no fixed length for the 'xxxx'
> > substring.)
> >
> > Rather than use a series of clunky index and substring calls, doesThanks for your reply. For some reason this also evaluated to 1, instead of>> > have a better suggestion? If anyone can recommend a good Perl language
> > reference website (or book) that has some excellent examples of regular
> > expressions, that would be helpful too.
>
> print $mystring =~ m/=([^\s]+)/;
the desired substring. And there is not a "1" character in the initial
string ($mystring). I tried placing brackets [] around the equal sign and
still no dice.
The expression seems, somehow, to be evaluating for a truth value. (??)
Alexandra
Alexandra Guest
-
Alexandra #9
Re: "between" function equivalent in Perl?
"Matija Papec" wrote:
Perl> "Alexandra" wrote:> >example of one) that is the equivalent of a "between" function in Perl.Thanks for your reply. Unfortunately, I only received a "1" as the return>> >is very good at string manipulation so I assume there must be a way to do
> >this. Here's what I'm trying to do:
> >
> >$mystring = 'aaa @ bbbb @ c @ dd @ eeeeee @ FFFFF=xxxx @ ggg @ h'
> >
> >The goal is to extract the 'xxxx' substring between the '=' sign and the
> >next whitespace character. (There is no fixed length for the 'xxxx'
> >substring.)
> my($match) = $mystring =~ /=(\S+)/;
value, as I also did with some of the other suggestions. I'm not sure if
this means it's evaluating to Boolean (or why). I did some debugging to
ensure $mystring is a valid text value, and it seems to be.
We do have some of the O'Reilly books in the office but not that one. Thank>> >Rather than use a series of clunky index and substring calls, does anyone
> >have a better suggestion? If anyone can recommend a good Perl language
> >reference website (or book) that has some excellent examples of regular
> >expressions, that would be helpful too.
> I've heard that "Mastering regular expression" is very good.
you, I'll check it out.
Alexandra
Alexandra Guest
-
Uri Guttman #10
Re: "between" function equivalent in Perl?
>>>>> "A" == Alexandra <sagittaur@ftml.net> writes:
A> Thanks for your reply. Unfortunately, I only received a "1" as the return>> my($match) = $mystring =~ /=(\S+)/;
A> value, as I also did with some of the other suggestions. I'm not sure if
A> this means it's evaluating to Boolean (or why). I did some debugging to
A> ensure $mystring is a valid text value, and it seems to be.
the above will not evaluate to 1. your code is not the same as that line
of code. wanna bet you don't have () around your $match var?
A> We do have some of the O'Reilly books in the office but not that one. Thank>> I've heard that "Mastering regular expression" is very good.
A> you, I'll check it out.
this is covered in perlre, perlretut and perlrequick. you need to learn
how regexes work in different contexts.
uri
--
Uri Guttman ------ [email]uri@stemsystems.com[/email] -------- [url]http://www.stemsystems.com[/url]
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url]
Damian Conway Class in Boston - Sept 2003 -- [url]http://www.stemsystems.com/class[/url]
Uri Guttman Guest
-
Uri Guttman #11
Re: "between" function equivalent in Perl?
>>>>> "A" == Alexandra <sagittaur@ftml.net> writes:
A> This also evaluated to 1. I had tried this statement with and without the>> Assuming there is only one occurrence per string, and that there are no
>> newlines in the string, this works.
>> my $matched = $mystring =~ m/=(.*?)\s/;
A> leading 'm' and came up with the same result. I'm wondering if the
A> expression is evaluating at all or if Perl is just seeing it as a true or
A> valid expression. When I do type in something incorrect I do get a Server
A> Error. So the expression must be evaluating on some level. Any insight or
A> further suggestions would be appreciated.
and that line of code is wrong. see my other post.
uri
--
Uri Guttman ------ [email]uri@stemsystems.com[/email] -------- [url]http://www.stemsystems.com[/url]
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url]
Damian Conway Class in Boston - Sept 2003 -- [url]http://www.stemsystems.com/class[/url]
Uri Guttman Guest
-
Uri Guttman #12
Re: "between" function equivalent in Perl?
>>>>> "A" == Alexandra <sagittaur@ftml.net> writes:
A> If only it would... <g>. For some reason, I'm receiving a 1 as the return>> middle: /from(.*?)$to/. In your case it doesn't matter:
>>
>> my $from = qr/=/; # a "="
>> my $to = qr/\s/; # any white space
>> my ( $between) = $mystring =~ /$from(.*?)$to/;
>> print "$between\n";
>>
>> That prints "xxxx".
A> value. I did some debugging to ensure the $to and $from values are correct
A> and that the string is a valid string value. I also tried using other valid
A> character values in the $to and $from fields to see if the '=' was the
A> culprit and still the expression evaluated to 1.
SHOW YOUR CODE. saying it doesn't work without showing your code is a
waste of everyone's time. the above code is fine. obviously yours is not
but we can't fix it without seeing it.
uri
--
Uri Guttman ------ [email]uri@stemsystems.com[/email] -------- [url]http://www.stemsystems.com[/url]
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url]
Damian Conway Class in Boston - Sept 2003 -- [url]http://www.stemsystems.com/class[/url]
Uri Guttman Guest
-
Uri Guttman #13
Re: "between" function equivalent in Perl?
>>>>> "A" == Alexandra <sagittaur@ftml.net> writes:
A> Thanks for your reply. For some reason this also evaluated to 1, instead of>> print $mystring =~ m/=([^\s]+)/;
A> the desired substring. And there is not a "1" character in the initial
A> string ($mystring). I tried placing brackets [] around the equal sign and
A> still no dice.
gack, you don't get context. print is returning the 1. or you are not
realizing print provides list context. this is a poor example since it
isn't clear why the grabbed string is printed.
A> The expression seems, somehow, to be evaluating for a truth value. (??)
again, SHOW YOUR CODE.
uri
--
Uri Guttman ------ [email]uri@stemsystems.com[/email] -------- [url]http://www.stemsystems.com[/url]
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url]
Damian Conway Class in Boston - Sept 2003 -- [url]http://www.stemsystems.com/class[/url]
Uri Guttman Guest
-
Dave Saville #14
Re: "between" function equivalent in Perl?
I just cut n pasted the code from previous posts
use strict;
use warnings;
my $mystring = 'aaa @ bbbb @ c @ dd @ eeeeee @ FFFFF=xxxx @ ggg @ h';
my $from = qr/=/; # a "="
my $to = qr/\s/; # any white space
my ( $between) = $mystring =~ /$from(.*?)$to/;
print "$between\n";
Prints xxxx here. Perl 5.8.0 on OS/2. Something odd your end I feel.
Regards
Dave Saville
NB switch saville for nospam in address
Dave Saville Guest
-
Alexandra #15
Re: "between" function equivalent in Perl?
"Uri Guttman" wrote:
return>> >>>>> "A" == Alexandra <sagittaur@ftml.net> writes:>> >> my($match) = $mystring =~ /=(\S+)/;
> A> Thanks for your reply. Unfortunately, I only received a "1" as theif> A> value, as I also did with some of the other suggestions. I'm not sureto> A> this means it's evaluating to Boolean (or why). I did some debuggingYes! That was it, thank you. All of the following 'mysubstr#' expressions> A> ensure $mystring is a valid text value, and it seems to be.
>
> the above will not evaluate to 1. your code is not the same as that line
> of code. wanna bet you don't have () around your $match var?
now work perfectly well. (My apologies to original posters for not
understanding this aspect.)
$mystr = `grep $in{lookup_key} /AAA/bbbb/ccc/dd/eeee.ff`;
$in{mystr} = $mystr; #debug
$from = '=';
$in{myfrom} = $from; #debug
$to = ' ';
$in{myto} = $to; #debug
($in{mysubstr0}) = $mystr =~ /=(\S+)/;
($in{mysubstr1}) = $mystr =~ /=(.*?)\s/;
($in{mysubstr2}) = $mystr =~ m/[=]([^\s]+)/;
($in{mysubstr3}) = $mystr =~ /$from(.*?)$to/;
I found that most all of the regular expression examples in the Programming
Perl book use shorthand of referring to $_ (thus, not providing an
explicitly named variable at all). The examples that do refer to a named
variable have something like "($foo = $bar) =~ s/this/that;". So, I'd
assumed the parentheses were only needed when using mulitple explicitly
named variables. Good for non-beginners, otherwise difficult to decipher.
Thank>> >> I've heard that "Mastering regular expression" is very good.
> A> We do have some of the O'Reilly books in the office but not that one.Great, I found them on perldoc.com and will read them.> A> you, I'll check it out.
>
> this is covered in perlre, perlretut and perlrequick. you need to learn
> how regexes work in different contexts.
Btw, the only reference to contexts in Programming Perl focused on lists and
scalar contexts. Nowhere did I see mention of enclosing an assignment
variable in parentheses or why. Though I did find this quote: "You will be
miserable until you learn the difference between scalar and list context,
because certain operators know which context they are in, and return lists
in contexts wanting a list, and scalar values in contexts wanting a scalar."
It's all so clear now...
Anyway, thanks for replying and for the fix.
Alexandra
Alexandra Guest
-
Alexandra #16
Re: "between" function equivalent in Perl?
"Dave Saville" wrote:
Yes, thank you for taking the time. It was indeed the lack of () on my end.> I just cut n pasted the code from previous posts
>
> use strict;
> use warnings;
> my $mystring = 'aaa @ bbbb @ c @ dd @ eeeeee @ FFFFF=xxxx @ ggg @ h';
> my $from = qr/=/; # a "="
> my $to = qr/\s/; # any white space
> my ( $between) = $mystring =~ /$from(.*?)$to/;
> print "$between\n";
>
>
> Prints xxxx here. Perl 5.8.0 on OS/2. Something odd your end I feel.
All of the initial suggestions worked once I added them:
($in{login0}) = $user_rec =~ /=(\S+)/;
($in{login1}) = $user_rec =~ /=(.*?)\s/;
($in{login2}) = $user_rec =~ m/[=]([^\s]+)/;
($in{login3}) = $user_rec =~ /$from(.*?)$to/;
Alexandra
Alexandra Guest
-
Brian Wakem #17
Re: "between" function equivalent in Perl?
"Alexandra" <sagittaur@ftml.net> wrote in message
news:bjnknd$rk7$1@lumberjack.rand.org...the>
> "Brian Wakem" wrote:
>>> > "Alexandra" wrote:> > > this. Here's what I'm trying to do:
> > >
> > > $mystring = 'aaa @ bbbb @ c @ dd @ eeeeee @ FFFFF=xxxx @ ggg @ h'
> > >
> > > The goal is to extract the 'xxxx' substring between the '=' sign andregular> anyone> > > next whitespace character. (There is no fixed length for the 'xxxx'
> > > substring.)
> > >
> > > Rather than use a series of clunky index and substring calls, does> > > have a better suggestion? If anyone can recommend a good Perl language
> > > reference website (or book) that has some excellent examples ofof>> >> > > expressions, that would be helpful too.
> >
> > print $mystring =~ m/=([^\s]+)/;
> Thanks for your reply. For some reason this also evaluated to 1, instead> the desired substring. And there is not a "1" character in the initial
> string ($mystring). I tried placing brackets [] around the equal sign and
> still no dice.
>
> The expression seems, somehow, to be evaluating for a truth value. (??)
All of the examples given in the replies to your original message work
absolutely fine. You are doing something you aren't telling us. Post your
code.
--
Brian Wakem
Brian Wakem Guest
-
Alexandra #18
Re: "between" function equivalent in Perl?
"Brian Wakem" wrote:
> "Alexandra" wrote:your> All of the examples given in the replies to your original message work
> absolutely fine. You are doing something you aren't telling us. PostYes, they do work (see other replies). It was my error with lack of> code.
parentheses around the assignment var. Next time I'll include my code in
initial replies.
Regards,
Alexandra
Alexandra Guest
-
Tad McClellan #19
Re: "between" function equivalent in Perl?
Alexandra <sagittaur@ftml.net> wrote:
> Btw, the only reference to contexts in Programming Perl focused on lists and
> scalar contexts.
That is what we are focusing on here as well!
You needed a m// in list context to get what you wanted.
> Nowhere did I see mention of enclosing an assignment
> variable in parentheses or why.
From p69 in the 3rd Camel:
Assignment to a list of scalars also provides list context
to the righthand side, even if there's only one element
in the list.
($foo) = ...list context...
has only one element in the list.
--
Tad McClellan SGML consulting
[email]tadmc@augustmail.com[/email] Perl programming
Fort Worth, Texas
Tad McClellan Guest
-
John W. Krahn #20
Re: "between" function equivalent in Perl?
Alexandra wrote:
Instead of running an external program for this you can do it in perl>
> "Uri Guttman" wrote:
>>> >> > >>>>> "A" == Alexandra <sagittaur@ftml.net> writes:> >> > >> my($match) = $mystring =~ /=(\S+)/;
> > A> Thanks for your reply. Unfortunately, I only received a "1" as the return
> > A> value, as I also did with some of the other suggestions. I'm not sure if
> > A> this means it's evaluating to Boolean (or why). I did some debugging to
> > A> ensure $mystring is a valid text value, and it seems to be.
> >
> > the above will not evaluate to 1. your code is not the same as that line
> > of code. wanna bet you don't have () around your $match var?
> Yes! That was it, thank you. All of the following 'mysubstr#' expressions
> now work perfectly well. (My apologies to original posters for not
> understanding this aspect.)
>
> $mystr = `grep $in{lookup_key} /AAA/bbbb/ccc/dd/eeee.ff`;
and have better control of error reporting:
my $file = '/AAA/bbbb/ccc/dd/eeee.ff';
open my $fh, '<', $file or die "Cannot open $file: $!"
my $mystr = join '', grep $in{lookup_key}, <$fh>;
close $fh;
Whenever you see "/regex/" or "$var = /regex/" you can expand them to> $in{mystr} = $mystr; #debug
> $from = '=';
> $in{myfrom} = $from; #debug
> $to = ' ';
> $in{myto} = $to; #debug
>
> ($in{mysubstr0}) = $mystr =~ /=(\S+)/;
> ($in{mysubstr1}) = $mystr =~ /=(.*?)\s/;
> ($in{mysubstr2}) = $mystr =~ m/[=]([^\s]+)/;
> ($in{mysubstr3}) = $mystr =~ /$from(.*?)$to/;
>
> I found that most all of the regular expression examples in the Programming
> Perl book use shorthand of referring to $_ (thus, not providing an
> explicitly named variable at all).
"$_ =~ /regex/" and "$var = $_ =~ /regex/" then the "$_ =~" part can be
replaced with whatever scalar variable you want.
Because the binding operator (=~) has higher precedence then the> The examples that do refer to a named
> variable have something like "($foo = $bar) =~ s/this/that;".
assignment operator (=) the parenthesis are required to assign the
contents of $bar to $foo before the substitution is performed on the
result so that $foo is changed and $bar is not. Without the parenthesis
the substitution would be performed on $bar first and the result of that
(true or false) would be assigned to $foo.
Is that irony?> So, I'd
> assumed the parentheses were only needed when using mulitple explicitly
> named variables. Good for non-beginners, otherwise difficult to decipher.
>>> >> > >> I've heard that "Mastering regular expression" is very good.
> > A> We do have some of the O'Reilly books in the office but not that one. Thank
> > A> you, I'll check it out.
> >
> > this is covered in perlre, perlretut and perlrequick. you need to learn
> > how regexes work in different contexts.
> Great, I found them on perldoc.com and will read them.
>
> Btw, the only reference to contexts in Programming Perl focused on lists and
> scalar contexts. Nowhere did I see mention of enclosing an assignment
> variable in parentheses or why. Though I did find this quote: "You will be
> miserable until you learn the difference between scalar and list context,
> because certain operators know which context they are in, and return lists
> in contexts wanting a list, and scalar values in contexts wanting a scalar."
>
> It's all so clear now...
perlsub.pod explains a bit about the difference between scalar and list
context.
perldoc perlsub
John
--
use Perl;
program
fulfillment
John W. Krahn Guest



Reply With Quote

