J
Just in
Need some peer guidance here, please correct and suggest as necessary.
I have code that generates a hash of arrays through auto vivification. Each
array for each key gets vivified from a file with a range of numbers that
matches a specific condition - for this example lets pick 1-15 (typically it
will be 1-3000).
What I need is to express all numbers in one array as a range if they
increment by 1 per element.
Heres my code:
use strict;
use warnings;
my @Numbers = qw(1 2 3 5 6 8 11 12 13 14);
my @Numbers2 = qw(4 7 9 10 15);
print &FormatNumbers(@Numbers2); # Read that this is deprecated as of perl5,
but still cant find a good reason why
sub FormatNumbers
{
my @In = @_;
my $Return;
my $Str = $In[0];
foreach my $i(1..$#In)
{
unless($In[$i] - $In[$i - 1] == 1)
{
$Str .= "-$In[$i - 1] $In[$i]";
}
$Str .= "-$In[$#In]" if $i == $#In;
}
my @Check = split /\s+/, $Str;
foreach my $i(1..$#Check)
{
if($Check[$i] =~ /^(\d+)-(\d+)/)
{
if($1 == $2) # ? : doesnt seem to work here
{
$Return = "@In";
}
else
{
$Return = "@Check";
}
}
}
return $Return;
}
First thing that seems dumb is that I have to do a check on the output, I'm
sure theres a nifty one line regexp that will suffice for the whole sub.
Next thing you'll note is that for the above the output for both arrays is:
1-3 5-6 8-8 11-14
4 7 9 10 15
8-8 obviously pollutes the desired result. And I'm sure there are other
variations that will mess it up even further, as there is no limitation on
the amount of generated arrays and their respective contents, (except that
each number in the range can be in any of the arrays only once).
Dont really mind what you guys suggest - be it a module, a patch for the
code, or a complete rewrite.
Just in
I have code that generates a hash of arrays through auto vivification. Each
array for each key gets vivified from a file with a range of numbers that
matches a specific condition - for this example lets pick 1-15 (typically it
will be 1-3000).
What I need is to express all numbers in one array as a range if they
increment by 1 per element.
Heres my code:
use strict;
use warnings;
my @Numbers = qw(1 2 3 5 6 8 11 12 13 14);
my @Numbers2 = qw(4 7 9 10 15);
print &FormatNumbers(@Numbers2); # Read that this is deprecated as of perl5,
but still cant find a good reason why
sub FormatNumbers
{
my @In = @_;
my $Return;
my $Str = $In[0];
foreach my $i(1..$#In)
{
unless($In[$i] - $In[$i - 1] == 1)
{
$Str .= "-$In[$i - 1] $In[$i]";
}
$Str .= "-$In[$#In]" if $i == $#In;
}
my @Check = split /\s+/, $Str;
foreach my $i(1..$#Check)
{
if($Check[$i] =~ /^(\d+)-(\d+)/)
{
if($1 == $2) # ? : doesnt seem to work here
{
$Return = "@In";
}
else
{
$Return = "@Check";
}
}
}
return $Return;
}
First thing that seems dumb is that I have to do a check on the output, I'm
sure theres a nifty one line regexp that will suffice for the whole sub.
Next thing you'll note is that for the above the output for both arrays is:
1-3 5-6 8-8 11-14
4 7 9 10 15
8-8 obviously pollutes the desired result. And I'm sure there are other
variations that will mess it up even further, as there is no limitation on
the amount of generated arrays and their respective contents, (except that
each number in the range can be in any of the arrays only once).
Dont really mind what you guys suggest - be it a module, a patch for the
code, or a complete rewrite.
Just in