Hi,
i want to match a string using perl that has got 5 pluses(+++++).
i am using a function for this.
$str1="+++++";
check($str1,"\\+");
sub check{
$str1=$[0];
$str2=$ [1];
if($str1=~m/^$str2{5}$/){
print "Correct.\n";
}else{
print "Wrong..\n";
}
}
But i am getting the output as wrong.
if i use if($str1=~m/^\+{5}$/) this i will get correct output.
Please help me.
Thanks,
Deepak
anbu23
March 28, 2007, 1:53am
2
Code is working fine.
$ cat spt
#!/usr/bin/perl
$str1="+++++";
check($str1,"\\+");
sub check{
$str1=$_[0];
$str2=$_[1];
if($str1=~m/^$str2{5}$/){
print "Correct.\n";
}else{
print "Wrong..\n";
}
}
$ ./spt
Correct.
ya this is working fine.
but if i change the code like using another variable $no.
#!/usr/bin/perl
$str1="+++++";
$no=5;
check($str1,"\\+",$no);
sub check{
$str1=$[0];
$str2=$ [1];
$str3=$_[2];
if($str1=~m/^$str2{$str3}$/){
print "Correct.\n";
}else{
print "Wrong..\n";
}
}
perl test1.pl
Wrong..
Help me please...
Thanks,
Deepak
anbu23
March 28, 2007, 2:33am
4
#!/usr/bin/perl
$str1="+++++";
$no=5;
check($str1,"\\+",$no);
sub check{
$str1=$_[0];
$str2=$_[1]."\{".$_[2]."\}";
if($str1=~m/^$str2$/){
print "Correct.\n";
}else{
print "Wrong..\n";
}
}
What is the need that ' { ' and ' } ' should be escaped as above?
Without that it would work !!!
#! /opt/third-party/bin/perl
sub check {
$first = $_[0];
$character = $_[1] . "{" . $_[2] . "}";
print "yes\n" if( $first =~ m/^$character$/ );
}
$str = "+++++";
$no = 5;
check($str, "\\+", $no);
exit 0
how about this
do_something if m/[+]{5}/