Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

comment on

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

Case sensitivity is outside the scope of split_sort, and should a user want to add case insensitivity to the use of split_sort, they can do something like the following.

my @sorted = sort { split_sort( fc($a), fc($b), 'type', 'split expr') +} @array; # I made changes to split_sort detailed in a moment.

With a change I made to split_sort that should work in alpha or letter is chosen for type.

After failing to come up with a list of strings where the split would return something different from a standard sort, I was about to abandon it again. Then a thought popped into my head, and it was not the Stay Puffed marshmallow man. There might be strings where numbers were on one side the potential split and alpha was on the other and someone might want the numbers sorted numerically. So, I added the left and right options. Also, since alpha (or letter) doesn't benefit from the splitting of the strings, I had those options skip the splitting.

sub split_sort { my ($in_a, $in_b, $sort_type, $split) = @_; if ($sort_type =~ /^(alpha|letter)/) { $in_a cmp $in_b } else { $split = qr($split); my ($numa1, $numa2) = split(/$split/, $in_a, 2); my ($numb1, $numb2) = split(/$split/, $in_b, 2); if ($sort_type =~ /^num/) { $numa1 <=> $numb1 || $numa2 <=> $numb2 } elsif (fc($sort_type) eq 'left' ) { $numa1 <=> $numb1 || $numa2 cmp $numb2 } elsif (fc($sort_type) eq 'right' ) { $numa1 cmp $numb1 || $numa2 <=> $numb2 } } }

Another change I made is the I changed is order of the parameters. Since the parameter $split can be ignored for alpha, I put it at the end. I prefer to put any parameter than can be ignored or undef to be at the end of the parameter list. (If two or more parameters can be undef or ignored, then I think it is best to make them $opt.)

With those changes, the POD became easier to write. It may still be incomplete, but this is what I have so far.

=pod =encoding utf8 =head1 NAME B<Fancy::Sort::Split> returns the expression to split the values in li +sts for sort. =head1 VERSION This document describes Fancy::Sort::Split version 1.0. =head1 SYNOPSIS my @numbers = qw(1:2 1:02 3:4 5:78 50:89 10:5); my @split_sorted = sort { split_sort($a, $b, 'number', ':') } @numbe +rs; # returns # [ # '1:2', # '1:02', # '3:4', # '5:78', # '10:5', # '50:89' # ]; my @left = qw(2:a 02:a 4:a 28:a 89:a 5:a); my @split_sorted_left = sort { split_sort($a, $b, 'left', ':') } + @left; # returns # [ # '2:a', # '02:a', # '4:a', # '5:a', # '28:a', # '89:a' # ]; my @right = qw(a:2 a:02 a:4 a:28 a:89 a:5); my @split_sorted_right = sort { split_sort($a, $b, 'right', ':') } + @right; # returns # [ # 'a:2', # 'a:02', # 'a:4', # 'a:5', # 'a:28', # 'a:89' # ]; =head1 DESCRIPTION Fancy::Sort::Split returns the expression to split the values in lists + for L<sort|> subroutines + using C<split_sort>. C<split_sort> has to be imported into your scri +pt. C<split_sort> has four required parameters. The first and second parem +eters are C<$a> and C<$b> from C<sort> or C<$b> and C<$a> if you want + a descending sort. The third parameter is the expression you want to + split the strings by. The fourth is the type of sort you want, C<num +ber> or C<alpha> (C<letter>). split_sort($a, $b, 'type', 'expr'); A note of caution for the numerical sorts, when a number has a leading + zero (C<02>), the leading zero will be dropped. So, C<02> will be th +e same as C<2>. It requires Perl version 5.16.0 or better. =head2 Numerical sort When you have numbers on both sides of the expression, use C<number> s +o the numbers on both sides are numerically sorted. split_sort($a, $b, 'number', 'expr'); =head2 Numerical sort on the left When you have numbers on the left side of the expression, use C<left> +so the numbers on the left side are numerically sorted.. split_sort($a, $b, 'left', 'expr'); =head2 Numberial sort on the right When you have numbers on the right side of the expression, use C<right +> so the numbers on the right side are numerically sorted.. split_sort($a, $b, 'right', 'expr'); =head2 Alphabetical sort When you have letters on both sides of the expression, use C<alpha> or + C<letter>. However, the alphabetical sort is redundant and was added + for completeness. The sort expression returned will be the same as C +<$a cmp $b> for the entire string. So, for alphabetical sorts, you ma +y omit the expression for the split. split_sort($a, $b, 'alpha', 'expr'); split_sort($a, $b, 'letter', 'expr'); =head1 DEPENDENCIES Fancy::Sort::Split depends on L<Exporter>. =head1 AUTHOR Lady Aleena =cut

I am a bit disappointed that the alpha part did not pan out as I wanted, but I did not put much thought into it in the first place. I am fairly happy with it as it is now.

My OS is Debian 10 (Buster); my perl versions are 5.28.1 local and 5.16.3 or 5.30.0 on web host depending on the shebang.

No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
Lady Aleena

In reply to Re^2: Coming up with good examples in POD by Lady_Aleena
in thread Coming up with good examples in POD by Lady_Aleena

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • 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 lurking in the Monastery: (6)
    As of 2021-01-21 19:26 GMT
    Find Nodes?
      Voting Booth?