in reply to Re: (Golf) Anagram Finder
in thread (Golf) Anagram Finder

I like this method but why do you think this works (also 59):

@dict = qw (foo oof fff bar baz); $letters = 'ofo'; print anagram($letters, \@dict); sub anagram { grep{(join'',sort split//,$_[0])eq join'',sort/./g}@{pop()} }

But this does not

grep{(join'',sort split//,$_[0])eq join'',sort/./g}@{pop}

Why do you need the parens to make pop() work?

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: Re: (Golf) Anagram Finder
by chipmunk (Parson) on Aug 04, 2001 at 18:34 UTC
    @{pop} doesn't work as you intended, because it refers to the plain old array @pop. The parentheses in @{pop()} make it into an expression, which is evaluated.

    However, you could save a character by using a plus instead, as in @{+pop}.

    Update: But, if you wanted to save even more characters... :)

    sub anagram { grep"@{[sort$_[0]=~/./g]}"eq"@{[sort/./g]}",@{+pop} }
    51 characters.
      The @{[func]} method for inserting the result of an arbitrary function call into a string is amazing. Someone once asked if they could put subroutine calls into a double-quoted string and have them expand properly, but I don't think anyone gave a satisfactory answer.

      I had no idea that the double-quoted string parser was so robust.
      If you lift the requirement that you have to compare one word to many, you can get the following (which returns whether its two arguments are anagramic)
      sub anagram { @_=map{join"",sort/./g}@_;pop eq pop }
      36 characters.