Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^2: Sort hash with values

by AnomalousMonk (Chancellor)
on Jun 24, 2013 at 17:53 UTC ( #1040471=note: print w/replies, xml ) Need Help??

in reply to Re: Sort hash with values
in thread Sort hash with values

# SPLIT THE IP-ADDRESSES INTO FOUR =STRINGS= BY # THE PERIOD CHARACTER. my ($a1, $a2, $a3, $a4) = split('.', @$a);

The split built-in function does not use single-quotes around a  /PATTERN/ argument to 'meta-quote' the argument: the  '.' in the above really is trying to split on "any character except newline". Use  '\.' to split on a literal period.

>perl -wMstrict -le "my $s = ''; ;; my @ra = split '.', $s; print 'naked dot'; printf qq{ '$_'} for @ra; print '@ra elements: ', scalar @ra; ;; @ra = split '\.', $s; print 'escaped dot'; printf qq{ '$_'} for @ra; " naked dot @ra elements: 0 escaped dot 'a' 'bb' 'ccc' 'd'

Replies are listed 'Best First'.
Re^3: Sort hash with values
by sundialsvc4 (Abbot) on Jun 24, 2013 at 18:55 UTC

    Excellent point.   Thank you.   I have updated the post accordingly.   But, even so, my post consisted of extemporaneous coding.   I did not separately test it to see if it would actually run.   I made it only as an illustration of the essential idea, “implementation left as an exercise to the reader.”

      ... extemporaneous coding ... “implementation left as an exercise to the reader.”

      I understand. But given my perception of the relative inexperience of Rahul Gupta WRT Perl coding, and of the relatively subtle nature of the bug and its manifestation, I wanted to be very explicit about the problen I saw.

      I did not separately test it to see if it would actually run.

      My own experience with the excellent advice I customarily dispense is that it always works — except when I don't test it, and then, for some reason, it never works! That's why I always try to provide a "complete input-execution-output experience", usually in the form of some kind of command-line capture. That way, there's no question about execution conditions, code, data, etc... Caveat praeceptor.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1040471]
[erix]: D.: yeah, I am in the low countries. To be precise, I am 0.5m above sea level
[erix]: going for a run in the dunes - unless the storm (well, Beaufort 7) gets fiercer
Discipulus had stew with tomato sauce, onions, carrots and potatoes + bottle of red wine and still warm bred.. slept as dynosaur

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (14)
As of 2017-11-22 09:14 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (316 votes). Check out past polls.