Anonymous Monk,
The short answer is yes. The long answer is yes, but which simple line of code all depends on what you mean. That is why it is important not to leave ambiguity in your questions. It is frustrating to have people answer questions you did not ask.
- Do you want to reduce only letters appearing twice that are together?
- Do you want to reduce letters appearing twice that appear anywhere in the string?
- Do you only want to perform the action if the letters appear exactly twice, but not greater than twice?
So as to not leave you with all questions and no answers. Take a look at this small snippet, it should help if what you are going for is option 3. It is intentionally "wordy" so that the process is more obvious.
#!/usr/bin/perl
use strict;
use warnings;
my $string = 'one bright day in the middle of the night';
my (%seen, $new_string);
for ( split // , $string ) {
next if $seen{$_};
$seen{$_}++;
$new_string .= $_;
}
print "$new_string\n";
Cheers -
L~R
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.