由于您对输入文件进行了相当简单的转换,最好的办法是读取输入文件并同时创建输出文件。我已经调整你的脚本来做到这一点,并且在整个代码中对发生的事情进行了评论。请问你是否不明白剧本正在做什么。
#!/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!
https://metacpan.org/pod/Text::CSV#SYNOPSIS如果你想做一些实际的工作。 – 2014-10-06 19:06:49
最好创建一个哈希数组(例如,数组中的每个元素都是一个具有行值的散列,就像一个哈希阵列一样......上面的链接模块应该会大大地帮助... – jm666 2014-10-06 19:10:45
'Data :: Dumper' (或CPAN上提供的一个较新的后继程序,例如'Data :: Dump'或'Data :: Dumper :: Streamer')在处理复杂的数据结构时非常有用。 – Slade 2014-10-06 19:13:46