Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

It still doesn't make any sense. I see now though that f is actually getting called in both cases (or would be if there is more than one element). In the runs that return 0, it's called before the sort. In the runs that return 1, it's called as the sorting subroutine. The fact that whitespace indicates precendence really bugs me though. I feel like it should always return one or the other (probaby 1), and that you should be forced to call the subroutine with &.

To get closer to my original code:

#!/usr/bin/perl use strict; use warnings; my @list = (1, 2, 1); print join ',', sort (unique (@list)); print join ',', sort (unique(@list)); sub unique { my %new = (); return grep (!$new{$_}++, @_); }
Returns:
Chromium:~ lexicon$ perl -l little2.pl Unquoted string "unique" may clash with future reserved word at little +2.pl line 7. 1,2,1 1,2
With a similar deparse:
Chromium:~ lexicon$ perl -l -MO=Deparse little2.pl Unquoted string "unique" may clash with future reserved word at little +2.pl line 7. BEGIN { $/ = "\n"; $\ = "\n"; } use warnings; use strict 'refs'; my(@list) = (1, 2, 1); print join(',', (sort unique @list)); print join(',', sort(unique(@list))); sub unique { use warnings; use strict 'refs'; my %new; return grep((!$new{$_}++), @_); } little2.pl syntax OK

I supose here, in the unique as sort-on-subroutine case, unique always returns a two element list which is probably interpreted as 1 and so leaves the list in order. This ends up looking like the unique subroutine is completely ignored, which drove me nuts for a couple hours.


In reply to Re^2: Unexpected difference in whitespace parsing in subroutine calls. by Lexicon
in thread Unexpected difference in whitespace parsing in subroutine calls. by Lexicon

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 taking refuge in the Monastery: (2)
As of 2024-04-19 01:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found