Goo::List - Utility functions for handling lists


Goo documentation Contained in the Goo distribution.

Index


Code Index:

NAME

Top

Goo::List - Utility functions for handling lists

SYNOPSIS

Top

use Goo::List;

DESCRIPTION

Top

METHODS

Top

get_unique

return a sorted unique list

halve_list

return a list split in two

quarter_list

return a list split in four

AUTHOR

Top

Nigel Hamilton <nigel@trexy.com>

SEE ALSO

Top


Goo documentation Contained in the Goo distribution.

package Goo::List;

###############################################################################
# Nigel Hamilton
#
# Copyright Nigel Hamilton 2005
# All Rights Reserved
#
# Author:       Nigel Hamilton
# Filename:     Goo::List.pm
# Description:  Utility functions for handling lists
#
# Date          Change
# -----------------------------------------------------------------------------
# 27/01/2005    Auto generated file
# 27/01/2005    Initially needed a way to split lists
#
###############################################################################

use strict;


###############################################################################
#
# get_unique - return a sorted unique list
#
###############################################################################

sub get_unique {

    my (@list) = @_;

    my $seen;

    foreach my $member (@list) {
        $seen->{$member} = 1;
    }

    return sort { $a cmp $b } keys %$seen;

}


###############################################################################
#
# halve_list - split a list in two
#
###############################################################################

sub halve_list {

    my (@list) = @_;

    my $halfway = scalar(@list)/2;

    my @list1;
    my @list2;

    for (my $i = 0; $i <= $#list; $i++) {

        if ($i >= $halfway) {
            push(@list2, $list[$i]);
        } else {
            push(@list1, $list[$i]);
        }

    }

    return (\@list1, \@list2);
}


###############################################################################
#
# quarter_list - split a list in four!
#
###############################################################################

sub quarter_list {

    my (@list) = @_;

    my ($a, $b) = halve_list(@list);

    my ($list1, $list2) = halve_list(@$a);

    my ($list3, $list4) = halve_list(@$b);

    return ($list1, $list2, $list3, $list4);

}


1;


__END__