将表单布局转换为perl中的表(csv)布局 [英] Converting form layout to table (csv) layout in perl

查看:176
本文介绍了将表单布局转换为perl中的表(csv)布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入文件

lockquote
inputfile.txt



 名字:George 
年龄:5
性质:好奇
喜欢:香蕉

这就是我所说的表单布局。我试图将其转换为表格布局,CSV。例如:

 名字,年龄,性质,喜欢
乔治,5,好奇,香蕉

因此,我读取文件,分割:和\ n并将值放入散列。然后将这个散列推入一个数组,以便我可以稍后将它们取出。这是我到目前为止所做的。

 #!/ usr / bin / perl 
use strict;

open(MYFILE,'inputfile.txt');
我的@records;
while(< MYFILE>){
chomp;
my%values = split(/ [:\\\
] /,$ _);
push @ records,%values;
}
close(MYFILE);

通过这个,我想 @records = {[name = George], [年龄= 5],[自然=好奇],[喜欢=香蕉]} 将会发生。



每个哈希数组 @records ?当我尝试这样的事情:

  foreach my $ record(@records){
my%record = $ record;
作为我的$ key(键%记录){
printkey:$ key \\\
;




$ b

它将所有令牌一个接一个地输出,而不像预期的那样(只是关键)。

解决方案

我会用稍微不同的方式来做...

  my(@values,@properties); $(< DATA>){
if(/(\ S *)\ s *:\ s *(\ S *)/){
push @values ,$ 1;
推送@properties,$ 2;
}
}
打印连接',',@values;
打印\\\
;
打印加入',',@properties;
打印\\\
;
__DATA__
名称:George
年龄:5
性质:好奇
喜欢:香蕉

...有两个原因。

首先,Perl中的散列是无序的,没有简单的方法将这些属性重新排列回原来的状态。第二,使用正则表达式匹配而不是 split 允许处理不好的数据更容易(在这个代码中,模式非常简单,但是在那里获得验证逻辑是相当容易的)。此外,所有的数据块立即被捕获;你不需要 chomp 或者转换它们。






说到你的原始代码:这一行...

$ p $ push @records,%values;

...必须重新写入...

  push @records,\%values; 

...表示任何可用的东西。请记住,数组在Perl中是扁平化的,当你试图将一个哈希数组推送到数组中时,它将被转换成一个列表(然后这个列表将被附加到该数组中)。

但是,当你必须输出这样一个哈希数组时,你必须通过它的元素的键,然后通过它的元素的值:这是没有必要的,如果你只是把它们分开存储,如我的代码。


I have an input file

inputfile.txt

        name: George
         age: 5
      nature: curious
       likes: banana

This is what I call a form layout. I am trying to convert this in to a table layout, CSV. For instance:

name,age,nature,likes
George,5,curious,banana

So, I read the file, split on ": and \n" and place values in to a hash. Then push that hash in to an array so I can take them out later. Here is what I have done so far.

#!/usr/bin/perl
use strict;

 open (MYFILE, 'inputfile.txt');
 my @records;
 while (<MYFILE>) {
        chomp;
        my %values = split(/[:\n]/,$_);
        push @records,%values;
 }
 close (MYFILE);

By this, I guess @records={[name=George],[age=5],[nature=curious],[likes=banana]} would have happened.

Now, how do I get each hash out of the array @records? when I try something like:

 foreach my $record(@records){
     my %record = $record;
     for my $key(keys %record){
        print "key : $key\n";
     }
 }

It outputs all tokens one after other unlike what is expected (just the key).

解决方案

I would do it in a slightly different way...

my (@values, @properties);
while (<DATA>) {
  if (/(\S*)\s*:\s*(\S*)/) {
    push @values, $1;
    push @properties, $2;
  }
}
print join ',', @values;
print "\n";
print join ',', @properties;
print "\n";
__DATA__
        name: George
         age: 5
      nature: curious
       likes: banana

... for two reasons.

First, a hash in Perl is unordered, and there's no easy way to sort these properties back to their original state. In these cases, using arrays is preferable.

Second, using regex match instead of split allows to deal with bad data easier (in this code, the pattern is quite simple, but it's quite easy to get validation logic there). Besides, all the chunks of data are captured immediately; you don't need to chomp or transform them additionally.


And speaking of your original code: this line...

push @records, %values;

... had to be rewritten into ...

push @records, \%values;

... to mean anything usable. Remember, arrays are flattened in Perl, and when you try to push a hash into array, it'll just be transformed into a list (and then this list will get appended to that array).

Still, when you have to output such an array of hashes, you have to go through keys of its elements, then go through values of its elements: it's not necessary, if you just store these separately, as in my code.

这篇关于将表单布局转换为perl中的表(csv)布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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