On 01/31/2011 01:50 AM, John Sampson wrote:
> m// is written with a regexp between the slashes and returns true (1) if
> the regexp matches the string in
> an internal variable. Or if one writes "$a =~ m/foo/" where $a is a
> string variable then if $a contains "foo"
> and "foo" is a regexp the statement will return 1.
>
> s/// is a substitution operator which works similarly but substitutes
> the string between the second and third
> slash for the regexp between the first and second slashes.
>
> This is an oversimplification of what they do because they have
> modifiers etc. but I assume there is a way
> of matching or substituting regexps in Unicon.
(Thanks for the explanations - that helps!)
There probably isn't a similar mechanism for the
regular expression support found in the Icon Programming Language
that comes with Unicon (I haven't looked, but that's a pretty old
RE package - you might take a look at it to see if that's true
or not!).
In Unicon (and Icon), string scanning is typically used in most
places where another language might use REs, and the two approaches
are fundamentally different. A RE is, in effect, a separate language
for describing sets of strings, so a match against a RE is a test to
see if the target string is a member of that set or not.
String scanning is a procedural description of how to move through
a string looking for features and you can perform any action supported
by the language at any time during the scan. The same goal-directed
evaluation found throughout Unicon/Icon is applied during scanning, so
you get the same success/failure and backtracking you get elsewhere.
This dichotomy makes difficult to translate actions in one approach
into the other, in Unicon you (the rhetorical 'you') don't think of
describing a pattern to apply to a string, you think of how to
move through the string locating items of interest.
So, for example perl's m/RE/ roughly translates to 'tell me if
any string described by RE appears in the target'. Without knowing
what the specific RE is, this is difficult to express in Unicon.
On the other hand, building a parser is probably considerably
easier using string scanning than expressing in an RE.
My recommendation is that you (rhetorically, again) not try to approach
string scanning by trying to map RE's into scanning expressions, but to
ask what a specific RE is trying to do and then trying to think of a way
to accomplish the same specific task using scanning *without* considering
how that task was done using an RE. There are lots of us here who
are happy to give advice on how a specific task might be approached.
For example, if the task had been to determine if there are any
sequences of two or more consecutive vowels in a target, that can be
done with (Danger, totally untested code coming!):
v := 'aeiou'
target ? {
if tab(upto(v)) && (*tab(many(v)) > 1) then {....}
}
Though of course you can write a procedure to help if this is commonly needed:
procedure sequence(c) # Find sequences of characters
suspend (tab(upto(c), tab(many(c)))
end
and then rewrite the above as:
target ? {
if *sequence('aeiou') > 1 then {...}
}
As for s///, the same generalities apply. Also, there's no concept of
replacing parts of a target string (this is true with s/// in perl as well,
but it's not quite so obvious there), instead, as you scan a string
you can use the information you learn during the scan to construct
a new string. For example, to produce a copy of a target string with all
sequences of two or more vowels replaced by '@', you could:
v := 'aeiou'
newS := ""
target ? {
while newS ||:= tab(upto(v)) do {
if tab(many(v)) > 1 then newS ||:= "@" # multi-vowel sequence
else newS ||:= move(1) # single vowel
}
newS := tab(0)
}
With perl's s///, the new string is constructed implicitly, here it's
done explicitly. (Hide it in a procedure if you want it to 'look'
implicit!)
Again, a procedure could be written to avoid code duplication if
this type of operation is needed frequently, and there are lots of
other ways to do this task.
--
Steve Wampler -- [email protected]
The gods that smiled on your birth are now laughing out loud.
------------------------------------------------------------------------------
Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)!
Finally, a world-class log management solution at an even better price-free!
Download using promo code Free_Logger_4_Dev2Dev. Offer expires
February 28th, so secure your free ArcSight Logger TODAY!
http://p.sf.net/sfu/arcsight-sfd2d
_______________________________________________
Unicon-group mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/unicon-group