Algorithm-Loops

 view release on metacpan or  search on metacpan

ex/MatrixMult.plx  view on Meta::CPAN

            if(  $exp <= $exp2fmt  ) {
                $fmt= $fmt4exp{$exp2fmt};
                last;
            }
        }
        my $str= $fmt;
        if(  $fmt =~ /e/  ) {
            $str =~ s/\+/$sign/;
            $str =~ s/1/$one/;
            $str =~ s{\.(0+)(?=[eE])}{
                (   sprintf( "%14.".length($1)."e", $num )
                        =~ /(\.\d+)/
                )[0]
            }e;
            $str =~ s/(9+)/sprintf "%0".length($1)."d", $eabs/e;
        } else {
            if(  $exp < 0  ) {
                $str =~ s/\+/$sign/;
                $rest= '0'x($eabs-1) . $one . $rest;
            } else {
                $one= substr( $one . $rest, 0, 1+$exp );
                substr( $rest, 0, $exp )= '';
                $str =~ s/([\s+0]+)/sprintf "%+".length($1)."s", $sign.$one/e;
            }
            $str =~ s{\.(0+)}{
                (   sprintf( "%14.".length($1)."f", $num )
                        =~ /(\.\d+)$/
                )[0]
            }e;
            $str =~ s{\.(0+)$}{
                (   sprintf( "%14.".length($1)."f", $num )
                        =~ /(\.\d+)/
                )[0]
            }e;
            $str =~ s/(\.?0+)$/' ' x length($1)/e
                if  $fmt =~ /\./;
        }
        return $str;
    }

}


sub Main {
    if(  ! @_  ) {

ex/NaturalSort.plx  view on Meta::CPAN


use Algorithm::Loops qw( Filter );

@ARGV= split ' ', <DATA>
    if  ! @ARGV;

print join " ", Filter {
    s/\d(\d+)/$1/g
} sort
&Filter( sub {
    s/(\d+)/length($1).$1/ge
}, @ARGV );

print join " ", Filter {
    s/\d(\d+)=(0*)=/$2$1/g
} sort
&Filter( sub {
    s/(0*)(\d+)/length($2)."$2=$1="/ge
}, @ARGV );

__END__
m5 m45 m345 m05 m004 m0035 n2 l8

lib/Algorithm/Loops.pm  view on Meta::CPAN

letters and natural numbers (non-negative
integers) both alphabetically and numerically
at the same time?  This simple way to do a
"natural" sort is also one of the fastest.
Great for sorting version numbers, file names,
etc.:

    my @sorted= Filter {
        s#\d{2}(\d+)#\1#g
    } sort Filter {
        s#(\d+)# sprintf "%02d%s", length($1), $1 #g
    } @data;

[ Note that at least some versions of Perl have a bug that breaks C<sort>
if you write C<sub {> as part of building the list of items to be sorted
but you don't provide a comparison routine.  This bug means we can't
write the previous code as:

    my @sorted= Filter {
        s#\d{2}(\d+)#\1#g
    } sort Filter sub {
        s#(\d+)# sprintf "%02d%s", length($1), $1 #g
    }, @data;

because it will produce the following error:

    Undefined subroutine in sort

in some versions of Perl.  Some versions of Perl may even require you
to write it like this:

    my @sorted= Filter {
        s#\d{2}(\d+)#\1#g
    } sort &Filter( sub {
        s#(\d+)# sprintf "%02d%s", length($1), $1 #g
    }, @data );

Which is how I wrote it in ex/NaturalSort.plx. ]

Need to sort names?  Then you'll probably want to ignore letter case and
certain punctuation marks while still preserving both:

    my @compare= Filter {tr/A-Z'.,"()/a-z/d} @names;
    my @indices= sort {$compare[$a] cmp $compare[$b]} 0..$#names;
    @names= @names[@indices];

lib/Algorithm/Loops.pm  view on Meta::CPAN


    $sum=map{(1)x$_}@ints;

for adding up a list of natural numbers. q-: ]

=head3 Differences

The different MapCar* functions are only different in how they deal with
being pqssed arrays that are not all of the same size.

If not all of your arrays are the same length, then MapCarU() will pass
in C<undef> for any values corresponding to arrays that didn't have
enough values.  The "U" in "MapCarU" stands for "undef".

In contrast, MapCar() will simply leave out values for short arrays (just
like I left the "U" out of its name).

MapCarE() will croak without ever calling your subroutine unless all of
the arrays are the same length.  It considers it an Error if your arrays
are not of Equal length and so throws an Exception.

Finally, MapCarMin() only calls your subroutine as many times as there
are elements in the B<shortest> array.

In other words,

    MapCarU \&MySub, [1,undef,3], [4,5], [6,7,8]

returns

lib/Algorithm/Loops.pm  view on Meta::CPAN


    MapCarE: Arrays with different sizes (3 and 2)

=head3 Examples

Transposing a two-dimensional matrix:

    my @transposed= MapCarE {[@_]} @matrix;

or, using references to the matrices and allowing for different row
lengths:

    my $transposed= MapCarU {[@_]} @$matrix;

Formatting a date-time:

    my $dateTime= join '', MapCarE {
        sprintf "%02d%s", pop()+pop(), pop()
    } [ (localtime)[5,4,3,2,1,0] ],
      [ 1900, 1, (0)x4 ],
      [ '// ::' =~ /./g, '' ];

t/assert.t  view on Meta::CPAN

        if(  m#^[/0]#  ) {
            chomp( $check= $_ );
        } elsif(  /^:/  ) {
            $croak= 0   if  /croak/;
        } elsif(  /^#/  ) {
            ;
        } else {
            if(  ! s/\s*#.*//s  ) {
                chomp;
            }
            $_ .= ' ' x ( 32 - length );
            my( $sub )= /^&?(\w+)/i;
            my $c= 4 - !$sub - !$check - !$croak;
            $_ .= join "#", '', $n..$n+$c-1, $/;
            $n += $c;
        }
        print;
    }
}
BEGIN { SetLineNum }
__END__



( run in 0.780 second using v1.01-cache-2.11-cpan-65fba6d93b7 )