Doubling up a character in a string 
Author Message
 Doubling up a character in a string

Solved my own problem:

sub double_up {



    my $len = length($string);
    my $new_str;

    for (my $i = 0; $i < $len; $i++) {

        my $char = substr($string, $i, 1);

        if ($char eq $dbl_char) {

            $new_str .= $char . $char;

        } else {

            $new_str .= $char;

        }

    }

    return $new_str;

Quote:
}


Quote:

>I am looking for a generic routine to which I can pass a character
>as an argument and have the routine search the string for that
>character, and then double it, i.e I have a string such as:

>"Bob's wife is married to Mike's cousin."

>When the routine is done I would like it to return:

>"Bob''s wife is married to Mike''s cousin."

>provided that I pass it a ' as an argument.  I was looking into using
>substitution with s//, but I heard that will only give me back:

>"Bob's wife is married to Mike''s cousin."

>because it performs greedy matching.

>Can anyone help me out on this and/or point me in the right direction
>to resolving it myself?

>Thanks in advance,

>Sal Sferrazza



Fri, 25 Feb 2000 03:00:00 GMT  
 Doubling up a character in a string

I am looking for a generic routine to which I can pass a character
as an argument and have the routine search the string for that
character, and then double it, i.e I have a string such as:

"Bob's wife is married to Mike's cousin."

When the routine is done I would like it to return:

"Bob''s wife is married to Mike''s cousin."

provided that I pass it a ' as an argument.  I was looking into using
substitution with s//, but I heard that will only give me back:

"Bob's wife is married to Mike''s cousin."

because it performs greedy matching.

Can anyone help me out on this and/or point me in the right direction
to resolving it myself?

Thanks in advance,

Sal Sferrazza



Fri, 25 Feb 2000 03:00:00 GMT  
 Doubling up a character in a string

Not sure why you can't do something like

sub Double ($$) {

    $ch = quotemeta($ch);
    $str =~ s/$ch/$ch$ch/g;
    return $str;

Quote:
}

In other words, use the -g- option on s///.


Quote:
>I am looking for a generic routine to which I can pass a character
>as an argument and have the routine search the string for that
>character, and then double it, i.e I have a string such as:

>"Bob's wife is married to Mike's cousin."

>When the routine is done I would like it to return:

>"Bob''s wife is married to Mike''s cousin."

>provided that I pass it a ' as an argument.  I was looking into using
>substitution with s//, but I heard that will only give me back:



Fri, 25 Feb 2000 03:00:00 GMT  
 Doubling up a character in a string


Quote:

> I am looking for a generic routine to which I can pass a character
> as an argument and have the routine search the string for that
> character, and then double it, i.e I have a string such as:
> provided that I pass it a ' as an argument.  I was looking into using
> substitution with s//, but I heard that will only give me back:

> "Bob's wife is married to Mike''s cousin."

> because it performs greedy matching.

I don't think the greedy matching matters here. This should work:

sub double_double
{

        $s =~ s/$c/$c$c/g;
        $s;                     # return the modified string

Quote:
}

---

#!perl is at ftp://.../CPAN/ports/msdos/tips-tricks/hbp_403.zip, Perl for
NT/Win 95 can be found at http://www.activeware.com, with an excellent FAQ
file at http://www.endcontsw.com/people/evangelo/Perl_for_Win32_FAQ.html
and no, I don't have the slightest idea what's wrong with your CGI script.
Try http://www.perl.com/perl/faq/idiots-guide.html


Fri, 25 Feb 2000 03:00:00 GMT  
 Doubling up a character in a string


Quote:

>provided that I pass it a ' as an argument.  I was looking into using
>substitution with s//, but I heard that will only give me back:

>"Bob's wife is married to Mike''s cousin."

>because it performs greedy matching.

Well s/// does do greedy matching if you ask it to, but since you only
want one character, you don't have to worry about it:

Try this:

$a =~ s/($char)/$1$1/g;

Or, you could do something like this:

while (($pos = index($a, $char, $pos)) >= 0) {
  substr($a, $pos++, 0) = $char;
  ++$pos;  ##To get beyond this character and avoid the infinite loop.

Quote:
}

Now you can argue the merits of substr vs s/// for speed and overhead.
Since s/$anyvar// has to be continuously recompiled each time it's run,
it should be considered slower, but from a code readability point of
view, it is a lot clearer.  Of course, like all good programmers, you
document you code thoroughly :) so it really doesn't matter (and so on
and so on...)

No matter which approach you choose, it sounds like you need to learn
more about regular expressions.  You could try Jeff Friedl's book, or
the Camel, or the Llama, or you could read the perl man pages.

To meta-quote Randal Schwartz:
"Use regular expressions.  Regular expressions are your friend"

Colin Kuskie



Fri, 25 Feb 2000 03:00:00 GMT  
 Doubling up a character in a string

Quote:

> sub double_up {
 <whew!!!>
>     return $new_str;
> }

> >provided that I pass it a ' as an argument.  I was looking into using
> >substitution with s//, but I heard that will only give me back:

It work's for me (you shouldn't listen to hearsay).

#!/usr/bin/perl -w

$_ = "Bob's wife is married to Mike's cousin.\n";
print;
s/'/''/g;
print;

--



Fri, 25 Feb 2000 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Evaluation of hex character representation in double quoted strings

2. changing a single quote string to a double quote string

3. Converting single quoted string to double quoted string

4. perl for double-byte characters

5. Search and Replacing a double-quote character

6. Help please : getting rid of double CR control characters

7. how to display double byte characters.

8. split a string character by character

9. What is the best methode to parse a string character by character

10. ${...} and @{...} in double-quoted string

11. How do you double-quote a string?

12. NEWBIE: double sort on a string

 

 
Powered by phpBB® Forum Software