> Ok this code should delete all multiple chars in $secret.

This spec is a bit uncertain: do you want to eliminated sequentially repeated characters ('bookkeeper' -> 'bokeper') or keep only one instance of each character ('bookkeeper' -> 'bokepr')? Gathering from your first attempt, you want the second behavior.

As an exercise, I'll demonstrate both, since they're both easy with regular expressions. Though your hash approach makes sense and is likely more efficient, I like the elegance of regexen. As always, TMTOWTDI.

The first case is quite simple; just use:($newsecret = $secret) =~ tr/a-zA-Z//s;The /s flag instructs the transliteration operator to squash any repeated characters that are replaced. The empty replacement list causes the search list to be copied into the replacement list. So, the operator matches any character, replaces it with itself, and eliminates any sequential duplicates it comes along.

The second form takes a little bit more ninja magic, but I hope it's still understandable. After all, you're looking for concise, not pretty: for ( $newsecret = $secret; $newsecret =~ s/(([a-zA-Z]).*)\2/$1/; ){1};

You can't just use /g because the replacements should happen right to left. This is an ugly form of the 1 while (...) construct that sneaks the initial assignment into the line. If you don't mind modifying $secret in-place, you should use the cleaner form:1 while $secret =~ s/(([a-zA-Z]).*)\2/$1/;

The regexp finds any character followed by the same character later in the string and crops the latter. This is repeated until it no longer matches.

An important point is that I've assumed $secret contains only alphabetic characters. You'll need to change the matching character class according to the actual contents of your variable.

Though I doubt it applies to your case, if the final order of the string doesn't matter then you could use the hash method:

$newsecret = join '', keys %{{map {($_, 1)} split //, $secret}};


In reply to Re: get it into one line ? by athomason
in thread get it into one line ? by physi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.