Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > using the result of a variable regular expression

Reply
Thread Tools

using the result of a variable regular expression

 
 
Brian McCauley
Guest
Posts: n/a
 
      08-27-2004

Following up in the wrong part of this thread Sara wrote:
> http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in message news:<cgl2im$(E-Mail Removed)>...
>
>>if ($numb == 1) {
>>print $1;
>>} elsif ($numb == 2) {
>> print $2;
>>} elsif ($numb == 3) {
>> print $3;
>>}

>
> Interesting question. As pointed out, $$numb will work nicely for you.


For certain values of "nice".

> The odd thing being that this LOOKS like a scalar dereference, which
> it really isn't


Yes it is. It's a scalar dereference of a _symbolic_ reference.

> since 2 isn't the memory location of the value.


If it were a _hard_ scalar reference then its numeric value would be the
address in memory.

> Seems like there is an ambiguity in there somewhere but I can't pinpoint it.


No ambiguity. Perl's scalar values can contain either ordinary
strings/numbers or they can contain hard references. It is possible to
convert a hard reference[1] into an address in memory simply by using it
in a numeric context. It is not possible to go the other way[4]. If
you use a non-reference in a reference context then it will never be
treated as a memory address - it will be converted into a string and
looked up in the symbol table - i.e. it will be a symbolic reference.
Of course most of the time one has "strict qw(refs)" in effect which
causes symbolic references to be diallowed except in a few special
cases[2].

[1] (other than one to an overloaded type object)
[2] To do with symbolic CODErefs[3].
[3] And due to a bug any symrefs resolved at compile-time.
[4] In Perl - you can of course do anything you want by dropping down
into C.

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      08-28-2004


bowsayge wrote:

> Brian McCauley said to us:
>
>
>>>Interesting question. As pointed out, $$numb will work nicely for you.

>>
>>For certain values of "nice".
>>

>
> [...]
>
> Bowsayge hopes that this is a better value of "nice":
>
> '8 t 4' =~ /(\d) (\w) (\d)/;
> my $numb = 3;
> print "matched: ", eval("\$$numb"), "\n";
>
> There is no need to enable sym-refs.


All the reasons to avoid symrefs bad also apply to eval(STRING), only
more so.

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      08-28-2004
bowsayge <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Brian McCauley said to us:
>
> >> Interesting question. As pointed out, $$numb will work nicely for you.

> >
> > For certain values of "nice".
> >

> [...]
>
> Bowsayge hopes that this is a better value of "nice":


Not really.

> '8 t 4' =~ /(\d) (\w) (\d)/;
> my $numb = 3;
> print "matched: ", eval("\$$numb"), "\n";
>
> There is no need to enable sym-refs.


Sure. You can re-write any symref unsing eval like that, so string
eval is the more general mechanism. It also allows Perl to break its own
rules in more ways than mere symrefs do, so it's higher in the hierarchy
of nastiness, not lower.

It is also ugly because it's disproportionate, in the way it would be
ugly to start a sawmill to make a toothpick from a twig. You are running
another Perl interpreter to interpret a program that reads "$1" or "$5" or
something.

That said, your solution is, of course, perfectly valid. The symref
solution needs to unexpectedly talk about "strict", and may need a
bare block to limit the effect. So "eval" is shorter and more to the
point, and it's arguably as readable. Since the string you eval is
entirely defined in the program text (as opposed to an external source),
there is no additional risk in "eval".

But "nicer", no.

Anno
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      08-31-2004

Quoth Brian McCauley <(E-Mail Removed)>:
> It is possible to
> convert a hard reference[1] into an address in memory simply by using it
> in a numeric context. It is not possible to go the other way[4]. If


[1] NMF

> [4] In Perl - you can of course do anything you want by dropping down
> into C.


You don't need C: unpack 'P' will work nicely...

Ben

--
Although few may originate a policy, we are all able to judge it.
- Pericles of Athens, c.430 B.C.
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
need regular expression to replace part of result based on a search pattern Jimmy Java 13 07-25-2012 09:37 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds Michael Tan Ruby 32 07-21-2005 03:23 PM
Set a variable from a substring of another variable using Regular Expression Tony Perl Misc 2 04-20-2005 11:57 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments