首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >复制和修改文件以合并其他数据

复制和修改文件以合并其他数据
EN

Stack Overflow用户
提问于 2014-10-06 19:01:51
回答 1查看 62关注 0票数 0

我是Perl的新手,我仍然处于探索/初学者阶段。

我有一个文本文件,如下所示:

代码语言:javascript
复制
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
...

使用下面的代码,我可以读取散列/列表中的文本文件。

代码语言:javascript
复制
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布鲁塞尔“){ $Symbol = "$Symbol.BR";{print "$Symbol\n"}; 其他市场也是如此。(注意,当我将文件读取到数组中时,上面的代码工作正常,但我发现很难修改与哈希/列表兼容的语法。)
  2. 如何将列:名称、符号、市场和Ticker导出到由标签分隔的文本文件中?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-06 22:13:38

由于您正在对输入文件进行相当简单的转换,最好是读取输入文件并同时创建输出文件。我已经对您的脚本进行了修改,并在整个代码中对所发生的事情进行了注释。如果你不明白剧本在做什么,请问一问。

代码语言:javascript
复制
#!/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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26222994

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档