我试图在Perl中创建一个2d数组
我的代码:
my @wordsList=();
my @words=();
for ($id=0; $id<=@language.length; $id++)
{
my $eng = $db->selectall_arrayref("select word from words
left outer join language
on words.languageId = language.languageId
where words.languageId = $id
;");
foreach $eng(@$eng)
{
my($word) = @$eng;
$ref_to_Array->[$id][$word] = @words($id,$word);
}
return $words($id, $word);
}
$wordsList= NextWords();
print $wordsList;
Run Code Online (Sandbox Code Playgroud)
它返回非...因为我想返回2d数组.
更新
我仍然感到困惑,因为当我执行查询时,它给了我一个单词列表,如:
select word from words where language_id = 1
(1=english, 2 - chinese, 3 - french)
Run Code Online (Sandbox Code Playgroud)
我正在使用for循环语言,以便从数据库中获取所有单词
问题是我想循环并希望数组自动添加到2d数组.
我是Perl的新手,我有一个非常简单的问题,但在查阅我的Perl书时我找不到答案.
打印结果时
Dumper($request);
Run Code Online (Sandbox Code Playgroud)
我得到以下结果:
$VAR1 = bless( {
'_protocol' => 'HTTP/1.1',
'_content' => '',
'_uri' => bless( do{\(my $o = 'http://myawesomeserver.org:8081/counter/')}, 'URI::http' ),
'_headers' => bless( {
'user-agent' => 'Mozilla/5.0 (X11; U; Linux i686; en; rv:1.9.0.4) Gecko/20080528 Epiphany/2.22 Firefox/3.0',
'connection' => 'keep-alive',
'cache-control' => 'max-age=0',
'keep-alive' => '300',
'accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'accept-language' => 'en-us,en;q=0.5',
'accept-encoding' => 'gzip,deflate',
'host' => 'localhost:8081',
'accept-charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'
}, 'HTTP::Headers' ),
'_method' => 'GET',
'_handle' => bless( \*Symbol::GEN0, 'FileHandle' )
}, 'HTTP::Server::Simple::Dispatched::Request' );
Run Code Online (Sandbox Code Playgroud)
如何访问'_method'('GET')或'host'('localhost:8081')的值.
我知道这是一个简单的问题,但Perl在开始时有点神秘.
我想构建一堆Perl子程序,它们都具有相同的模板if elsif elsif else,可以根据因子变量做出决定.这是子程序模板的一个例子:
sub get_age{
my $factor=shift;
if ($factor == 1 ){ print "do something" }
elsif ($factor == 2 ){ print "do somthing2" }
elsif ($factor == 3 ){ print "do somthing3" }
elsif ($factor == 4 ){ print "do somthing4" }
else { print "error" }
}
Run Code Online (Sandbox Code Playgroud)
我想知道在Perl上是否有一些设计模式if else用更优雅的解决方案替换条件,如果我需要更改某些条件或删除其中某些条件,将来是否容易维护?
这会在Perl v5.20中引发错误:
use strict;
use warnings;
my @a = (2,3,9);
my %b = map { "number $_" => 2*$_ } @a;
Run Code Online (Sandbox Code Playgroud)
错误:
syntax error at a.pl line 4, near "} @a"
Execution of a.pl aborted due to compilation errors.
Run Code Online (Sandbox Code Playgroud)
这不是:
use strict;
use warnings;
my @a = (2,3,9);
my %b = map { "number ".$_ => 2*$_ } @a;
Run Code Online (Sandbox Code Playgroud)
为什么$_在mapBLOCK 中不允许插值?
我是Perl的新手,我正试图递归地构建一个哈希并且无处可去.我尝试搜索动态构建哈希的教程,但我能找到的只是关于哈希的介绍性文章.如果你指出我正确的方向或建议一篇好文章/教程,我将不胜感激.
我试图从一个文件中读取具有路径的文件
one/two/three
four
five/six/seven/eight
Run Code Online (Sandbox Code Playgroud)
我想建立像哈希一样的哈希
VAR = {
one : {
two : {
three : ""
}
}
four : ""
five : {
six : {
seven : {
eight : ""
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我目前使用的脚本是:
my $finalhash = {};
my @input = <>;
sub constructHash {
my ($hashrf, $line) = @_;
@elements = split(/\//, $line);
if(@elements > 1) {
$hashrf->{shift @elements} = constructHash($hashrf->{$elements[0]}, @elements );
} else {
$hashrf->{shift @elements} = "";
}
return $hashrf; …Run Code Online (Sandbox Code Playgroud) 我有一个$subscribers可能是undef 的标量,引用HASH或引用ARRAY.我已经分配了样本值$VAR1,$VAR2并$VAR3进行了测试.
我只$subscribers对它是ARRAY的引用感兴趣,其中它包含多个值.在其他情况下,我对打印任何东西都不感兴趣(例如$subscribers=$VAR2;
在Perl v5.16.2下,代码似乎运行良好; 但是,当我将它移动到运行Perl v5.8.8的目标机器时,我收到编译错误:
% ./test.pl
Type of arg 1 to keys must be hash (not private variable) at ./test.pl line 23, near "$subscribers) "
Execution of ./test.pl aborted due to compilation errors.
Run Code Online (Sandbox Code Playgroud)
代码如下:
#!/usr/bin/perl -w
use strict;
use warnings;
use Data::Dumper;
my $VAR1 = undef;
my $VAR2 = {'msisdn' => '1234'};
my $VAR3 = [
{'msisdn' => '1111'},
{'msisdn' => '2222'}, …Run Code Online (Sandbox Code Playgroud) 基于我目前对Perl中哈希的理解,我希望这段代码可以打印出"hello world".它没有打印任何东西.
%a=();
%b=();
$b{str} = "hello";
$a{1}=%b;
$b=();
$b{str} = "world";
$a{2}=%b;
print "$a{1}{str} $a{2}{str}";
Run Code Online (Sandbox Code Playgroud)
我假设散列就像一个数组,为什么我不能让散列包含另一个?
我有这样的动态嵌套哈希引用:
my $hash = { 'a' => { 'b' => { 'c' => 'value' } } };
Run Code Online (Sandbox Code Playgroud)
我想通过允许用户输入"abc something"来将c的值设置为'something'.
现在可以像这样获得价值:
my $keys = 'a.b.c';
my $v='something';
my $h = $hash;
foreach my $k(split /\./, $keys) {
$h = $h->{$k};
}
print $h; # "value"
Run Code Online (Sandbox Code Playgroud)
但是我如何设置 key的值c以便$v这样做
print Dumper $hash;
Run Code Online (Sandbox Code Playgroud)
会反映出这种变化吗?$h在foreach循环结束时不是ref,所以改变它不会反映出变化$hash.任何提示如何解决我头上的结?
我制作了这个脚本来检查标量在意外使用'eq'而不是'=='时如何变化,反之亦然.在字符串上使用'=='只会改变,但在数字上使用'eq'会以某种方式改变标量.代码如下:
#!/usr/bin/perl
use strict;
use JSON;
my $str = "123";
my $num = 123;
print "BEFORE:\n";
print "str: ", \$str, " num: ", \$num, "\n";
print to_json({str => $str, num => $num}, {pretty => 1});
if ($str == 23) { }
if ($num eq "123") { }
print "AFTER:\n";
print "str: ", \$str, " num: ", \$num, "\n";
print to_json({str => $str, num => $num}, {pretty => 1});
print "\n";
Run Code Online (Sandbox Code Playgroud)
输出:
BEFORE:
str: SCALAR(0x8010f8) num: SCALAR(0x801050)
{
"num" : 123, …Run Code Online (Sandbox Code Playgroud) 我有一个如下所示的数组引用:
my $strings = [qw(a b c d)];
Run Code Online (Sandbox Code Playgroud)
我想形成所有可能的组合并创建一个数组数组:
my $output = [qw(qw([a],[b],[c],[d],[a,b],[a,c],[a,d],[b,c],[b,d],[c,d], [a,b,c],[a,b,d],[b,c,d],[a,b,c,d]))]
Run Code Online (Sandbox Code Playgroud)
我尝试了什么:
foreach my $n(1..scalar(@array)) {
my $iter = combinations($strings, $n);
while (my $c = $iter->next) {
print "@$c\n";
}
}
Run Code Online (Sandbox Code Playgroud)