Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Perl-如何从分隔的txt文件中读取每一行并对其进行处理_Arrays_Linux_Perl_File_Hash - Fatal编程技术网

Arrays Perl-如何从分隔的txt文件中读取每一行并对其进行处理

Arrays Perl-如何从分隔的txt文件中读取每一行并对其进行处理,arrays,linux,perl,file,hash,Arrays,Linux,Perl,File,Hash,我有一个以“:”分隔的文本文件 它有3个字段 字段-1-->文件名 字段2-->文件的源路径 字段-3-->文件的目标路径 例如 现在我必须将这个文件helloWorld.txt从源路径复制到目标路径 这需要对文本文件中的所有可用行执行 我不确定我正在尝试的是最佳实践。然而,它没有起作用 有人能告诉我最好的方法吗 非常感谢 open FILE, $inputFile or die $!; while(my $file_name=<FILE>) { my

我有一个以“:”分隔的文本文件

它有3个字段

字段-1-->文件名

字段2-->文件的源路径

字段-3-->文件的目标路径

例如

现在我必须将这个文件helloWorld.txt从源路径复制到目标路径

这需要对文本文件中的所有可用行执行

我不确定我正在尝试的是最佳实践。然而,它没有起作用

有人能告诉我最好的方法吗

非常感谢

    open FILE, $inputFile or die $!;
    while(my $file_name=<FILE>)
    {
    my ($tmpvar1, $tmpvar2, $tmpvar3) = split(/:/, $_);
    my $command = "cp ".$tmpvar2. "/". $tmpvar1 $tmpvar3;
    exce $command;
    }
打开文件,$inputFile或die$!;
while(我的$file\u name=)
{
my($tmpvar1,$tmpvar2,$tmpvar3)=拆分(/:/,$);
my$command=“cp”。$tmpvar2./”$tmpvar1$tmpvar3;
EXE$命令;
}

使用有意义的变量名(而不是
$tempvar
)。一旦开始使用它们(
$file\u name
),请确保变量确实包含其名称提示(它不包含)并在任何地方使用它(即,不要拆分
$\u

要复制文件,请使用。它来自5.002版的Perl

缩进代码以提高可读性

不要发布导致语法错误的代码

Scalar found where operator expected at /home/choroba/1.pl line 6, near "$tmpvar1 $tmpvar3"
        (Missing operator before $tmpvar3?)
可能的解决方案:

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

use File::Copy;

open my $IN, '<', $inputFile or die $!;
while (my $line = <$IN>) {
    chomp $line;
    my ($name, $source, $destination) = split /:/, $line;
    copy("$source/$name", "$destination/$name")
        or warn "Copying $name from $source to $destination failed: $!";
}
#/usr/bin/perl
使用警告;
严格使用;
使用文件::复制;
在“
使用strict”中打开我的$IN;
使用警告;
使用文件::复制;

打开我的$file,“最佳做法是使用核心模块操作文件名和文件本身:

!/usr/bin/env perl
严格使用;
使用警告;
使用文件::复制qw(cp);
使用File::Spec::函数;
而(){
咀嚼;
(我的($name,$source,$destination)=拆分/:/)==3
或“第$.:$行上的损坏数据”\un;
-d$destination或die“destination$destination不存在。\n”;
my$src=catfile($source,$name);
cp($src,$destination)或die“无法复制$src->$destination\n”;
}

这不是因为
exce
中的输入错误,是吗?使用有意义的变量名,使用,缩进代码。在
$tmpvar1
$tmpvar3
之间没有运算符。拆分是有效的。我个人会使用Text::CSV(),尤其是允许您通过哈希引用按名称访问字段的函数。谢谢您的评论。但我想知道是否有更好的方法来处理此类任务。例如使用数组/哈希,这可能更简单、更优雅。
tempvar
是个坏名字,但如果您使用meani,则类似如果变量不完整,但有编号,则可能应该使用列表。
#!/usr/bin/perl
use warnings;
use strict;

use File::Copy;

open my $IN, '<', $inputFile or die $!;
while (my $line = <$IN>) {
    chomp $line;
    my ($name, $source, $destination) = split /:/, $line;
    copy("$source/$name", "$destination/$name")
        or warn "Copying $name from $source to $destination failed: $!";
}
use strict;
use warnings;
use File::Copy;

open my $file, "<", $inputFile or die $!;
while( my $line=<$file> )
{
    chomp $line;
    my ($tmpvar1, $tmpvar2, $tmpvar3) = split(/:/, $line);
    copy "$tmpvar2/$tmpvar1", $tmpvar3;
}
close $file;
#!/usr/bin/env perl
use strict;
use warnings;
use File::Copy qw(cp);
use File::Spec::Functions;

while (<>) {
    chomp;
    ( my ( $name, $source, $destination ) = split /:/ ) == 3
        or die "Broken data on line $.:$_\n";
    -d $destination or die "Destination $destination doesn't exist.\n";
    my $src = catfile( $source, $name );
    cp( $src, $destination ) or die "Can't copy $src -> $destination\n";
}