2行を1行にまとめる

30分プログラム、その446。http://gauc.no-ip.org/awk-users-jp/blis.cgi/DoukakuAWK_020より。

123458.69
KK5899966
23698565.36
MM5897155998

というものを以下のようにします。

123458.69,KK5899966
23698565.36,MM5897155998

要するに 2 行を 1 行にして表示するというものです。

普通に行数を覚えておいて、カンマと改行を出力し分けるだけだとつまんないなー、と思ってmapとか使っているけど、効率が悪くなっている気がする。まあいいや。

使い方

$ perl 2to1.pl sample
123458.69, KK5899966
23698565.36, MM5897155998

ソースコード

#! /usr/bin/perl
# -*- mode:perl; coding:utf-8 -*-
#
# 2to1.pl -
#
# Copyright(C) 2008 by mzp
# Author: MIZUNO Hiroki / mzpppp at gmail dot com
# http://howdyworld.org
#
# Timestamp: 2008/12/26 23:02:58
#
# This program is free software; you can redistribute it and/or
# modify it under MIT Lincence.
#

use strict;
use warnings;
use Data::Dumper;
sub sep($@){
    my ($n,@xs) = @_;
    if(@xs){
	([@xs[0..$n-1]],&sep($n,@xs[$n..$#xs]));
    }else{
	();
    }
}

for(sep 2,map {chomp;$_} <>){
    print "$_->[0], $_->[1]\n";
}