in reply to Re: Collapsing a string to unique characters
in thread Collapsing a string to unique characters

That is too nice, but you will have to explain how it works.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

  • Comment on Re^2: Collapsing a string to unique characters

Replies are listed 'Best First'.
Re^3: Collapsing a string to unique characters
by JavaFan (Canon) on Jan 12, 2009 at 14:42 UTC
    -nE'@;{/./g}=();%;=!say%;'

    /./g is in list context, so it's a shorthand for /(.)/g and will hence return a list of all characters (without the newline).

    @;{...} is a slice of the hash %;. @;{/./g} = () sets all values in the slice to undef. The keys are the characters of the line.

    say %; prints the hash; as key-value pairs. Since the values are undefined, the values are printed as empty strings. So, in effect, it prints all the characters of the line, without duplicates.

    %;=!say%; say will return true, so its negation will be the empty string. So it'll make %; have one element: the empty string as key, and the undefined value as value. This will be printed for the next line, but since they are both printed as empty strings, you won't actually see it.

      Thanks! The fact that you can call a variable ';' threw me off.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James