【问题标题】:I need help around file access and modification in Perl我需要有关 Perl 中文件访问和修改的帮助
【发布时间】:2020-10-02 19:17:21
【问题描述】:

我有文件夹“segmentation”,我需要在其中使用“.purseg”文件(x.purseg、y.purseg、z.purseg)。它们是一种文本文件。 它们的形式是:

'0.1 4.5 语音_L1'

'4.7 9.2 Speech_L2'

等等

我还有文件夹音频,其中有“音频”:x.wav、y.wav、z.wav。 每个“.purseg”文件匹配一个“.wav”文件,它们都具有相同的名称。 对于我的脚本,我必须从“.purseg”文件中获取信息,并且基于它我必须从 wav 文件中删除我需要的部分(将扬声器提到为 speech_L2)。我制作了一个脚本,如果我可以工作在同一个文件夹中同时拥有“.purseg”和“.wav”文件,但是因为我正在处理大量数据,所以我需要修复我的脚本才能使用文件夹。这是脚本:

#! /usr/bin/perl -w

use List::MoreUtils qw(uniq);
use File::Path qw(make_path);
use File::Copy "cp";
use warnings;


my $directory = '/home/taurete/Desktop/diar_fem_fin/segmentation/';
opendir (DIR, $directory) or die $!;
    while (my $file = readdir(DIR)) 
    {
    next unless ($file =~ m/\.purseg$/);    
    $file =~ s{\.[^.]+$}{};     
    push (@list1, $file);   
#   print "$file\n";
    }
my $list=@list1;
#   print "$list";
$i=0;
while ($i<$list)
    {
    my $nume1=$list[$i];
    open my $fh, "$nume1.purseg" or die $!;
    my @file_array;
    while (my $line = <$fh>) 
        {
           chomp $line;
           my @line_array = split(/\s+/, $line);
           push (@file_array, \@line_array);
        }
    my @arr=@file_array;
    $cont1=0;
    my $s1= @arr;
    for (my $i=0;$i < $s1;$i++)
        {
        $directory="$nume1";
        make_path($directory);
        if ("speech_L2" eq "$arr[$i][2]")
            {
            my $directory = '/home/taurete/Desktop/data/audio/';
            opendir (DIR, $directory) or die $!;
            $interval = $arr[$i][1] - $arr[$i][0];
            $speakername=$nume1._.$cont1;
            `sox $nume1.wav ./$directory/$speakername.wav trim $arr[$i][0] $interval`;
            $cont1++;   
            }   
        }
    $i++;
    }

这是我得到的:

名称“main::list”只使用了一次:./spkfinal.pl 行可能有错字 23. ./spkfinal.pl 行在连接 (.) 或字符串中使用未初始化的值 $nume1 27. ./spkfinal.pl 没有这样的文件或目录 第 27 行。

【问题讨论】:

    标签: arrays perl file audio


    【解决方案1】:

    要回答您关于Name "main::list" used only once: possible typo at ./spkfinal.pl line 23. 的问题,请更改:

    my $nume1=$list[$i];
    

    到:

    my $nume1=$list1[$i];
    

    您没有数组@list,但您有数组@list1

    我认为这也会清除您随后的警告。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      • 2015-01-15
      • 1970-01-01
      相关资源
      最近更新 更多