English 中文(简体)
我怎么能够正确处理包含在Perl的表格分离价值的文件?
原标题:How can I correctly process this file containing tab separated values in Perl?
  • 时间:2010-11-18 21:49:37
  •  标签:
  • perl

我对Perl说得很新,对Perl的 proper子一无所知。

我有一份案文文件,我每天用一份姓名清单,并用其他信息为我们的用户使用。 这一档案每天都有变化,有时有两行(划界),其他有100加权。

档案中的数据分6-9栏。 我用了一张纸张,在表格上使用分机功能,但我讨论的问题是,如果我投了a<>m>,其中5栏,然后添加第二栏b>>>>>>,其中6栏是所有数据。

我无法指出,如何让Perl看到,只有5列数据,并继续从这个角度对文本进行分类。 它仍在继续,但产出细微划线。 我如何处理这个问题? 我希望这一点是有意义的。

最佳回答

你们必须张贴一些法典,可能还有一些样本数据,但这里的法典是不加争议地划分不同长度的行文。

<>:

#!/usr/bin/perl
use strict;

while (<STDIN>)
{
    chomp;
    my @info = split("	");
    print join(";", @info), "
";
}

exit;

<>试验文件:

  jsmith  101     777-222-5555    Office 1        Building 1      Manager 
  aposse  104     777-222-5556    Office 2        Building 2      Stock Clerk 
  jbraza  105     777-222-5557    Office 3 
  mcuzui  102     777-222-5557    Office 3        Building 3      Cashier 
  ghines  107     777-222-5557    Office 3

<<>Output>:

%> test.pl < file.txt
jsmith;101;777-222-5555;Office 1;Building 1;Manager
aposse;104;777-222-5556;Office 2;Building 2;Stock Clerk
jbraza;105;777-222-5557;Office 3
mcuzui;102;777-222-5557;Office 3;Building 3;Cashier
ghines;107;777-222-5557;Office 3
问题回答




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...