develooper Front page | perl.beginners | Postings from February 2002

Re: Format list ?

Thread Previous
From:
John W. Krahn
Date:
February 5, 2002 23:23
Subject:
Re: Format list ?
Message ID:
3C60D9FB.CE406213@acm.org
Guru^Garzah wrote:
> 
> Say I have a list like this
> DOG    FEMALE DALMATION    62 POUNDS    299.00    320.00
> DOG    FEMALE DALMATION    12 POUNDS    280.00    320.00
> DOG    GERMAN SHEPARD    48 POUNDS    290.00    330.00
> CAT   FEMALE PERSIAN     8 POUNDS    219.00    300.00
> CAT    MALE CALICO     9    POUNDS    219.00   300.00
> CAT    MALE SIAMESE    12 POUNDS    219.00    300.00
> 
> I want to format say like this
> DOG
> ----------------------------------------------------------cost----------retail
> Dalmation---------------
> 
> Female dalmation 62                            299.00        320.00
> Female dalmation 12                            280.00        320.00
> 
> German Shepard------
> 
> German Shepard  48                             290.00        330.00
> 
> Cat
> -----------------------------------------------------------------------------------
> Calico--------------------
>  Calico   9                                               219.00   300.00
> 
> I hope that this provides enough information


Here is one way to do it:

my ( $heading1, $heading2 ) = ('','');

while ( <DATA> ) {
    my ( $species, $sex, $type, $weight, $cost, $retail ) =
        /^([A-Z]+)\s+(FEMALE|MALE)?\s+(\S.*\S)\s+(\d+)\s+POUNDS\s+([\d.]+)\s+([\d.]+)$/i;
    $sex ||= '';

    unless ( $heading1 eq $species ) {
        print "\n\L\u$species", '-' x (61 - length $species), "cost----------retail\n";
        $heading1 = $species;
        }
    unless ( $heading2 eq $type ) {
        print "\L\u$type", '-' x (24 - length $type), "\n";
        $heading2 = $type;
        }

    my $temp = (length $sex ? "\L\u$sex " : '') . "\L\u$type $weight";
    printf "%s%*s%-16s%-16s\n", $temp, 59 - length $temp, ' ', $cost, $retail;
    }

__DATA__
DOG    FEMALE DALMATION    62 POUNDS    299.00    320.00
DOG    FEMALE DALMATION    12 POUNDS    280.00    320.00
DOG    GERMAN SHEPARD    48 POUNDS    290.00    330.00
CAT   FEMALE PERSIAN     8 POUNDS    219.00    300.00
CAT    MALE CALICO     9    POUNDS    219.00   300.00
CAT    MALE SIAMESE    12 POUNDS    219.00    300.00



John
-- 
use Perl;
program
fulfillment

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About