2014-10-06 50 views
0

我对Perl很新颖,我仍处于探索/初学者阶段。复制和修改文件以合并其他数据

我有一个是建立了如下的文本文件:

Naam;ISIN;Symbol;Market;Trading Currency\n 
IDI;FR0000051393;IDIP;Euronext Paris;EUR\n 
BETER BED;NL0000339703;BBED;Euronext Amsterdam;EUR\n 
ALCATEL-LUCENT;FR0000130007;ALU;Euronext Paris;EUR\n 
FIPP;FR0000038184;FIPP;Euronext Paris;EUR\n 
... 

用下面的代码,我设法阅读文本文件的哈希/列表。

use strict; 
use warnings; 

my %data; 
my @names; 

my $myFile = "myfile.csv"; 
open(FH, '<', $myFile) or error("Cannot open file ($!)"); 

while (<FH>) { 
    chomp; 
    my @list = split(';'); 
    for (my $i = 0; $i <= $#list; $i++) { 
     if ($. == 1) { 
      $names[$i] = $list[$i]; 
     } else { 
      push @{$data{$names[$i]}}, $list[$i]; 
     } 
    } 
} 
close FH; 

我有以下两个问题。

  1. 我希望创建一个额外的列(散/列表)称为Ticker,这确实大致有以下几种:

    if ($Market eq "Euronext Paris") { 
        $Symbol = "$Symbol.PA"; 
        {print "$Symbol\n"}; 
    } elsif ($Market eq "Euronext Brussels") { 
        $Symbol = "$Symbol.BR"; 
        {print "$Symbol\n"}; 
    } 
    

    等的其他市场。 (注意上面的代码工作,当我将文件读入数组时,但是,我发现很难修改为hases/lists兼容语法。)

  2. 如何导出列名称,符号,市场并以制表符分隔的文本文件?

+2

https://metacpan.org/pod/Text::CSV#SYNOPSIS如果你想做一些实际的工作。 – 2014-10-06 19:06:49

+0

最好创建一个哈希数组(例如,数组中的每个元素都是一个具有行值的散列,就像一个哈希阵列一样......上面的链接模块应该会大大地帮助... – jm666 2014-10-06 19:10:45

+0

'Data :: Dumper' (或CPAN上提供的一个较新的后继程序,例如'Data :: Dump'或'Data :: Dumper :: Streamer')在处理复杂的数据结构时非常有用。 – Slade 2014-10-06 19:13:46

回答

0

由于您对输入文件进行了相当简单的转换,最好的办法是读取输入文件并同时创建输出文件。我已经调整你的脚本来做到这一点,并且在整个代码中对发生的事情进行了评论。请问你是否不明白剧本正在做什么。

#!/usr/bin/perl 
use warnings; 
use strict; 

# here are some tickers (including some I made up) 
# initialise these at the top of your script in a hash 
# if you want to get the abbreviation for, say, 'Euronext Roma', 
# you access it using $tickers{'Euronext Roma'} 
my %tickers = (
    'Euronext Paris' => 'PA', 
    'Euronext Brussels' => 'BR', 
    'Euronext Madrid' => 'MA', 
    'Euronext Roma' => 'RO' 
); 

# set up the input file 
my $infile = "myfile.csv"; 
open my $in, '<', $infile or die "Cannot open file $infile: $!"; 

# this will be our output 
my $outfile = 'stocks_with_tickers.txt'; 
open my $out, '>', $outfile or die "Cannot open file $outfile: $!"; 

# print out the column headers for the new file 
# the function join takes a separator as its first argument, 
# and joins the array of variables following it with that separator 
print { $out } join "\t", "Name", "Symbol", "Market", "Ticker\n"; 

# input file format: 
# Naam;ISIN;Symbol;Market;Trading Currency 
# when we split the line to form an array, we will want the values 
# in array positions 0, 2, 3 (the first array position is 0 in Perl) 

# output file format: 
# Name; Symbol; Market; Ticker 
# we get the ticker by looking for $tickers{ MARKET } 
# 

while (<$in>){ 
    # skip the line unless it contains some alphanumeric characters 
    next unless /\w+/; 
    # $. is the number of the line that we are on. 
    # Skip the first line of the file (the header row) 
    if ($. == 1) { 
     next; 
    } 

    # split up the line 
    my @list = split ";"; 
    # print cols 0, 2, 3 to the output 
    # note that the braces around $out (i.e. { $out }) are to make it 
    # clearer that $out is the file that you're printing the results in 
    print { $out } join "\t", $list[0], $list[2], $list[3], 

    # we're going to use the ternary form of if/else, which allows 
    # us to do an in/else test within the print statement 
    # this line tests if $tickers{ MARKET } exists 
    (exists $tickers{ $list[3] }) 
    # if it does, print out the market symbol plus the ticker abbrev 
    ? $list[2] . "." . $tickers{$list[3]} . "\n" 
    # otherwise, leave it blank 
    : "\n"; 
} 
close $in; 
close $out; 
# we're done! 
+0

非常感谢。我正在非常感兴趣地研究这些代码。熟悉Perl术语对于我来说非常有帮助。 – 2014-10-07 10:45:57

+0

@GaMmeeu对不起,它没有使用任何哈希(好吧,除了'%tickers'!),但我相信未来会有问题需要解决,这将需要哈希。如果你有问题,就问吧。如果答案有帮助,请点击打勾并/或向上滚动以显示该答案。 – 2014-10-07 10:57:05

+1

今天我花了相当一段时间研究代码。事实上,我非常乐于将自己融入其中。良好的学习经验。我开始了解Perl。 – 2014-10-07 21:36:32