Front page | perl.beginners |
Postings from December 2011
Re: issue with perl array code
Thread Previous
|
Thread Next
From:
Brandon McCaig
Date:
December 2, 2011 09:32
Subject:
Re: issue with perl array code
Message ID:
20111202173546.GG9756@castopulence.org
On Fri, Dec 02, 2011 at 01:24:05AM -0500, Sunita.Pradhan@emc.com wrote:
> Hi All
Hello:
> I have following code set of code which will have a
> subroutine which has 2 parameters. The first parameter
> (VALUES) is a reference to an array of non-sorted, hex strings.
> The second parameter (RANGES) is a reference to an array that
> is empty. The subroutine should fill the RANGES array with
> consecutive ranges built from the VALUES array. The subroutine
> should return the number of ranges built .
>
> Example:
>
> @array = ("0A0", "005", "001", "004", "0BC", "004", "002", "001");
>
> $numRanges = buildRanges(VALUES => \@array, REF_RANGES=>\@ranges);
>
> The array @ranges should be filled as follows:
>
> @ranges = ("001:002", "004:005", "0A0", "0BC"); ==> I am not getting all the values in the array . I am getting only ranges in array .
>
> And $numRanges should be 2.
If I were to tackle this problem I would probably start by
converting the hex strings to numbers and sorting them in
numerical order. Then I'd build the ranges, iterating through the
sorted array from beginning to end, and then generate the ranges
from that. Finally, convert the numbers back to hex strings and
separate range lists with a ':'.
#!/usr/bin/perl
use v5.010;
use strict;
use warnings;
use Data::Dumper;
sub build_ranges
{
my ($values, $ranges) = @{{@_}}{qw(values ranges)};
# A little bit of bounds checking.
return 0 if @$values == 0;
# A little flexibility.
$ranges //= [];
# Convert hex strings to numbers; sort numerically.
@$values = sort { $a <=> $b } map hex, @$values;
# Initialize the first range with the first element.
my @range = ($values->[0]);
my $i = 0;
for my $value (@{$values}[1 .. $#{$values}])
{
# If this element is right after the last element in the
# current range then add it to the current range.
if($range[-1] + 1 == $value)
{
push @range, $value;
}
else
{
# Otherwise, add this range to the ranges array, and
# reset the range array with just the current value.
push @$ranges, [@range];
@range = ($value);
}
}
# Add the last range to the ranges array.
push @$ranges, [@range];
# Turn our numbers back into hex strings and join ranges by a
# ':' character.
for my $i (0 .. $#{$ranges})
{
$ranges->[$i] = join ':',
map { sprintf '%03X', $_ } @{$ranges->[$i]};
}
# Return the number of ranges (that are more than one number
# in length), and the ranges array reference (for
# convenience).
return (scalar (grep { /:/ } @$ranges), $ranges);
}
my @tests = (
[qw(0A0 005 001 004 0BC 004 002 001)],
[qw(0A0 005 001 0A1 004 0BC 002)]);
for my $values (@tests)
{
my ($num_ranges, $ranges) = build_ranges(values => $values);
print Data::Dumper->Dump(
[$num_ranges, $ranges],
[qw(num_ranges ranges)]);
}
__END__
HTH.
Regards,
--
Brandon McCaig <bamccaig@gmail.com> <bamccaig@castopulence.org>
Castopulence Software <https://www.castopulence.org/>
Blog <http://www.bamccaig.com/>
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'
Thread Previous
|
Thread Next