Hello Monks
i have one subroutine which will take input the range of numbers and gives the expansion of it.
to be clear , if i input 2-8,10-13,18 it will give 2,3,4,5,6,7,8,10,11,12,13,18 which iam going to use it in my array extraction..
subroutine is as below(just framed as one example)...
#!/usr/bin/perl
print "To get the range of numbers \n";
print "Enter the range to see expansion \n";
chomp($range=<STDIN>);
print "The range from subroutine is :",join("==>",&get_numbers_from_ra
+nges($range)),"\n";
sub get_numbers_from_ranges {
my $spec = shift;
my %numbers;
foreach my $x ( split ',', $spec ) {
$x =~ s/\s*//g;
if ( $x =~ /(\d+)-(\d+)/ ) {
$numbers{$_} = undef for $1 .. $2;
}
else {
$numbers{$x} = undef;
}
}
# subtracting every element by one because these are used in array
+s i.e. 1st element in array index is 0
#return map { $_ - 1 } sort { $a <=> $b } keys %numbers;
return map { $_ - 1 } keys %numbers;
}
my problem is , i dont want the result to be sorted , i want the result unsorted .
for instance, if i give input as 6-9,1-3,11 i need output as 6,7,8,9,1,2,3,11 , but iam not able to achieve this, by using sort iam getting 1,2,3,6,7,8,9,11 which is not iam looking for.
if i remove sort statement as see above in code, iam getting wrong output
pls help me to fix the above code where i get the expansion of numbers in the order i inputted..
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.