使用Perl读取输入.csv文件,引入数组并提取到预期输出

时间:2017-10-10 07:45:47

标签: arrays perl csv

我收到了使用array uniq,

创建perl脚本的请求

我的@filtered = uniq(@array);

但不知道如何用perl编写代码

任何人都可以帮助我,我很欣赏......

=============================================== =======

1)输入.csv文件:

ATPG         TCD             tcd/T1, xxx/tcd/T2        
ATPG         INSTANCE        inst1/I1, xxx/inst2/I2, inst3/I3   
ATPG         PATTERN         pat1/P1, pat2/P2   
SIM          BLACKBOX        bb1/B1, bb2/B2

2)请求:

第1步:读取输入.csv文件

第2步:使用预期内容创建输出out.txt

=============================================== =======

3)输出out.txt(if(1st column = ATPG))

// TCD

read_core_description     tcd/T1
read_core_description     xxx/tcd/T2

// INSTANCE
add_core_instance  -instance inst1/I1  
add_core_instance  -instance xxx/inst2/I2   
add_core_instance  -instance inst3/I3

//PATTERN 
read_patterns pat1/P1  
read_patterns pat2/P2

=============================================== =======

我目前的代码,请在最后继续

#1) Open input
my $inF="input.csv";
open( my $inF_var, "<", $inF) || die ("Can't open input file\n");

#2) Open output
my $outF_Blk="output/out.txt";

open( my $outF_Blk_var,  ">$outF_Blk") || die ("Can't open output file");


#3) Write to files
while( <$inF_var> ){
next if (/^$/);
my $line=$_;

my @value=split(/:/, $line);

# EXTRACT THE EXPECTED OUTPUT HERE # 
### NEED EVERYONE CAN HELP ####

1 个答案:

答案 0 :(得分:0)

我只实现了一种输出类型。其他应该足够简单,可以添加。

#!/usr/bin/perl

use strict;
use warnings;

use feature 'say';

my %process = (
  TCD      => \&process_tcd,
  INSTANCE => \&process_instance,
  PATTERN  => \&process_pattern,
);

while (<DATA>) {
  my ($type, $cmd, @row) = split /[\s,]+/;

  next unless $type eq 'ATPG';

  if (!exists $process{$cmd}) {
    warn "Unknown record type: $_";
    next;
  }

  $process{$cmd}->(@row);
}

sub process_tcd {
  my @data = @_;

  say "//TCD\n";
  say "read_core_description    $_" for @data;
  say '';
}

sub process_instance {}
sub process_pattern {}

__DATA__
ATPG         TCD             tcd/T1, xxx/tcd/T2        
ATPG         INSTANCE        inst1/I1, xxx/inst2/I2, inst3/I3   
ATPG         PATTERN         pat1/P1, pat2/P2   
SIM          BLACKBOX        bb1/B1, bb2/B2