Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Multi replace in a Line

by gurpreetsingh13 (Scribe)
on Jun 20, 2013 at 10:33 UTC ( #1039938=note: print w/ replies, xml ) Need Help??


in reply to Multi replace in a Line

Could not understand properly. Here is your code, with just print statements commented. I am getting correct output for all enteries, for all lines.

use strict; use warnings; my @input = ( "Figure <xref ref-type=\"fig\" rid=\"F1\">1</xref> Figure <xref re +f-type=\"fig\" rid=\"F1\">1</xref>", "Figure <xref ref-type=\"fig\" rid=\"F2\">2</xref>", "Figure <xref ref-type=\"fig\" rid=\"F3\">3</xref>" ); my $isize = scalar(@input); my ($same , $dx1 , $dx2); for (my $il = 0; $il < $isize; $il++ ) { $input[$il] =~ s/<xref ref-type="fig" rid=/\\protect\\customizeref +{/g; if ( $input[$il] =~ /\\protect\\customizeref{(.*?)<\/xref>/ ) { # $input[$il] =~ /<xref ref-type=" fig " rid=" ( . *? ) < \/xr +ef>/; #print("$1\n"); $_ = $1; $same = $1; $dx1 = /"(.*)"/; $dx1 = $1; $dx2 = />(.*)/; $dx2 = $1; #print("$dx1\n"); #print("$dx2\n"); $input[$il] =~ s/\\protect\\customizeref{$same<\/xref>/\\protect\\customizere +f{$dx1}{Figures}{}/g; $input[$il] =~ s/$same/$dx1}{Figures}{}/g; } if ( ( defined $same ) and ( defined $dx1 ) ) { #print(">$same\n"); $input[$il] =~ s/$same<\/xref>/$dx1}{Figures}{}/g; } print $input[$il], "\n"; }
Here is the output. Let me know if it is correct.
Figure \protect\customizeref{F1}{Figures}{} Figure \protect\customizer +ef{F1}{Figures}{} Figure \protect\customizeref{F2}{Figures}{} Figure \protect\customizeref{F3}{Figures}{}


Comment on Re: Multi replace in a Line
Select or Download Code
Re^2: Multi replace in a Line
by veera (Initiate) on Jun 20, 2013 at 10:54 UTC

    thanks for your reply!, I need to find and replace the data in a single line. so our match can be anywhere in the line of the array

      That is what I have done in first line of array. Multiple entries of the same data.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1039938]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2014-10-02 03:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (45 votes), past polls