How does @filesize_names get altered by the map when the conditional in make_singular is true? When the conditional is false, @filesize_names is not altered by the map outside make_singular.
#!/usr/bin/perl
use strict;
use warnings FATAL => qw( all );
use Data::Dumper;
use List::MoreUtils qw(firstidx);
use lib 'lib';
use Base::Nifty qw(pretty_number);
local $\ = "\n";
my @filesize_names = qw(bit nibble byte kilobyte megabyte gigabyte ter
+abyte petabyte exabyte zettabyte yottabyte);
# I put the these sizes here just for my information.
my %little_filesizes;
$little_filesizes{bit} = 1;
$little_filesizes{nibble} = $little_filesizes{bit} * 4;
$little_filesizes{byte} = $little_filesizes{bit} * 8;
# I never know when I'll want a random file size.
sub random_filesize {
return $filesize_names[rand @filesize_names]
}
sub make_singular {
my $word = shift;
$word =~ s/s$//;
my @short_sizes = map(s/^(\w)\w{1,}$/$1b/,@filesize_names);
if (grep(/\L$word\E/,@short_sizes)) {
$word = $filesize_names[firstidx { $_ eq lc $word} @short_sizes];
}
return lc $word;
}
# from tye: my %hash; @hash{@sizes} = 0..$#sizes;
# from MidLifeXis: $result = $original * $units{$inmultiplier} / $unit
+s{$outmultiplier}
sub convert_filesize {
my %opt = @_;
# I took out bits and nibbles just to keep me sane.
my @filesizes = grep($_ =~ /byte/,@filesize_names);
my $from = firstidx { $_ eq make_singular($opt{from}) } @filesizes;
my $to = firstidx { $_ eq make_singular($opt{to}) } @filesizes;
my $dec = $opt{decimals} ? $opt{decimals} : 0;
my $base = $opt{base} ? $opt{base} : 1024;
my ($diff,$converted);
if ( $from > $to ) {
$diff = $from - $to;
$converted = $opt{size} * ($base ** $diff);
}
elsif ( $to > $from ) {
$diff = $to - $from;
$converted = $opt{size} / ($base ** $diff);
}
else {
$converted = $opt{size};
}
my $org_filesize = pretty_number($dec,$opt{size});
my $new_filesize = pretty_number($dec,$converted);
return "$org_filesize $opt{from} ($from) is $new_filesize $opt{to} (
+$to)";
}
print random_filesize;
my $conversion = convert_filesize(
size => 10000000,
from => 'bytes',
to => 'megabytes',
);
print $conversion;
I just tried...
sub make_singular {
my $word = shift;
$word =~ s/s$//;
my @in_sub_filesize_names = @filesize_names; # added this line, the
+output is still wrong.
my @short_sizes = map(s/^(\w)\w{1,}$/$1b/,@in_sub_filesize_names);
if (grep(/\L$word\E/,@short_sizes)) {
$word = $filesize_names[firstidx { $_ eq lc $word} @short_sizes];
}
return lc $word;
}
Have a cookie and a very nice day!
Lady Aleena
-
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 How to display code and escape characters
are good places to start.