如何从特定数字中获取值?
让我们说数字是20040819.我想得到最后两位数,即19使用Perl.
我需要一个Perl脚本,它将数字作为输入示例222,它应该输出为二百二十二.
请你能解释一下这种明显不一致的行为:
use strict;
my @a;
print "a" x 2; # this prints: aa
@a = "a" x 2; print @a; # this prints: aa
print ("a") x 2; # this prints: a
@a = ("a") x 2; print @a; # this prints: aa
Run Code Online (Sandbox Code Playgroud)
不应该最后一个打印一个'a'?
编辑:好的,现在这对我来说更有意义:"二进制"x"是重复运算符...在列表上下文中,如果左操作数括在括号中或者是由qw/STRING /形成的列表,它重复了这个清单." perlop得到
这对我来说很清楚(二进制x - 为什么使用二进制这个词?是否有一个否定X?)但无论如何:@a =("a")x 2#似乎在列表上下文中,因为我们有一个数组在开头 - 一个数组不是一个列表,但它包含一个列表,所以我认为我们可能有一个列表上下文,(虽然它们可能是同义词,但不是数组上下文).
我想"左操作数"是("a").(或者是那个或@a).perlop没有说明操作数实际上是什么,查询perldoc.perl.org给出"找不到匹配",谷歌搜索给出"在计算机编程中,操作数是用于描述任何能够被操纵的对象的术语".就像数组一样.
因此左操作数可能会括在括号中,所以它可能会"重复列表".该列表是:("a") x 2
或者是:("a")
如果我们重复,("a") x 2我们会得到("a") x 2 ("a") x 2.这似乎不对.
如果我们键入:print $a[1]我们将得到一个'a',所以"它重复列表"意味着Perl变成("a") …
我使用Spreadsheet :: WriteExcel来创建电子表格报告.
我尝试使用merge_range()函数合并一些单元格.
$ worksheet-> merge_range(3,5,9,4,$ title,$ format); [要么]
$ worksheet-> merge_range('E3:I4',$ title,$ format);
$ worksheet->写( 'F6', "语言",$格式);
它显示如下错误消息
错误:请参阅文档中的merge_range().不能在非合并单元格中使用以前合并的格式
怎么解决这个问题.如果我用write()..那个时候只显示错误.如果我没有使用任何写函数,那么上面的错误信息没有显示,并且单元格被合并.
这是我的情况.从前一个问题我得到了从grep方法获得价值的答案.
while(<READFILE>)
{
my ($dbtest_name) = grep(/@/,@dataRecord);
// Next I insert this value into a mysql database I get the following error message.
$sth->execute($dbtest_name);
}
DBD::mysql::st execute failed: Column 'test' cannot be null
Use of uninitialized value $dbtest_name in print
Run Code Online (Sandbox Code Playgroud)
问题,如何确保来自$ dbtest_name变量的值始终具有值而不是null?
我希望我的数组成为我的新哈希的键.我正在编写一个计算文档中单词出现次数的程序.
my @array = split(" ", $line);
keys my %word_count = @array; #This does nothing
Run Code Online (Sandbox Code Playgroud)
当我逐行读取infile时,这段正在发生.我试图找到一种方法来使用哈希来完成这个项目.单词是键,它们出现的次数是值.但是,这一步特别让我感到困惑.
我有两个包含日期值的 Perl 字符串变量。我想检查 str1 变量日期值是否比 str2 值早 1 天。我该如何检查?如果它在 str2 之前没有 1 天,那么我需要打印一条错误消息。
$str1="20120704"
$str2="20120705
Run Code Online (Sandbox Code Playgroud) sub open_directory {
my $directory = shift @_;
my @files = ();
opendir (my $dh, $directory) or die "Couldn't open dir '$directory' : $!";
my @all_files = readdir $dh;
closedir $dh;
foreach my $files(@all_files){
while($files =~ /\.htm/){
push(@files);
}
}
return @files;
}
Run Code Online (Sandbox Code Playgroud)
错误在代码push(@files);
错误是:
Useless use of push with no values
我想使用正则表达式来处理名称结尾.htm或.html在@files数组中的文件,/\.htm/请帮助我.
假设,如果一个表有一个名为test的列,并且我在内部测试中写了一行"Dear $ name,Hello",其中$ name是一个变量.我需要选择我正在做的这一行
my $test = $dbh->prepare( "select test from testing")
Run Code Online (Sandbox Code Playgroud)
在脚本中我已经分配了$ name ="Joe".现在,我需要将$ name变量替换为脚本中指定的变量名称(即Joe).我试过打印$ test.它打印"亲爱的名字,你好"我怎么能这样做.
我有这个javascript函数:
print <<"EOT";
<script type="text/javascript">
function alertSize() {
var myWidth = 0, myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
//IE 4 compatible
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
window.alert( 'Width = ' …Run Code Online (Sandbox Code Playgroud) perl ×10
regex ×3
numbers ×2
perl-module ×2
arrays ×1
cpan ×1
excel ×1
html ×1
javascript ×1
mysql ×1
spreadsheet ×1
string ×1
substr ×1