Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
please use real <code> tags and not &lt;code&gt;

looks like you want the symmetric difference of two character sets

DB<111> @a1=split//,$str1 => ("A", "B", "C", "D", "E") DB<112> @a2=split//,$str2 => ("B", "C", "D", "E", "O") DB<113> @h2{@a2}=() DB<114> @h1{@a1}=() DB<115> delete @h2{@a1} DB<116> delete @h1{@a2} DB<117> (keys %h1, keys %h2) => ("A", "O")

see Using hashes for set operations... for background.

UPDATE:

seems like nobody mentioned that its a FAQ How do I compute the difference of two arrays? How do I compute the intersection of two arrays?

UPDATE:

Just in case you only want the unique characters in both strings:

DB<137> $h{$_}++ for (split//,$str1),(split//,$str2) => "" DB<138> \%h => { A => 1, B => 2, C => 2, D => 2, E => 2, O => 1 } DB<139> grep {$h{$_}==1} keys %h => ("A", "O")

oops basically already shown by Not_a_Number

EDIT: one liner =)

DB<146> grep {$h{$_}==1} map {$h{$_}++;$_} split//,$str1.$str2 => ("A", "O")

Cheers Rolf


In reply to Re: compare two strings and return only he unique values. by LanX
in thread compare two strings and return only he unique values. by Learnlot1

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-25 12:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found