| [reply] [d/l] |
Can I use the code similar to this?
#!/usr/bin/perl
while ($line = <DATA>){
$line =~ s/<cc>([\s\S]+?)<\/cc>/defined ($1) ? "(&amp;|&)" : '
+-'/eg;
print $line;
}
This prints out (&|&).
But replacing of & and & doesnot gets replaced with '-'.
| [reply] [d/l] |
| [reply] [d/l] |
$Name =~ s/-$//g;
//g switch in above regexp doesn't make sense, can be omitted.
$Name_pre =~ s/$Name_pre/$Name/g;
I have been thinking intensively for a moment, but cannot find any example, where it would be better than
$Name_pre = $Name;
What exactly is this code supposed to do? | [reply] [d/l] [select] |
grizzley I couldn't agree more... Corion fixes the original Unmatched ( in regex; marked by <-- HERE in m/fg-&amp;&+(and( <-- HERE we- error, but beyond that i am struggling...
#!/usr/bin/perl
use warnings;
use strict;
while(my $line=<DATA>){
my $name_pre=$line;
my$name=$line;
print "1: name $name * name_pre $name_pre \n";
$name=~ s/&amp;/-/g;
print "2: name $name * name_pre $name_pre \n";
$name=~ s/&/-/g;
print "3: name $name * name_pre $name_pre \n";
$name =~ s/-$//;
print "4: name $name * name_pre $name_pre \n";
$name_pre=~ s/\Q$name_pre\E/\Q$name\E/g;
print "5: name $name * name_pre $name_pre \n";
$name_pre =~ s/\\//g;
print "6: name $name * name_pre $name_pre \n";
}
__DATA__
fg-&amp;&+(and(we-
Prints: 1: name fg-&amp;&+(and(we-
* name_pre fg-&amp;&+(and(we-
2: name fg--&+(and(we-
* name_pre fg-&amp;&+(and(we-
3: name fg---+(and(we-
* name_pre fg-&amp;&+(and(we-
4: name fg---+(and(we
* name_pre fg-&amp;&+(and(we-
5: name fg---+(and(we
* name_pre fg\-\-\-\+\(and\(we\
6: name fg---+(and(we
* name_pre fg---+(and(we
Just a something something...
| [reply] [d/l] [select] |
The actual program code is below:
Open FH,file.txt;
while (<$FH>) {
$arr[$i].="$_";
}
close ($FH);
$text=$arr[$i];
$text =~ /<cc>([\s\S]+?)<\/cc>/;
$Name_pre=$1;
$Name=$1;
$Name=~ s/&amp;/-/g;
$Name=~ s/&/-/g;
$Name =~ s/-$//g;
$st=$Name;
$char=chop($st);
if($char eq '-')
{$Name=~ s/$//g;}
$arr[$i]=~ s/$Name_pre/$Name/g;
Is it the right way of coding. When I execute the program with the
$Name_pre as
fg-&amp;&+(and(we-
the program terminates giving an error at the line
$arr[$i]=~ s/$Name_pre/$Name/g;
as
Unmatched ( in regex; marked by <-- HERE in m/fg-&amp;&+(and(
+<-- HERE we-
/
| [reply] [d/l] [select] |
$arr[$i]=~ s/\Q$Name_pre\E/$Name/g;
True laziness is hard work
| [reply] [d/l] |
Take a look at perlre for more info and the opportunity to learn, but basically you'll either have to escape them using backslashes or use \Q \E to contain your $Name_pre string, though as pointed out above, why are you assigning the value in such an odd way?
you could substitute all ( in a string with the regex
$string =~ s/\(/$cheesesauce/g;
Oh Goddess, look this code needs a lot of work, take a look at the following
open($FH,'<','file.txt');
$arr[$i]=join('',(<$FH>));
close ($FH);
$arr[i] =~ /<cc>([\s\S]+?)<\/cc>/$1/;
$arr[i] =~ s/&amp;/-/g;
$arr[i] =~ s/&/-/g;
$arr[i] =~ s/\(/-/g; # I assume
$arr[i] =~ s/-$//;
if (substr($arr[i],-1,1) eq '-') {
$arr[i]=~ s/-$//g; # I assume you meant, though you've already done
+ it above
}
I'm sure it's far from perfect, but an improvement on your current code nonetheless | [reply] [d/l] [select] |