Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

btw, the compare function is suppose to return -1, 0, +1, not just 0 and 1, so I expect your sort has some amount of randomness to it.

Maybe you want something like:

sub intermix { my $a_prime = join('.', reverse(split(/\./, $a))); my $b_prime = join('.', reverse(split(/\./, $b))); $a_prime cmp $b_prime } my @list = ('', '', '', ''); @list = sort intermix @list; use Data::Dumper; print Dumper(@list); __END__ output ====== $VAR1 = ''; $VAR2 = ''; $VAR3 = ''; $VAR4 = '';

Almost the same, but more efficient:

sub intermix { my $a_prime = reverse($a); my $b_prime = reverse($b); $a_prime cmp $b_prime } my @list = ('', '', '', ''); @list = sort intermix @list; use Data::Dumper; print Dumper(@list); __END__ output ====== $VAR1 = ''; $VAR2 = ''; $VAR3 = ''; $VAR4 = '';

This is the same thing as the last, but probably more efficient, especially for longer lists:

my @list = ('', '', '', ''); @list = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [ $_, ''.reverse($_) ] } @list; use Data::Dumper; print Dumper(@list); __END__ output ====== $VAR1 = ''; $VAR2 = ''; $VAR3 = ''; $VAR4 = '';

Note: Given addresses of the form 'a.b.c.d', the above snippets only work well if there is there is a common 'd' of for every 'a.b.c' in the list.

In reply to Re: intermix list of items by ikegami
in thread intermix list of items by tstock

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 or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2021-05-16 08:46 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (151 votes). Check out past polls.