Java教程

[ Perl ] 对文本文件进行行列翻转

本文主要是介绍[ Perl ] 对文本文件进行行列翻转,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

https://www.cnblogs.com/yeungchie/

code

#!/usr/bin/env perl
#-----------------------------
# Program  : reverseRowCol.pl
# Language : Perl
# Author   : YEUNGCHIE
# Version  : 2021.09.01
#-----------------------------
use v5.10;
use strict;
use warnings;
use List::Util qw/max/;

my ($in,$out) = @ARGV;

open IN,"< $in";
my @array;
push @array,[split] for <IN>;
close IN;

my $index = max(map { scalar @$_ } @array)-1;

open OUT,"> $out";
for my $i (0..$index){
    printf OUT "%s\n",(join ' ',map { my @row = @$_; $row[$i] } @array);
}
close OUT;

__END__

example

shell> cat txt
00 01 02 03 04
05 06 07 08 09
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24

shell> ./reverseRowCol.pl txt txt1

shell> cat txt1
00 05 10 15 20
01 06 11 16 21
02 07 12 17 22
03 08 13 18 23
04 09 14 19 24
这篇关于[ Perl ] 对文本文件进行行列翻转的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!