下面我用来$size = keys %prices;获取哈希中的条目总数。但是,下面的例子中是否有办法获取价格低于 4 美元的条目数量?
use strict;
my %prices;
# create the hash
$prices{'pizza'} = 12.00;
$prices{'coke'} = 1.25;
$prices{'sandwich'} = 3.00;
$prices{'pie'} = 6.50;
$prices{'coffee'} = 2.50;
$prices{'churro'} = 2.25;
# get the hash size
my $size = keys %prices;
# print the hash size
print "The hash contains $size elements.\n";
Run Code Online (Sandbox Code Playgroud) 我正在使用下面的代码尝试使用python-messaging https://github.com/pmarti/python-messaging/blob/master/doc/tutorial/mms.rst发送彩信.虽然连接似乎顺利但我从mmsc获得以下响应:
PROXY RESPONSE HTTP/1.0 200 OK
content-type: application/vnd.wap.mms-message
content-length: 59
Connection: close
Date: Sat, 05 Jan 2019 16:36:44 GMT
Server: Mavenir Web Application Server
???1234?????,?Failed to handle HTTP request in Mm1Server
Run Code Online (Sandbox Code Playgroud)
有没有人知道问题可能是什么以及如何解决?这是我的代码:
from messaging.mms.message import MMSMessage, MMSMessagePage
mms = MMSMessage()
mms.headers['To'] = '+212XXXXXXX/TYPE=PLMN'
mms.headers['Message-Type'] = 'm-send-req'
mms.headers['Subject'] = 'Test python-messaging.mms'
slide1 = MMSMessagePage()
slide1.add_image('/home/richard/screensaver/TolleConscQte.jpg')
slide1.add_text('This first slide, is a step towards enlightenment.')
slide2 = MMSMessagePage()
slide2.set_duration(4500)
slide2.add_image('/home/richard/screensaver/TollePastALL.jpg', 1500)
slide2.add_text('This second slide is a second step towards enlightenment.', 500, …Run Code Online (Sandbox Code Playgroud) 一旦确定其中包含“$”,如何使下面的字符串进行插值?
use strict;
my $yrs;
my $stg = 'He is $yrs years old.';
if ($stg =~ /[@#\$\%\/]/) {
print "Matched dollar sign.\n";
$yrs = '4';
} else {
print "No Match.\n";
}
print "Year's scalar: $yrs\n";
print $stg . "\n";
Run Code Online (Sandbox Code Playgroud)
我得到:
He is $yrs years old.
Run Code Online (Sandbox Code Playgroud)
我想得到:
He is 4 years old.
Run Code Online (Sandbox Code Playgroud) 虽然标量$quit明显不等于'j',但下面的循环不会停止.为什么不停止?
#!/usr/bin/perl -w
use strict;
my $quit = 'j';
while ($quit eq 'j') {
print "Enter whatever value you want and I bet I still continue.\n";
chomp (my $quit = <STDIN>);
print "quit equals: $quit\n";
}
Run Code Online (Sandbox Code Playgroud) 我试图弄清楚如何计算字符串中的任何和所有非空白字符,以便在下面的示例中我会得到 4 的输出。
my $str = 'A b C $';
my $cnt =~ /\S/g;
print "Char Count: $cnt\n";
Run Code Online (Sandbox Code Playgroud)
这是否有一个Perl的快捷方式来计算的字符串匹配的数量?不回答我的问题。
perl -e 'my $str = "A b C"; my $cnt = () = $str =~ /\./gi; print "Chs: $cnt\n";'
Chs: 0
Run Code Online (Sandbox Code Playgroud)
有人一直想说这个问题是这个问题的重复:是否有 Perl 快捷方式来计算字符串中的匹配数?
但是,另一个问题涉及如何匹配特定字符,在他们的情况下,我认为它是一个点。我查看了许多其他寻求匹配特定字符的示例,但无法让它们匹配除空格之外的所有字符。
这不能回答我的问题。
$string = "ThisXlineXhasXsomeXx'sXinXit";
$count = ($string =~ tr/X//);
print "There are $count X characters in the string";
Run Code Online (Sandbox Code Playgroud)
这个问题:\S、\W、\D 在正则表达式中代表什么?仅定义了各种 Perl 通配符运算符——其中一个(\S 运算符)我试图在我的原始问题中使用但无济于事。然而,它没有演示如何实际使用这些运算符之一来获取字符串中所有非空白字符的计数。
是否可以使下面的示例正常工作,以便通过标量变量存储和调用子例程的名称?
use strict;
use warnings;
sub doit {
my ($who) = @_;
print "Make us some coffee $who!\n";
}
sub sayit {
my ($what) = @_;
print "$what\n";
}
my $action = 'doit';
$action('john');
Run Code Online (Sandbox Code Playgroud) 我有以下字符串,我需要拆分,但我需要保持分隔符与字符串的第二部分.所以我最终得到:
$a = 'State of mind is primary';
$b = '\098\455\098evertyhing else is secondary.-Eckhart Tolle '
Run Code Online (Sandbox Code Playgroud)
我的尝试:
my $string = 'State of mind is primary\098\455\098evertyhing else is secondary. -Eckhart Tolle';
my $separator = '\098\455\098';
my ($a, $b) = split($separator, $string);
print "$a\n";
print "$b\n";
Run Code Online (Sandbox Code Playgroud)
如何通过perl拆分实现这一目标?
我需要在字符串中添加一定数量的零,以便下面的标量$ newstg出现'1000'.
use strict;
my $number_of_zeros = 3;
my $newstg = '1';
$newstg = $newstg . sprintf("%0$number_of_zerosd\n",$subd);
print "Subd: $newstg\n";
Run Code Online (Sandbox Code Playgroud) 我正在尝试按照本教程进行操作:https://cylab.be/blog/92/measure-ambient-temper-with-temper-and-linux让 TEMPer USB 传感器测量环境温度,以便我可以将其合并到一个 Perl 脚本,用于提醒我房间的环境温度。在本教程的示例中,它们转换来自设备的以下数据字节:
Response from device (8 bytes):
80 80 0b 92 4e 20 00 00
Run Code Online (Sandbox Code Playgroud)
到:
In the response, the Bytes 3 and 4 (so 0b 92) indicate the ambient temperature:
0b 92 converted into decimal is 2932
2932 divided by 100 is 29.32 C
Run Code Online (Sandbox Code Playgroud)
有谁知道我如何使用 Perl 将这些字节的数据转换为十进制,从而转换为摄氏度?