Perl的:需要追加两列,如果ID是重复 [英] Perl : Need to append two columns if the ID's are repeating

查看:192
本文介绍了Perl的:需要追加两列,如果ID是重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果ID重复被我追加APP1,APP2一旦打印。

输入:

  ID |名称| APP1 | APP2
1 | ABC | 234 | 231 |
2 | XYZ | 123 | 215 |
1 | ABC | 265 | 321 |
3 | ASD | 213 | 235 |

输出:

  ID |名称| APP1 | APP2
1 | ABC | 234265 | 231321 |
2 | XYZ | 123 | 215 |
3 | ASD | 213 | 235 |

输出我得到:

  ID |名称| APP1 | APP2
1 | ABC | 234231 |
2 | XYZ | 123215 |
1 | ABC | 265321 |
3 | ASD | 213235 |

我的code:

 #! USR /斌/ perl的
使用严格的;
使用警告;
我的$ BASEDIR ='E:\\ Perl的\\输入\\\\';
我的$文件='doctor.txt';
我的$计数器= 0;
我的%RepeatNumber;
我的$ POS = 0;
打开(OUTFILE,'>','E:\\ Perl的\\输出\\ DoctorOpFile.csv')||死$ !;
开放(FH,'<',加入('',$ BASEDIR,$文件))||死$ !;
我的$行=的ReadLine(FH);
除非($计数器){
    的Chomp $线;
    打印OUTFILE $线;
    打印OUTFILE\\ n;
}
而($行=的ReadLine(FH)){
    的Chomp $线;
    我@obj =拆分('\\ |',$线);
    如果($ RepeatNumber {$物镜[0]} ++){
        我的$ STR1 =加入(|,$ OBJ [0]);
        我的$ STR2 =加入(,,$物镜[2],$物镜[3]);
        打印OUTFILE加盟(|,$ STR1,$ STR2);
        打印OUTFILE\\ n;
    }
}


解决方案

这应该做的伎俩:

 使用严格的;
使用警告;我的$ file_in =doctor.txt;
开(FF,< $ file_in);我的$ TEMP =< FF取代; #删除第一行我走出%;而(小于FF>)
{
    我(的$ id,$名称,$ APP1,APP2 $)=分流/ \\ | /,$ _;
    从$ {$ ID} [0] = $名称;
    推@ {$ {出$ ID} [1]} $ APP1;
    推@ {$ {出$ ID} [2]} $ APP2;
}我的foreach $键(按键%了)
{
    打印$关键,|,$出{$ key}的[0],|,加入(,@ {$出{$ key}的[1]}),|,加入( @ {$ {出来键$} [2]}),\\ n;
}

修改

要看到什么%了包含(如果它是不明确的),你可以使用

 使用数据::自卸车;

并通过

打印

 打印自卸车(%了);

If id gets repeated I am appending app1, app2 and printing it once.

Input:

id|Name|app1|app2    
1|abc|234|231|
2|xyz|123|215|
1|abc|265|321|
3|asd|213|235|

Output:

id|Name|app1|app2
1|abc|234,265|231,321|
2|xyz|123|215|
3|asd|213|235|

Output I'm getting:

id|Name|app1|app2
1|abc|234,231|
2|xyz|123,215|
1|abc|265,321|
3|asd|213,235|

My Code:

#! usr/bin/perl
use strict;
use warnings;
my $basedir = 'E:\Perl\Input\\';
my $file ='doctor.txt';
my $counter = 0;
my %RepeatNumber;
my $pos=0;
open(OUTFILE, '>', 'E:\Perl\Output\DoctorOpFile.csv') || die $!;
open(FH, '<', join('', $basedir, $file)) || die $!;
my $line = readline(FH);
unless ($counter) {
    chomp $line;
    print OUTFILE $line;
    print OUTFILE "\n";
}
while ($line = readline(FH)) {
    chomp $line;
    my @obj = split('\|',$line);
    if($RepeatNumber{$obj[0]}++) {
        my $str1= join("|",$obj[0]);
        my $str2=join(",",$obj[2],$obj[3]);
        print OUTFILE join("|",$str1,$str2);
        print OUTFILE "\n";
    }
}

解决方案

This should do the trick:

use strict;
use warnings;

my $file_in = "doctor.txt";
open (FF, "<$file_in");

my $temp = <FF>; # remove first line

my %out; 

while (<FF>)
{
    my ($id, $Name, $app1, $app2) = split /\|/, $_;
    $out{$id}[0] = $Name;
    push @{$out{$id}[1]}, $app1;
    push @{$out{$id}[2]}, $app2;
}

foreach my $key (keys %out)
{
    print $key, "|", $out{$key}[0], "|", join (",", @{$out{$key}[1]}), "|",     join (",", @{$out{$key}[2]}), "\n";
}

EDIT

To see what the %out contains (in case it's not clear), you can use

use Data::Dumper;

and print it via

print Dumper(%out);

这篇关于Perl的:需要追加两列,如果ID是重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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