Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How to avoid the duplication in a string

by soubalaji (Sexton)
on Oct 22, 2008 at 10:56 UTC ( [id://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

Replies are listed 'Best First'.
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 jwkrahn (Abbot) on Oct 22, 2008 at 12:10 UTC
    $ perl -le' $_ = "aabbccdddd"; print; tr///cs; print; ' aabbccdddd abcd
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 (Archbishop) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-24 09:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found