简体   繁体   English

如何使用多维数组

[英]How to use Multi Dimensional Array

I have a directory file which contains all the paths for Definition files Test FIlesand Instruction files. 我有一个目录文件,其中包含定义文件,测试文件和指令文件的所有路径。 Definition file looks like 定义文件看起来像

##Definition file

Def 1 : "definition 1"

Def 2 : "definition 2"

Def 3 : "definition 3"

Each Instruction File contains Instruction sets like 每个指令文件都包含指令集,例如

##Instruction File 1
{

Instruction 1

run Def1

sleep

run def2

}
{

Instruction 2

run Def 3

sleep

run Def1

}

likewise multiple instruction files with multiple instruction sets 同样具有多个指令集的多个指令文件

Test File looks like
{

run Instruction file1 Instruction 2

sleep 5

}

Can anyone guide me how to do this. 谁能指导我该怎么做。 Like how to run a specific instruction from an Instruction file 就像如何从指令文件运行特定指令

Can I use multidimensional array to do it like Storing the Commands, Instruction file and Instruction in a multidimensional array? 我可以使用多维数组来做到这一点吗,例如将命令,指令文件和指令存储在多维数组中?

You could use hash refs and array references. 您可以使用哈希引用和数组引用。

The hash could key on the destination and then the array reference could store the array of instructions. 哈希可以键入目标,然后数组引用可以存储指令数组。

$ref->{'key'} = \\@array

To open a file for reading "<" 打开文件以读取“ <”

open FILE, "<", "filename.txt" or die $!

To get all its lines: 要获取其所有行:

my @lines = <FILE>

From here just split up the code based on your instructions, perhaps using a regex. 在这里,您可以根据您的说明拆分代码,也许使用正则表达式。

foreach my $line (@lines) {
    #do stuff here for the instructions
    #maybe a regex
}

Then iterate through and process each one.. 然后遍历并处理每个。

For your test file issue you could store it associatively. 对于测试文件问题,您可以将其关联存储。

So you'd have an array of test file "command groups" 因此,您将拥有一组测试文件“命令组”

Each element could store a hash of 3 associated values 每个元素可以存储3个关联值的哈希

$test[0] = { command => $command , filename => $filename, instruction => $instruction };

From here you could access elements such as: 从这里您可以访问以下元素:

$test[0]->{command}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM