为什么$a成为arrayref?我不是在推动它.
perl -MData::Dumper -e 'use strict; 1 for @$a; print Dumper $a'
$VAR1 = [];
Run Code Online (Sandbox Code Playgroud) 我正在迭代一个纬度键散列的缓存,指向经度/城市的键/值对.我试图找到纬度/经度的近似匹配,这些匹配足够接近已经查找过的并且在散列中.
我是这样做的
foreach my $lat_key ( keys $lookup_cache_latlonhash ) {
if ( ($lat > ($lat_key - .5)) && ($lat < ($lat_key + .5)) ) {
foreach my $lon_key ( keys %{ $lookup_cache_latlonhash->{$lat_key}} ) {
if ( ($lon > ($lon_key - .5)) && ($lon < ($lon_key + .5)) ) {
$country = $$lookup_cache_latlonhash{$lat_key}{$lon_key};
print "Approx match found: $lat_key $lon_key $country\n";
return $country;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
该代码用于在该范围内找到这些lat/lon对.然而,对于每个纬度,它通过使用循环,当它确实发现它在范围内(第一个嵌套条件)时,它将它添加到哈希(大概keys %{ $goog_lookup_cache_latlonhash->{$lat_key}}),这是不希望的,向哈希添加无用/空键:
$VAR1 = {
'37.59' => {},
'37.84' => {},
'37.86' …Run Code Online (Sandbox Code Playgroud) 调用程序时为什么自动生效不起作用?在这种情况下有没有办法禁止它?
#!/usr/bin/env perl
no autovivification;
use Data::Dumper;
sub testsub { }
my $task;
print Dumper($task); # $VAR1 = undef;
my $a = $task->{parent_id};
print Dumper($task); # $VAR1 = undef;
my $b = testsub($task->{parent_id});
print Dumper($task); # $VAR1 = {};
Run Code Online (Sandbox Code Playgroud) 有人可以帮助我理解这个Perl程序的输出:
use Data::Dumper;
my %hash;
$hash{hello} = "foo";
$hash{hello}{world} = "bar";
print $hash{hello} . "\n";
print $hash{hello}{world} . "\n";
print Dumper(\%hash);
Run Code Online (Sandbox Code Playgroud)
并输出:
foo
bar
$VAR1 = {
'hello' => 'foo'
};
Run Code Online (Sandbox Code Playgroud)
"foo"来自哪里?怎么没有翻斗车打印出来?
请注意,如果我交换作业的顺序:
use Data::Dumper;
my %hash;
$hash{hello}{world} = "bar";
$hash{hello} = "foo";
print $hash{hello} . "\n";
print $hash{hello}{world} . "\n";
print Dumper(\%hash);
Run Code Online (Sandbox Code Playgroud)
我的输出是我所期望的:
foo
$VAR1 = {
'hello' => 'foo'
};
Run Code Online (Sandbox Code Playgroud)
编辑:我知道use strict;会抓住这个,但我更感兴趣的是知道如何打印字符串"foo".
我试图使用Perl中定义的函数来检查是否定义了元素.
代码:
$mylist[0][0]="wqeqwe";
$mylist[0][1]="afasf";
$mylist[1][0]="lkkjh";
print scalar(@mylist), "\n";
if (defined($mylist[2][0])){print "TRUE\n";}
print scalar(@mylist), "\n";
Run Code Online (Sandbox Code Playgroud)
产量
2
3
Run Code Online (Sandbox Code Playgroud)
在使用定义函数之前,第一维中有两个元素@myarray.使用定义的函数后,元素数量增加到3.
如何在不添加新元素的情况下使用已定义的函数?
我有以下代码:
$headers;
some_sub( %$headers );
Run Code Online (Sandbox Code Playgroud)
当我打电话时,some_sub我得到一个错误:
不能使用未定义的值作为HASH参考...
但类似的代码不会产生错误:
$headers->{ x };
Run Code Online (Sandbox Code Playgroud)
为什么自动复制在第一个示例中的工作方式与在第二个示例中的工作方式不同?
UPD
我注意到@ThisSuitIsBlackNot.我真的问:
为什么我的$ h; $ h - > {foo}的作品和我的$ h; %$ h没有
UPD
真正的代码:
my $email = Email::Simple->create(()
,header => [()
,To => $address
,From => $cnf->{ from }
,Subject => $subject
,'Content-Type' => 'text/html; charset="utf8"'
,%$headers
]
,body => $body
);
Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个反向的文档索引,因此我需要从集合中的所有独特单词中了解它们发生在哪些文档中以及发生的频率.
我已经使用这个答案,以便两个创建一个嵌套字典.提供的解决方案工作正常,但有一个问题.
首先,我打开文件并列出一个独特的单词列表.这些独特的单词我想要与原始文件进行比较.当存在匹配时,应更新频率计数器并将其值存储在二维数组中.
输出最终应该如下所示:
word1, {doc1 : freq}, {doc2 : freq} <br>
word2, {doc1 : freq}, {doc2 : freq}, {doc3:freq}
etc....
Run Code Online (Sandbox Code Playgroud)
问题是我无法更新字典变量.尝试这样做时,我收到错误:
File "scriptV3.py", line 45, in main
freq = dictionary[keyword][filename] + 1
TypeError: unsupported operand type(s) for +: 'AutoVivification' and 'int'
Run Code Online (Sandbox Code Playgroud)
我想我需要以某种方式将AutoVivification的实例转换为int ....
怎么去?
提前致谢
我的代码:
#!/usr/bin/env python
# encoding: utf-8
import sys
import os
import re
import glob
import string
import sets
class AutoVivification(dict):
"""Implementation of perl's autovivification feature."""
def __getitem__(self, item):
try:
return dict.__getitem__(self, item) …Run Code Online (Sandbox Code Playgroud) 假设我有一个Perl脚本:
my $hash = {};
$hash->{'a'} = {aa => 'b'};
$hash->{'b'} = undef;
for (qw(a b c)) {
if(defined $hash->{$_}->{aa})
{
say "defined $_";
}
else
{
say "undef $_";
}
}
print Dumper $hash;
Run Code Online (Sandbox Code Playgroud)
但是我的输出会自动创建'c',这是我不想要的.
defined a
undef b
undef c
$VAR1 = {
'c' => {},
'a' => {
'aa' => 'b'
},
'b' => {}
};
Run Code Online (Sandbox Code Playgroud)
我的发行版也不允许我禁用自动修复.有没有办法制作一个检查每个级别的子程序?
今天,当我在 Perl 中发现以下行为时,我感到很惊讶:
sub f { die if %{ $_[0] }; 42 }
my %h;
$h{x} ||= f(\%h); # we die. $_[0] references a hash with an 'x' key during f's run-time
Run Code Online (Sandbox Code Playgroud)
相反,在相同的设置下,以下语句的行为有所不同。
$h{x} = $h{x} || f(\%h); # $h{x} is now 42
Run Code Online (Sandbox Code Playgroud)
分配或与分配和逻辑或的组合之间的潜在差异是否记录在某处?
如果这是由于自动激活造成的,那么模块中是否存在错误或缺少功能autovivification,似乎无法检测到此特定构造中的自动激活?
为了实现Ruby哈希的自动生成,可以使用以下类
class AutoHash < Hash
def initialize(*args)
super()
@update, @update_index = args[0][:update], args[0][:update_key] unless
args.empty?
end
def [](k)
if self.has_key?k
super(k)
else
AutoHash.new(:update => self, :update_key => k)
end
end
def []=(k, v)
@update[@update_index] = self if @update and @update_index
super
end
def few(n=0)
Array.new(n) { AutoHash.new }
end
end
Run Code Online (Sandbox Code Playgroud)
该类允许执行以下操作
a = AutoHash.new
a[:a][:b] = 1
p a[:c] # => {} # key :c has not been created
p a # => {:a=>{:b=>1}} # note, that it does not …Run Code Online (Sandbox Code Playgroud)