Hi Sunita,
On Fri, 2 Dec 2011 01:24:05 -0500
<[email protected]> wrote:
> Hi All
>
> 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.
>
> ============================
> #!/usr/bin/perl
> use strict;
>
You should also add "use warnings;".
> my @array = ("0A0", "005", "001", "0A1","004", "0BC", "002" );
> my @ranges = ();
> my $numRanges = buildranges(VALUE => \@array , REF_RANGES=>\@ranges);
>
> print "No of ranges exist in array : $numRanges .\n";
>
> sub buildranges{
> my ($value , $ref_array , $range , $ref_range)= @_;
That's not a very robust way to unpack the named arguments. A better way would
be to do:
my %args = @_;
my $ref_array = $args{VALUE};
my $ref_range = $args{REF_RANGES};
Furthermore, you shouldn't call a variable "array" - give it something more
meaningful.
> my @array = @$ref_array;
> my @ranges = @$ref_range;
This will dereference the arrays and create a flat copy. As a result, the
referenced variable won't be modified. If you want it to be, you should operate
on the reference directly. See:
http://perl-begin.org/topics/references/
> my ($int1,$int2);
> for (my $i=0;$i < @array;$i++){
> my $int1 = hex($array[$i]);
> for (my $j=1;$j < @array; $j++){
> $int2 = hex($array[$j]);
> if ($int2 == ($int1 + 1)){
> push(@ranges,"$array[$i]:$array[$j]");
> }
> elsif ($int2 == ($int1 - 1)){
> push(@ranges,"$array[$j]:$array[$i]");
> }
> else {
> next;
> }
1. You don't need the "next" here.
2. I don't think this will handle ranges of more than one consecutive integer
correctly.
3. You can have a more efficient algorithm by sorting the array first.
4. You can use a foreach loop here instead.
>
> }
> }
> my %hash_ranges = map {$_ => 1 } @ranges;
> @ranges = keys %hash_ranges;
> my $numRanges = \@ranges;
That will not put the number of ranges into $numRanges. If you want that, you
should do «my $numRanges = @ranges;» which will evaluate @ranges in scalar
context and return its number of elements.
Regards,
Shlomi Fish
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
My Aphorisms - http://www.shlomifish.org/humour.html
Chuck Norris does not code; when he sits at a computer, it just does whatever
he wants. — Kattana on Freenode’s #perl6 .
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/