我有一个函数将Excel数据提取到一个哈希数组中,如下所示:
sub set_exceldata {
my $excel_file_or = '.\Excel\ORDERS.csv';
if (-e $excel_file_or) {
open (EXCEL_OR, $excel_file_or) || die("\n can't open $excel_file_or: $!\n");
while () {
chomp;
my ( $id, $date, $product, $batchid, $address, $cost ) = split ",";
my %a = ( id => $id
, date => $date
, product => $product
, batchid => $batchid
, address => $address
, cost => $cost
);
push ( @array_data_or, \%a );
}
close EXCEL_OR;
}
}
Run Code Online (Sandbox Code Playgroud)
填充哈希数组是很好的.但是,困难的部分是在数组中搜索特定项(哈希).我似乎无法找到可能具有id或21,或batchid为15或成本> $ 20等的项目.
我将如何实施这样的搜索工具?
谢谢大家,
在Perl中,我该如何获得:
$VAR1 = { '999' => { '998' => [ '908', '906', '0', '998', '907' ] } };
$VAR1 = { '999' => { '991' => [ '913', '920', '918', '998', '916', '919', '917', '915', '912', '914' ] } };
$VAR1 = { '999' => { '996' => [] } };
$VAR1 = { '999' => { '995' => [] } };
$VAR1 = { '999' => { '994' => [] } };
$VAR1 = { '999' => { '993' …Run Code Online (Sandbox Code Playgroud) 天儿真好,
我正在努力从很多小哈希创造大哈希.假设这些较小的哈希值在每个文件中定义,然后可以包含在较大的哈希值中.
例如,让我们看一些小哈希
档案personcontact.pl:
return {
\'firstname\' => {
\'__type\' => \'String\'
},
\'lastname\' => {
\'__type\' => \'String\'
},
%{include("/tmp/address.pl")}
}
Run Code Online (Sandbox Code Playgroud)
档案address.pl:
return {
\'address\' => {
\'street\' => {
\'__type\' => \'String\'
},
\'unit\' => {
\'__type\' => \'String\',
\'__validation_function\' => {
\'is_a_number\' => \'\'
},
\'__schema_constraints\' => {
\'is_not_null\' => \'\'
}
},
\'suburb\' => {
\'__type\' => \'String\'
},
\'__type\' => \'ARRAY\'
}
}
Run Code Online (Sandbox Code Playgroud)
而且我有相当多的......
我试图重新创建哈希的方式是使用include子程序,如下所示:
sub include {
my …Run Code Online (Sandbox Code Playgroud) 我有这个代码有效
my @new = keys %h1;
my @old = keys %h2;
function(\@new, \@old);
Run Code Online (Sandbox Code Playgroud)
但是可以在不必先声明变量的情况下完成吗?
function必须将其参数作为参考.
我有一堆以下形式的数据文件:
("String"
:tag1 (value)
:tag2 (value2)
:tag3 (
:nested_tag1 (foo)
:nested_tag2 (
:nested2_tag1 (
: ( nested3_tag1
:baz (true)
:qux ("a really long block of text")
)
)
)
)
)
Run Code Online (Sandbox Code Playgroud)
这只是一个小例子.真实文件有数千行.
原谅我的无知,但我不承认格式.这是一种常见或已知的格式吗?它有名字吗?
我想用Perl处理它,并想知道是否有任何外部模块可以让我轻松将其转换为Perl数据结构 - 我不必自己编写Parser.毕竟,为什么重新发明轮子!;-)
我正在尝试使用标头实现多维表.
这是2D的一个例子:
< dimension1 >
/\ 'column0' 'column1'
dimension0 'row0' data00 data10
\/ 'row1' data01 data11
Run Code Online (Sandbox Code Playgroud)
行和列的标题是文本,数据是任何内容.我希望能够做到这样的事情(语法可以不同,我是Perl的初学者):
my $table = new table(2); # 2 is the number of dimensions
# the following line creates a new row/column if it didn't exist previously
$table['row0']['column0'] = data00;
$table['row0']['column1'] = data01;
$table['row1']['column0'] = data10;
$table['row1']['column1'] = data11;
# the following line returns the headers of the specified dimension
$table->headers(0);
=> ('row0', 'row1')
Run Code Online (Sandbox Code Playgroud)
第一个问题:在CPAN中是否已经完成了这样的事情?(在你问我搜索了大量的时间之前,我没有找到类似的东西)
第二个问题:这是我的尝试,我知道它很难看,可能是错的.任何Perl专家都在考虑审查我的代码?
package table;
sub …Run Code Online (Sandbox Code Playgroud) 我正在构建一组数组哈希
my @array = (
{label => 'first hash'},
{label => 'second hash',
innerarray => [
{label => 'first inner hash'},
{label => 'second inner hash'},
]
},
);
Run Code Online (Sandbox Code Playgroud)
有没有办法只在条件满了时才添加第二个内部哈希?像这样的东西:
my @array = (
{label => 'first hash'},
{label => 'second hash',
innerarray => [
{label => 'first inner hash'},
{label => 'second inner hash'} if 1==1,
]
},
);
Run Code Online (Sandbox Code Playgroud)
我尝试使用push重写代码:
my @innerarray = ();
push @innerarray, {label => 'first inner hash'};
push @innerarray, {label => 'second inner hash'} if …Run Code Online (Sandbox Code Playgroud) 假设我有两个看起来像这样的数组:
('1', '6', '8', '4', '5')
('a', 'c', 'd', 'f', 'w')
Run Code Online (Sandbox Code Playgroud)
我想对第一个数组进行排序,第二个数组中元素的顺序应该与第一个数组的顺序相同,所以两者的顺序如下:
('1', '4', '5', '6', '8')
('a', 'f', 'w', 'c', 'd')
Run Code Online (Sandbox Code Playgroud)
在Perl中如何做到这一点的任何想法?
我有一组可变大小的字符串,例如:
AAA23
AB1D1
A1BC
AAB212
我的目标是按字母顺序排列并为COLUMNS收集独特的字符,例如:
第一栏:AAAA
第二栏:AB1A
等等...
在这一刻,我能够通过散列哈希来提取帖子.但是现在,我该如何对数据进行排序?我可以为每个散列哈希创建一个新数组吗?
非常感谢你的帮助!
人
我的代码:
#!/usr/bin/perl
use strict;
use warnings;
my @sessions = (
"AAAA",
"AAAC",
"ABAB",
"ABAD"
);
my $length_max = 0;
my $length_tmp = 0;
my %columns;
foreach my $string (@sessions){
my $l = length($string);
if ($l > $length_tmp){
$length_max = $l;
}
}
print "max legth : $length_max\n\n";
my $n = 1;
foreach my $string (@sessions){
my @ch = split("",$string);
for my $col (1..$length_max){
$columns{$n}{$col} = $ch[$col-1];
}
$n++; …Run Code Online (Sandbox Code Playgroud) 我试图序列化散列哈希值,然后反序列化它以获取哈希的原始散列.问题是每当我反序列化它时..附加一个自动生成的$ var1例如.
原始哈希
%hash=(flintstones => {
husband => "fred",
pal => "barney",
},
jetsons => {
husband => "george",
wife => "jane",
"his boy" => "elroy",
},
);
Run Code Online (Sandbox Code Playgroud)
出现在$ VAR1 = {'simpsons'=> {'kid'=>'bart','wife'=>'marge','husband'=>'homer'},'flintstones'=> {'丈夫' >'fred','pal'=>'barney'},};
有没有什么办法可以得到没有$ var1的哈希的原始哈希.. ??
perl ×10
hash ×5
arrays ×4
cpan ×1
if-statement ×1
merge ×1
perl-hash ×1
perl-module ×1
reference ×1
sorting ×1