我正在学习perl,当我尝试做面向对象时,我遇到了错误,这是代码,Test.pm
#!/usr/bin/perl
package Test;
sub new
{
my $class = shift;
my $self = {
_firstName => shift,
_lastName => shift,
_ssn => shift,
};
# Print all the values just for clarification.
print "First Name is $self->{_firstName}\n";
print "Last Name is $self->{_lastName}\n";
print "SSN is $self->{_ssn}\n";
bless $self, $class;
return $self;
}
sub setFirstName {
my ( $self, $firstName ) = @_;
$self->{_firstName} = $firstName if defined($firstName);
return $self->{_firstName};
}
sub getFirstName {
my( $self ) = @_;
return …Run Code Online (Sandbox Code Playgroud) 我如何grep perl数组中的某些模式并使用sed命令并将输出保存为另一个数组,如下所示
my @modifiedfiles=`echo @files | grep -E '(DataFiles|Pfgas|Startups)' | sed -e 's/.*something//g; s/#.*$//g;'`
Run Code Online (Sandbox Code Playgroud) dict={}
i=["abc","def","ghi","jkl"]
j=[["a","b","c","d"],["q","w","e","r"],["t","y","u","i"]]
for item in i:
dict[item]=[str(j[item])]
print dict
Run Code Online (Sandbox Code Playgroud)
输出应该是这样的
dict={"abc":["a","b","c","d"], "def":["q","w","e","r"] ...}
Run Code Online (Sandbox Code Playgroud)
如何在python中将列表添加到字典中?
我的清单是这样的,
mylist=[ " ","abc","bgt","llko"," ","hhyt"," "," ","iuyt"]
Run Code Online (Sandbox Code Playgroud)
如何删除此列表中包含字符串的所有空格?
这是我的清单和代码:
x=[["hi hello"], ["this is other"],["this"],["something"],["this"],["last element"]]
for line in x:
y=x.index(line)
#code
Run Code Online (Sandbox Code Playgroud)
第一次获得“ this”,它可以正常工作,但是第二次,它仅获得第一个“ this”的索引!
如何在列表中找到第二次出现的字符串?
我想把两个数组中的项目写入文件,比如
@a = ('1', '2', '3')
@b = ('0.1', '0.2', '0.3')
Run Code Online (Sandbox Code Playgroud)
我希望我的输出像这样:
1 0.1
2 0.2
3 0.3
Run Code Online (Sandbox Code Playgroud)
在文件中.
我尝试使用两个foreach循环,这显然是错误的,
foreach my $a (@a) {
foreach my $b (@b) {
print FP "$a $b \n";
}
}
Run Code Online (Sandbox Code Playgroud)
这是错的.如何将多个数组传递给foreachPerl中的循环?
我正在尝试从数据库访问数据并将数据复制到数组.这是我的代码,
$sth = $dbh->prepare("SELECT * FROM abcd WHERE id=100 ");
$sth->execute;
$N=$sth->rows;
print "$N\n";
while (my @row_val = $sth->fetchrow_array()){
my ($uniqid, $time, $current, $id ) = @row_val;
$y[k]=$current;
$k++;
}
for ($k=0;$k<$N;$k++) {
print "$y[k]\t";
}
Run Code Online (Sandbox Code Playgroud)
但它显示所有$ y [k]的相同值.如何将数据从数据库复制到perl中的数组?