Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How to avoid the duplication in a string

by soubalaji (Sexton)
on Oct 22, 2008 at 10:56 UTC ( #718695=perlquestion: print w/ replies, xml ) Need Help??
soubalaji has asked for the wisdom of the Perl Monks concerning the following question:

$a='aabbccdddd';
I want the Output like this
$a='abcd';
Any any help how to avoid the duplication in a string. Regards, Balaji S

Comment on How to avoid the duplication in a string
Re: How to avoid the duplication in a string
by ccn (Vicar) on Oct 22, 2008 at 11:00 UTC
      could you please explain what is the meaning for \1+ in perl
        could you please explain what is the meaning for \1+ in perl

        In regular expressions, backslashed numeric digits are references back to the preceding captures. So \1 refers to the same thing that was captured in the first set of parentheses, and \2 refers to the same thing that was captured in the second set, and so forth.

        As for the +, it modifies a backreference in exactly the same way it modifies any other atom in a regular expression.

        -- 
        We're working on a six-year set of freely redistributable Vacation Bible School materials.
Re: How to avoid the duplication in a string
by andreas1234567 (Vicar) on Oct 22, 2008 at 11:25 UTC
    Is this homework?
    $ perl -wle '%h = map { print unless defined($h{$_}); $_ => $h{$_}++ } + split(//,q{abcdabcdabcd}); ' a b c d
    --
    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]
Re: How to avoid the duplication in a string
by ww (Bishop) on Oct 22, 2008 at 12:07 UTC

    TIMTOWTDI:

    #!/usr/bin/perl -w use strict; =head - Per OP $a='aabbccdddd'; I want the Output like this $a='abcd'; =cut my $a = 'aabbccdddd'; my $prev = ''; my @a = split //,$a; for my $found(@a) { if ($prev eq $found) { next; } else { print "\$found is: $found \n"; #Omit \n if oneline output desi +red. $prev = $found; next; } }
    Output:
    ww@GIG:~/pl_test$ perl -c 718695.pl 718695.pl syntax OK ww@GIG:~/pl_test$ perl 718695.pl $found is: a $found is: b $found is: c $found is: d

    Note: This will fail on the likes of $a='aabbccadddd'

    ww@GIG:~/pl_test$ perl 718695.pl $found is: a $found is: b $found is: c $found is: a # dup $found is: d

    If the data is in this form, use a %hash as andreas1234567 does, above or sort the string.

Re: How to avoid the duplication in a string
by jwkrahn (Monsignor) on Oct 22, 2008 at 12:10 UTC
    $ perl -le' $_ = "aabbccdddd"; print; tr///cs; print; ' aabbccdddd abcd

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://718695]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (14)
As of 2014-07-25 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls