Hi All,
I have an array in perl as @match = (201001,201002,201001,201002);
I am trying to sort this array as
@match = sort(@match);
print "@match";
I dont see the output sorted any answers
I also tried another way, but still the results are not sorted
foreach my $match (sort { $a <=> $b } @match) {
print "$match\n"; }
any help please
Thanks
Deepak
Assuming: @match = (201001,201002,201001,201002);
try this, which is assuming numerical sort:
@sorted = sort { $a <=> $b } @match ;
if char based:
@sorted = sort { lc($a) cmp lc($b) } @match;
Thank you for your answers,
I am not getting results for an array in the run time
for above example : i wrote a small program it works
$ vi test.pl
#!/usr/bin/perl
@match = ("201001","201002","201001","201002");
@sorted = sort { $a <=> $b } @match ;
print "@sorted";
==> but for my runtime array program i am not getting the right results
#!/usr/bin/perl
@input=<>;
foreach (@input)
{
if(/2010/)
{
@G=split /_/;
@match = grep (/p20/i, @G);
foreach (@match){
s/p//;
}
#@match = reverse sort(@match);
@sorted = sort { $a <=> $b } @match ;
#@sorted = sort { lc($a) cmp lc($b) } @match;
print "@sorted";
#@sorted_numbers = reverse sort {$a <=> $b} @match;
#foreach my $match (sort { $a <=> $b } @match) {
#print "$match"; }
}
}
===> Input to the file is
test_main_p201001
test_main_p201002
test_sub_p201001
test_sub_p201002
---------- Post updated at 11:15 AM ---------- Previous update was at 11:13 AM ----------
any help of suggestion
Thank you so much for taking your time to answer my question.
---------- Post updated at 11:29 AM ---------- Previous update was at 11:15 AM ----------
thank you for reading... I found my mistake
===>Here is the correct one...
#!/usr/bin/perl
@input=<>;
@test=('');
foreach (@input)
{
if(/2010/)
{
@G=split /_/;
@match = grep (/p20/i, @G);
foreach (@match){
s/p//;
}
push(@test,@match);
}
}
@sorted = sort { $a <=> $b } @test ;
print "@sorted";