在Perl

时间:2019-02-10 22:02:28

标签: perl csv

我写了这个漂亮的Perl脚本来为广播电台的自动化系统生成一个.cue文件。我写它的第一天就可以了,一个星期后就不起作用了。

以下是其工作方式的伪代码:

  1. 调用脚本并在启动时分配以下变量作为参数: -导入到数组中的CSV文件的名称 -MP3文件的名称

  2. 我声明将逗号分隔的值分配给的变量的名称

  3. 打开文件

  4. 在while循环中,我遍历CSV的每一行并将每个逗号分隔的值分配给一个变量。在进行下一次迭代之前,我在屏幕上打印.cue文件的输出

  5. 遍历while循环,直到处理完CSV的所有行

这是问题所在,在我的CSV中,我的最后一个字段的值包含一个冒号,而Perl认为它是一个运算符。这是示例行:

没人喜欢你,蝎子,停电,13:38:00

运行脚本时,出现以下错误: part1.csv第4行的“ 13:”附近出现语法错误

这是我的问题:如何将包含:运算符的字符串分配给变量?看起来,当Perl看到其中包含冒号的字符串时,它认为它需要对:做一些事情,而不是仅仅将其传递给变量。

我正在Windows 10上运行活动状态perl。这是我的

#!perl
use strict;
use warnings;


my $file = $ARGV[0] or die "Need to get CSV file on the command line\n";
my $mp3Filename = $ARGV[1] or die "Need the MP3 File Name\n";
my $title;
my $performer;
my $remAlbum;
my $myTrackIndex;
my $line;
my $data;
my @fields;
my $track = 1;
#FILE "show1.mp3" WAVE
print "FILE \"$ARGV[1]\" WAVE\n";
open($data, '<', $file) or die "Could not open '$file' $!\n";

while (my $line = <$data>) {
chomp $line;
my @fields = split "," , $line;
$title = $fields[0];
$performer = $fields[1];
$remAlbum = $fields[2];
$myTrackIndex = $fields[3];
print "TRACK 0$track AUDIO\n";
print "TITLE \"$title\"\n";
print "PERFORMER \"$performer\"\n";
print "REM ALBUM \"$remAlbum\"\n";
print "INDEX 01 $myTrackIndex\n";
$track++;
#TRACK 01 AUDIO
#TITLE "Heavy Metal Halloween - Toccata And Fugue"
#PERFORMER "Heavy Metal Halloween"
#REM ALBUM "Heavy Metal Halloween"
#INDEX 01 00:02:00



}

这是我遇到的错误:

Semicolon seems to be missing at part1.csv line 1.
 at part1.csv line 1.
Semicolon seems to be missing at part1.csv line 2.
 at part1.csv line 2.
Semicolon seems to be missing at part1.csv line 3.
 at part1.csv line 3.
Semicolon seems to be missing at part1.csv line 4.
 at part1.csv line 4.
Semicolon seems to be missing at part1.csv line 5.
 at part1.csv line 5.
syntax error at part1.csv line 1, near "00:"
syntax error at part1.csv line 2, near "05:"
Illegal octal digit '9' at part1.csv line 3, at end of line
syntax error at part1.csv line 3, near "09:"
syntax error at part1.csv line 4, near "13:"
syntax error at part1.csv line 5, near "17:"
syntax error at part1.csv line 6, near "24:"
Execution of part1.csv aborted due to compilation errors.
 at part1.csv line 6.

感谢您的耐心配合。自从我编写任何代码以来已经有好几年了。

1 个答案:

答案 0 :(得分:7)

错误消息表明Perl的调用方式存在问题。它正在尝试将part1.csv作为Perl程序运行(而不是运行实际的Perl程序)。

perl -d part1.csv EJHour1-1.mp3

应该是

perl script.pl part1.csv EJHour1-1.mp3

或者只是

script.pl part1.csv EJHour1-1.mp3