Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: intermix list of items

by pg (Canon)
on Oct 10, 2004 at 17:09 UTC ( #398011=note: print w/replies, xml ) Need Help??


in reply to intermix list of items

That eq definitely does not make sense. I can guess what was in your mind, you probably thought that by using 'eq', sort will group elements for you base on how common they are. No, that is not what sort does. You have to tell sort "between each pair of two elements, how they shall be ordered".

You are looking for something like:

use Data::Dumper; my @list = ('10.1.1.1', '10.1.1.2', '10.2.2.1', '10.2.2.2'); @list = sort intermix @list; print Dumper(@list); sub intermix { return ( substr( $a, 0, rindex($a, '.')) gt substr( $b, 0, rindex($b, '.')) ) ? 1 : 0; }

Replies are listed 'Best First'.
Re^2: intermix list of items
by tstock (Curate) on Oct 10, 2004 at 17:20 UTC
    'gt' refers to the size of the string, running your example solution didn't seem to help. I was going for (and failed) non-equality of the substring means they are identical in sort order by using 'eq'.
      "'gt' refers to the size of the string"

      No. In your code, gt looks at "dictionary sequence".

      If string size/length is what you wanted, say length($str) expliciltly.

      use Data::Dumper; my @foo = ("abc", "bc"); print Dumper(sort bar1 @foo); print Dumper(sort bar2 @foo); sub bar1 { return $a gt $b; } sub bar2 { return length($a) gt length($b); }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://398011]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2021-04-15 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?