Making a range from numbers

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
 
J

Jay Tilton

: What I need is to express all numbers in one array as a range if they
: increment by 1 per element.

Check out the Set::IntSpan module.

: 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);

use Set::IntSpan;
(my $str =
Set::IntSpan
->new( join ',' => @Numbers )
->run_list
) =~ y/,/ /;
print $str;

: print &FormatNumbers(@Numbers2); # Read that this is deprecated as of perl5,
: but still cant find a good reason why

The "&foo()" form of subroutine call is certainly not deprecated, but
the "foo()" form should be preferred unless you know what effects the
leading "&" has and you need those effects.
 
J

Just in

Thanks for the code and the pointer to the module. I installed it to my
local directory on my Unix box, and it runs like a charm.
-----8<--------------------
->new( join ',' => @Numbers )
-----8<--------------------
Had to change the => to a , in order to get it to work though.

-----8<--------------------
The "&foo()" form of subroutine call is certainly not deprecated, but
the "foo()" form should be preferred unless you know what effects the
leading "&" has and you need those effects.

-----8<--------------------
Care to expand on what the effects are?

Cheers

Just in
 
B

Ben Morrow

Just in said:
Thanks for the code and the pointer to the module. I installed it to my
local directory on my Unix box, and it runs like a charm.
Had to change the => to a , in order to get it to work though.

Huh? Which version of perl are you running?

'=>' should be the same as ',', except that if its left argument is a
bareword it will be stringified.
Care to expand on what the effects are?

Read perldoc perlsub, the paragraph starting 'A subroutine may be
called using an explicit "&" prefix.'.

Ben
 
T

Tad McClellan

Care to expand on what the effects are?


Perl's subroutines are documented in

perldoc perlsub

where it says:

&NAME(LIST); # Circumvent prototypes.


So I'm guessing the effect is to circumvent prototypes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,365
Latest member
BurtonMeec

Latest Threads

Top