Sponsored Content
Full Discussion: perl array sorting
Top Forums Shell Programming and Scripting perl array sorting Post 302393370 by bsdeepu on Monday 8th of February 2010 02:29:53 PM
Old 02-08-2010
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

Code:
$ 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

Code:
#!/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
Code:
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...
Code:
#!/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";


Last edited by pludi; 02-09-2010 at 02:01 AM.. Reason: code tags, please...
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl: Sorting an associative array

Hi, When using sort on an associative array: foreach $key (sort(keys(%opalfabet))){ $value = $opalfabet{$key}; $result .= $value; } How does it handle double values? It seems to me that it removes them, is that true? If so, is there a way to get... (2 Replies)
Discussion started by: tine
2 Replies

2. Shell Programming and Scripting

sorting data using array in ksh

plz help me..........i have a ksh script that sorts data in ascending order. the 1st half is correct,but for the line no 31 its showing problem 1 #!/bin/ksh 2 3 4 5 echo "Enter the array length" 6 read num 7 8 9 echo "enter the... (4 Replies)
Discussion started by: ali560045
4 Replies

3. Shell Programming and Scripting

Sorting Awk generalized array

Generalized arrays take any type of variable(s) as subscripts, but the subscript(s) are treated as one long string expression. The use of for(a in x) on a generalized array will return all of the valid subscripts in some order, not necessarily the one you wished. How can I make it so that i... (2 Replies)
Discussion started by: gio001
2 Replies

4. Shell Programming and Scripting

Sorting value frequency within an array

How is it possible to sort different nummeric values within an Array. But i don`t want the highest or the lowest. I need the most frequently occurring value. For examble: My Array has to following values = (200 404 404 500 404 404 404 200 404) The result should be 404 The values are... (3 Replies)
Discussion started by: 2retti
3 Replies

5. Shell Programming and Scripting

sorting numeric array

Hi, I would like to do the following sorting, but the output is not what i expected. Why 222 and 2222 are not at the last two elements of array? awk 'BEGIN{a="22";a="2222";a="33";a="44";a="222";a="11";a="22";a="33";asort(a); for (i=1;i<=8;i++) print a}' 11 22 22 222 2222 33 33 44... (1 Reply)
Discussion started by: phoeberunner
1 Replies

6. Shell Programming and Scripting

[Perl] Sorting an String-Array

Hi, i have a txtfile with the format <Nr>tab<word>tab<other stuff>new line and i want to sort the <word>-colum with a perl script. My textfile: <Nr>tab<word>tab<other stuff>new line 6807 die ART.Acc.Sg.Fem 6426 der ART.Gen.Sg.Fem 2 die ART.Nom.Sg.Fem 87 auf APPR.-- 486 nicht PTKNEG.--... (1 Reply)
Discussion started by: buckelede
1 Replies

7. Shell Programming and Scripting

Sorting awk array output?

Hi all, I have a script which produces a nice table but I want to sort it on column 3. This is the output line in the script: # Output { FS = ":"; format = "%11s %6s %-16s\n"; prinft "\n" printf ( format, "Size","Count","Who" ) } for (i in... (21 Replies)
Discussion started by: Cowardly
21 Replies

8. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

9. Shell Programming and Scripting

Sorting output of AWK array

I need help to sort the output of an awk array Example datadata="1 blue 2 green 3 blue 4 yellow 5 blue 6 red 7 yellow 8 red 9 yellow 10 yellow 11 green 12 orange 13 black" My awk line to get output in one lineecho "$data" | awk ' {arr++; next} END { for (i in arr) { if(arr>1 )... (2 Replies)
Discussion started by: Jotne
2 Replies

10. UNIX for Beginners Questions & Answers

Perl/Array Sorting : Can someone please explain below code $h{$_}++

sub uniq { my %h; return grep { !$h{$_}++ } @_ } The above code is to remove duplicates from array. I am having hard time understanding below things (basically around highlighted code in bold)- when was the value inserted in hash? and are we only adding a key in Hash not... (1 Reply)
Discussion started by: Tanu
1 Replies
FC-MATCH(1)															       FC-MATCH(1)

NAME
fc-match - match available fonts SYNOPSIS
fc-match [ -svV? ] [ --sort ] [ --verbose ] [ --version ] [ --help ] [ font-pattern ] DESCRIPTION
fc-match matches font-pattern (empty pattern by default) using the normal fontconfig matching rules to find the best font available. If --sort is given, the sorted list of best matching fonts is displayed. With --verbose, the whole font pattern for each match is printed, otherwise only the file, family and style are printed.. OPTIONS
This program follows the usual GNU command line syntax, with long options starting with two dashes (`-'). A summary of options is included below. -v --verbose Print whole font pattern for each match. -? --help Show summary of options. -V --version Show version of the program and exit. -s --sort Displays sorted list of best matching fonts. font-pattern Displays fonts matching font-pattern (uses empty pattern by default). SEE ALSO
fc-list (1). The fontconfig user's guide, in HTML format: /usr/share/doc/packages/fontconfig/fontconfig-user.html. AUTHOR
This manual page was updated by Patrick Lam <plam@csail.mit.edu>. 16 October 2006 FC-MATCH(1)
All times are GMT -4. The time now is 07:39 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy