2014-01-20 97 views
0

所以我有几个问题,以最有效的方式来设置和使用我的数据结构。如何索引二维数组与字符串的索引

首先,我在形式[header1 string1.1 string1.2 .. string1.n header2 string2.1 .. string2.n ...]一维字符串数组。我想建立一个2d数组,其中每个stringX.Y有4个与它相关的整数。

例如

header1 
string1.1 0 0 0 0 
string2.1 0 0 0 0 

header2 
string2.1 0 0 0 0 
... 
  1. 什么是设置此(初始化为0),如果我已经有一维字符串数组的最佳方式?
  2. 我怎样才能索引到二维数组的字符串?

然后,我需要grep多个文件path1/foo* path2/foo*并使用主要master_search_string结合上述字符串索引。然后,我将将其分为4个水桶取决于硬编码字符串我期待(如foo bar baz other

我原来(修改)内核如下:

for item in $list; 
do 
    s1=`grep -P "bucket1:.*$item" $path | wc -l` 
    s2=`grep -P "bucket2:.*$item" $path | wc -l` 
    s3=`grep -P "bucket3:.*$item" $path | wc -l` 
    s4=`grep -P "bucket4:.*$item" $path | wc -l` 
    echo "$item $s1 $s2 $s3 $s4" >> $outFile 
done 
+0

一切不取决于效率,但是,你要* *做与您的数据,一旦你已经把它的东西。你原来的平面字符串阵列听起来并不像是一个有用的选择。请描述您的应用程序并显示一些*真实*数据,以便我们能更好地理解您。 – Borodin

+0

我已经有原来的平面数组,因为我是在一个不同的方式,这是最简单的代码,这样做。我现在想要为它的运行时间构建它。将我的原始脚本的内核添加到问题中。 – Stuart

回答

0

我最终要回原始文件和保持字符串列表分开(例如string1.* string2.*等)。这些是散列,所以我创建了一个哈希引用数组。到存储在哈希从标量各值改变为一个数组我没有(修改为简单起见):

my %full_list; 
@list = \(%string1, %string2, %string3); 
foreach (0 .. $#list) { 
    my $temp_list = $list[$_]; 
    foreach my $key (sort keys %$temp_list){ 
     delete $temp_list->{$key}; 
     $temp_list->{$key} = [0,0,0,0]; #this creates a list reference, but not sure if I am doing this the best way 
     $full_list{$key} = $temp_list->{$key}; #creates a shallow copy so I can index the arrays with either the full_list by index 
    } 
} 

的最后一行代码解决第二个问题。要索引数组,我们可以做$full_list{$key}[0]

** 有可能是在我的代码输入错误,因为它是什么,我实际做的简化版本。