Monday, January 14, 2013

Perl 에서 sort 하기 / sort 된 array 에 새 item 삽입하기

 use strict;  
 use warnings;  
   
 test();  
   
 sub test  
 {  
        my @a = (  
               ["Hobbit", 168, 63],  
               ["Elf", 180, 54],  
               ["Dwarf", 159, 100],  
        );  
        my @b = sort byHeight(@a);  
        my @elmt = ("Human", 176, 70);  
        my @c = sort byHeight \@elmt, @b;  
          
        printDS(\@a);  
        printDS(\@b);  
        printDS(\@c);  
 }  
 sub byHeight{  
        $a->[1] <=> $b->[1];  
 }  
 sub printDS  
 {  
        my $ds = shift;  
        use Data::Dumper;   
        print Dumper @$ds;  
        print "~~~~~~~~~~~~~~~~~~~~~\n";  
 }  
레퍼런스 : Insert into a sorted array
키워드 : new element, elmt, binary search

No comments:

Post a Comment