[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] using one array to sort another




6/28/99, alan wrote:

    The end result I would like to have is an array @results to be:

    1|||Joe||Alexander
    1.1|||Nathan||Alexander
    1.1.p.1|||Claudia||Alexander
    1.1.1|||Bob||Alexander
    1.1.2|||Jeff||Alexander
    1.1.3|||Herb||Alexander

    Any ideas?


Alan,

Here's one way, if you're willing to store things in a hash, and print
things in lexical sort order, rather than 'p' before '1' order...

rkm

    -------------------------------------------------
    #!/usr/bin/perl -w
    use diagnostics ;
    use strict ;

    my $names =
    {
        '1.1.2' => 'Jeff||Alexander',
        '1' => 'Joe||Alexander',
        '1.1.p.1' => 'Claudia||Alexander',
        '1.1' => 'Nathan||Alexander',
        '1.1.3' => 'Herb||Alexander',
        '1.1.1' => 'Bob||Alexander'
    } ;

    my @results = map $_ . '|||' . $names->{$_} . "\n" , sort keys %$names ;

    print @results ;


    ## Prints:
    ##
    ## 1|||Joe||Alexander
    ## 1.1|||Nathan||Alexander
    ## 1.1.1|||Bob||Alexander
    ## 1.1.2|||Jeff||Alexander
    ## 1.1.3|||Herb||Alexander
    ## 1.1.p.1|||Claudia||Alexander
    -------------------------------------------------



===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org