0

最初に、私は配列をほとんど扱ったことがなく、ハッシュを扱ったことはありません。また、Perl は私の最強のスクリプト言語ではありません。私はシェルスクリプトのバックグラウンドを持っています。

そうは言っても、私はこれをPerlスクリプトに持っています:

$monitored_paths = { '/svn/test-repo'  => 'http://....list.txt' };

URL は、次のようなパスのリストを含むファイルを指しています。

/src/cpp
/src/test
/src/test2

目的は、URL を次の内容に置き換えることです。

$monitored_paths = {'svn/test-repo' => '/src/cpp', '/src/test', '/src/test2'}

これを達成するための最良の方法は何ですか?ありがとう!

サム

4

4 に答える 4

1

次の行があるため、質問の前提に誤りがあります。

$monitored_paths = {'svn/test-repo' => '/src/cpp', '/src/test', '/src/test2'}

次のいずれかと同等です。

$monitored_paths = {'svn/test-repo' => '/src/cpp', '/src/test' => '/src/test2'}
$monitored_paths = {'svn/test-repo', '/src/cpp', '/src/test', '/src/test2'}

あなたが本当に欲しいのは:

$monitored_paths = {'svn/test-repo' => ['/src/cpp', '/src/test', '/src/test2']}

ここで、[] は配列参照を示します。次のような配列参照を作成します。

my $arrayref = [1, 2, 3]; # called an "anonymous array reference"

またはこのように:

my @array = (1, 2, 3);
my $arrayref = \@array; 

次のようなものが必要です。

$monitored_paths = { '/svn/test-repo'  => 'http://....list.txt' }
foreach my $key (keys %$monitored_paths) {
    next if ref $monitored_paths{$key} eq 'ARRAY'; # skip if done already
    my @paths = get_paths_from_url($key);
    $monitored_paths->{$key} = \@paths; # replace URL with arrayref of paths
}

get_paths_from_url を URL フェッチおよび解析関数に置き換えます (LWP などを使用します...それは実際には質問の一部ではなかったので、その方法は既に知っていると思います)。関数 get_paths_from_url を記述して、最初に配列ではなく配列参照を返す場合は、ステップを保存して$monitored_paths->{$key} = get_paths_from_url($key)代わりに記述できます。

于 2012-06-26T20:09:15.700 に答える
0
use LWP;
foreach (keys %monitored_paths)
{
   my  $content = get($monitored_paths{$_});# Perform error checking
   $monitored_paths_final {$_} = join(",",split("\n",$content));
}
于 2012-06-26T20:07:56.093 に答える
0
use LWP::Simple;
my $content = get($url); ## do some error checking
$monitored_paths = {'svn/test-repo' => [split( "\n", $content)]}
于 2012-06-26T18:56:02.840 に答える
0

ファイルから読み取り、各パスを配列に追加する場合は、次のようにすることができます。

use strictures 1;

my $monitored_paths = {};
open( my $FILE, '<', '/path/to/file' ) or die 'Unable to open file '. $!;
while($FILE){
    push @{ $monitored_paths->{'svn/test-repo'} }, $_;
}
于 2012-06-26T19:00:10.457 に答える