如何在 Perl 中拆分固定宽度的列? [英] How can I break apart fixed-width columns in Perl?

查看:46
本文介绍了如何在 Perl 中拆分固定宽度的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编程对我来说太陌生了,我很抱歉不知道如何表达这个问题.

Programming is so new to me that I apologize for not knowing how to phrase the question.

我有一个从内部工具获取变量的 Perl 脚本.这并不总是它看起来的样子,但它会始终遵循以下模式:

I have a Perl script that gets a variable from an internal tool. This isn't always what it looks like, but it will always follow this pattern:

darren.local           1987    A      Sentence1
darren.local           1996    C      Sentence2
darren.local           1991    E      Sentence3
darren.local           1954    G      Sentence4
darren.local           1998    H      Sentence5

使用 Perl,将这些行中的每一行单独放入一个变量中的最简单方法是什么?根据内部工具吐出的内容,每行总是不同的,并且可能超过五行.每行中的大写字母是它最终将被排序的(所有 As、所有 Cs、所有 Es 等).我应该看正则表达式吗?

With Perl, what's the easiest way to get each of these lines into a variable by itself? Depending on what the internal tool spits out each line will always be different and there can be more than five lines. The capitalized letter in each line is what it will end up being sorted by (all As, all Cs, all Es, etc.). Should I be looking at regular expressions?

推荐答案

我喜欢使用 unpack 对于这种事情.它快速、灵活且可逆.

I like using unpack for this sort of thing. It's fast, flexible, and reversible.

您只需要知道每一列的位置,unpack 就可以自动修剪每列多余的空格.

You just need to know the positions for each column, and unpack can automatically trim the extra whitespace from each column.

如果您更改其中一列中的某些内容,通过使用相同格式重新打包很容易恢复到原始格式:

If you change something in one of the columns, it's easy to go back to the original format by repacking with the same format:

my $format = 'A23 A8 A7 A*';

while( <DATA> ) {
    chomp( my $line = $_ );

    my( $machine, $year, $letter, $sentence ) =
        unpack( $format, $_ );

    # save the original line too, which might be useful later
    push @grades, [ $machine, $year, $letter, $sentence, $_ ];
    }

my @sorted = sort { $a->[2] cmp $b->[2] } @grades;

foreach my $tuple ( @sorted ) {
    print $tuple->[-1];
    }

# go the other way, especially if you changed things
foreach my $tuple ( @sorted ) {
    print pack( $format, @$tuple[0..3] ), "\n";
    }

__END__
darren.local           1987    A      Sentence1
darren.local           1996    C      Sentence2
darren.local           1991    E      Sentence3
darren.local           1954    G      Sentence4
darren.local           1998    H      Sentence5

<小时>

现在,还有一个额外的考虑.听起来您可能在单个变量中包含这么大块的多行文本.通过打开对标量的引用的文件句柄,像处理文件一样处理它.文件句柄会处理剩下的事情:


Now, there's an additional consideration. It sounds like you might have this big chunk of multi-line text in a single variable. Handle this as you would a file by opening a filehandle on a reference to the scalar. The filehandle stuff takes care of the rest:

 my $lines = '...multiline string...';

 open my($fh), '<', \ $lines;

 while( <$fh> ) {
      ... same as before ...
      }

这篇关于如何在 Perl 中拆分固定宽度的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆