maps and regexs 
Author Message
 maps and regexs

Hi,

I have a string as such: "thisismystring" and want to analyse the letters
in sets of threes. I'm also learning more about maps and regexs too at the
moment, and wanted to do this using either of the two.

I want to analyse the first three characters, then shift one char forward,
and repeat, so that i process "thi", "his", "ism", "smy", etc.,

The question I am asking essentially is: when using a map to process an
array, is there a way of accessing other elements other than the current
one? I'd like to convert my string into an array, and calculate my new
element based upon the sets of three chars (as shown above)

Alternatively, if I was to use a regex in a substitution like:

$string =~ s/(\w)(\w)(\w)/... some calculation to modify $2/ge

..but with the sets of three previously shown, instead of the groups "thi",
"sis", "mys", etc., (which I can do ok)

Sorry if this doesn't make sense, I'm just looking for a pointer really as
to how I can go about this using either a map, or a regex. I know how to
write code to do this without them, it's more of a learning experience in
using maps, or regexs.

Cheers,

Tony

--------------------------------------
web:   http://www.*-*-*.com/
email: mercutio at digitalrice dot com
--------------------------------------



Thu, 22 Sep 2005 18:52:41 GMT  
 maps and regexs

Quote:

> I have a string as such: "thisismystring" [...]  I want to analyse
> the first three characters, then shift one char forward, and repeat,
> so that i process "thi", "his", "ism", "smy", etc.,

> [...] when using a map to process an array, is there a way of
> accessing other elements other than the current one? I'd like to
> convert my string into an array, and calculate my new element based
> upon the sets of three chars (as shown above)

The map() function processes a *list* not an array.  And there's no
way to look at the previous or next element of that list -- but you
can use map() on a list of array indexes, e.g.

    $ perl -l



    ^D
    t h i
    h i s
    s i m
    s m y
    ...

Quote:

> Alternatively, if I was to use a regex in a substitution like:

> $string =~ s/(\w)(\w)(\w)/... some calculation to modify $2/ge

> ..but with the sets of three previously shown, instead of the
> groups "thi", "sis", "mys", etc., (which I can do ok)

You can do something like that with pos() and m//g

    $ perl -l
    $_ = "thisismystring";
    pos() -= 2, print $1 while m/(\w{3})/g
    ^D
    thi
    his
    sim
    smy
    ...

There are several reasons why this approach won't work with s///g,
though.  What are you actually trying to do?

--
Steve



Thu, 22 Sep 2005 19:24:39 GMT  
 maps and regexs

Quote:

> I have a string as such: "thisismystring" and want to analyse the letters
> in sets of threes. I'm also learning more about maps and regexs too at the
> moment, and wanted to do this using either of the two.
> I'm just looking for a pointer really as
> to how I can go about this using either a map, or a regex.

   foreach my $str3 ( map substr($string, $_, 3), 0..length($string)-3 ) {

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Thu, 22 Sep 2005 21:32:38 GMT  
 maps and regexs


Quote:

>> I have a string as such: "thisismystring" and want to analyse the letters
>> in sets of threes. I'm also learning more about maps and regexs too at the
>> moment, and wanted to do this using either of the two.

>> I'm just looking for a pointer really as
>> to how I can go about this using either a map, or a regex.

>   foreach my $str3 ( map substr($string, $_, 3), 0..length($string)-3 ) {

length($string)-2, actually.

--
Eric Amick
Columbia, MD



Thu, 22 Sep 2005 22:28:45 GMT  
 maps and regexs

Quote:

> > I have a string as such: "thisismystring" and want to analyse the letters
> > in sets of threes. I'm also learning more about maps and regexs too at the
> > moment, and wanted to do this using either of the two.

> > I'm just looking for a pointer really as
> > to how I can go about this using either a map, or a regex.

>    foreach my $str3 ( map substr($string, $_, 3), 0..length($string)-3 ) {

...and here is one using a regex:

    foreach my $str3 ( $string =~ /(?=(...))/g ) {

Anno



Fri, 23 Sep 2005 10:26:33 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. mapping dots to a map image

2. serverside map 2 clientside map

3. Issues with interpolating regexs

4. telnet chars and chat regexs

5. Index and Regexs

6. Matching many similar regexs

7. RegExs and question marks...a possible bug?

8. Compiling regexs for efficency

9. difference in execution time of regexs

10. perl (?) browser for reading comp.mail.maps map information

11. Permute with "map"

12. Map Syntax Error

 

 
Powered by phpBB® Forum Software