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

Re: Concatenating

Thread Previous | Thread Next
From:
John W. Krahn
Date:
April 27, 2002 05:27
Subject:
Re: Concatenating
Message ID:
3CCA9911.657A1AA2@acm.org
James Poni wrote:
> 
> Hello

Hello,

> I have 3 text files and i want to perform concatenation from each line of
> the 3 files.
> 
> I want the output to look like this:
> 
> aaa mon 1
> bbb wed 2
> ccc sun 3
> ddd tue 4
> 
> Is it possible to sort the days column in order of the week? Eg
> 
> ccc sun 3
> aaa mon 1
> ddd tue 4
> bbb wed 2
> 
> [snip code]


Here is one way to do it:

#!/usr/bin/perl -w
use strict;
use Inline::Files;

my @info;
my %days = (
    sun => 0,
    mon => 1,
    tue => 2,
    wed => 3,
    thu => 4,
    fri => 5,
    sat => 6,
    );

my $count;
chomp, push @{$info[$count++]}, $_ for <INFO1>;
$count = 0;
chomp, push @{$info[$count++]}, $_ for <INFO2>;
$count = 0;
chomp, push @{$info[$count++]}, $_ for <INFO3>;


@info = sort { $days{$a->[1]} <=> $days{$b->[1]} } @info;


for my $row ( @info ) {
    print "@$row\n";
    }

__INFO1__
aaa
bbb
ccc
ddd
__INFO2__
mon
wed
sun
tue
__INFO3__
1
2
3
4



John
-- 
use Perl;
program
fulfillment

Thread Previous | Thread Next


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