嗨,
我有一个看起来像的字符串
/dir/dir1/filename.txt
Run Code Online (Sandbox Code Playgroud)
我想用一些其他名称替换"filename.txt",使"/ dir/dir1"保持原样,所以在替换字符串之后会看起来像
/dir/dir1/newfilename.txt
Run Code Online (Sandbox Code Playgroud)
考虑到我不知道"filename"的值,我怎么能在Perl中使用RegExp呢?
非常感谢
PS:"filename.txt"和"newfilename.txt"用于在询问原始文件名不同的问题时使事情变得简单.
$hash_map{$key}->{$value1} = 1;
Run Code Online (Sandbox Code Playgroud)
我只是perl的初学者,我需要这个表达式的帮助,这个表达式意味着什么?我假设将创建一个新的键/值对,但这里1的含义是什么?
有一些像:
-e /path/to/file or die "file doesn't exist";
Run Code Online (Sandbox Code Playgroud)
有可能做这样的事情:
-p /path/to/pipe or die "not a valid pipe";
Run Code Online (Sandbox Code Playgroud) 我在文件1中有这个文字:
printf ("integer value is %x \n", a);
Run Code Online (Sandbox Code Playgroud)
我想从文件1读取数据并写入文件2.当我到达此特定行时,文件2显示如下:
printf ("integer value is 0 \n", a);
Run Code Online (Sandbox Code Playgroud)
为什么会这样?我怎么能避免这个?
这是我的Perl代码的外观:
while ($line = <$in_fh>) {
printf $out_fh $line;
}
Run Code Online (Sandbox Code Playgroud)
这里,$ in_fh和$ out_fh是进出文件句柄.
我有以下HTML,我试图用R中的gregexpr函数运行正则表达式
<div class=g-unit>
<div class=nwp style=display:inline>
<input type=hidden name=cid value="22144">
<input autocomplete=off class=id-fromdate type=text size=10 name=startdate value="Sep 6, 2013"> -
<input autocomplete=off class=id-todate type=text size=10 name=enddate value="Sep 5, 2014">
<input id=hfs type=submit value=Update style="height:1.9em; margin:0 0 0 0.3em;">
</div>
</div>
</div>
<div id=prices class="gf-table-wrapper sfe-break-bottom-16">
<table class="gf-table historical_price">
<tr class=bb>
<th class="bb lm lft">Date
<th class="rgt bb">Open
<th class="rgt bb">High
<th class="rgt bb">Low
<th class="rgt bb">Close
<th class="rgt bb rm">Volume
<tr>
...
...
</table>
</div>
Run Code Online (Sandbox Code Playgroud)
我试图通过使用以下正则表达式从这个HTML中提取表部分
<table\\s+class="gf-table historical_price">.+<
Run Code Online (Sandbox Code Playgroud)
当我使用perl = …
open(LOG,"logfile.txt") or die "Unable to open $logfile:$!";
print "\n";
while(<$LOG>){
print if /\berror\b/i;
}
close(LOG);
Run Code Online (Sandbox Code Playgroud) 我正在编译这个方法:
#changes the names of the associations for $agentConf
#where the key value pairs in %associationsToChangeDict are the old and new values respectively
sub UpdateConfObjectAssociations{
my($agentConf, %associationsToChangeDict) = @_;
foreach my $association ($agentConf->GetAssociations()) {
if ( grep {$_ eq $association->Name()} keys %associationsToChangeDict) {
my $newValue = %associationsToChangeDict{$association->Name()};
$association->Value($newValue);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是错误消息:
syntax error at D:\Install\AutoDeployScripts\scripts\Perl/.\AI\SiteMinderHelper
.pm line 75, near "%associationsToChangeDict{"
syntax error at D:\Install\AutoDeployScripts\scripts\Perl/.\AI\SiteMinderHelper
.pm line 79, near "}"
Run Code Online (Sandbox Code Playgroud)
任何人都可以看到问题出在哪里?
我有这个输出 Dumper
'group' => {
'1104' => {
'a' => 1
},
'52202' => {
'b' => 1,
'c' => 1
},
'52201' => {
'c' => 1
},
'52200' => {
'c' => 1
}
},
Run Code Online (Sandbox Code Playgroud)
我假设是哈希数组哈希?
我想宣布这个结构是我自己的.
有没有办法做到这一点,所以下次我看到这么复杂的结构时,我可以立刻做到这一点?=)
我在尝试根据散列中的值对哈希的arrayref进行排序时遇到了麻烦,数据结构如下所示:
my %usera = (name => 'tom',
weight=> 10);
my %userb = (name => 'harry',
weight=> 1);
my %userc = (name => 'peter',
weight=> 5);
my $users = [];
push(@$users,\%usera,\%userb,\%userc);
Run Code Online (Sandbox Code Playgroud)
我想按重量降序对结果进行排序,所以它会按照"汤姆,彼得,哈利"的顺序排列,但我没有太多运气.我试过了:
for my $user (sort{ $users->[$a]{'Weight'} <=> $users->[$b]{'Weight'} } @$users){
.....
}
Run Code Online (Sandbox Code Playgroud)
我认为可能有用,但没有爱.
任何的想法?
TIA!
我试图用文件中的另一个相应的数组元素替换许多数组元素,但它需要花费很长时间才能执行.有更简单的方法吗?以下是我的代码:
open( my $in, '<', "Test.txt") or die "cannot open Test.txt $!";
open( my $out, '>', "TestFinal.txt") or die "cannot create TestFinal $!";
while( <$in>)
{
for(my $i=2 ; $i<=$LastRowGlossary; $i++)
{
s/$variable[$i]/$vardescription[$i]/g;
}
for(my $j=2 ; $j<=$LastRowTable; $j++)
{
s/$COVERAGE_TYPE_CODE[$j]/$TCOVERAGE[$j]/g;
s/$CVG_TEST_CRIT_CD[$j]/$TCVG_TEST_CRIT_TYP[$j]/g;
}
print {$out} $_;
}
close $in;
close $out;
Run Code Online (Sandbox Code Playgroud)
请指教.