texuser74 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I want to find a text and paste it in some other location. e.g. text..... <X-REF REFID="FN001">YYY</XREF> ....text <FTN ID="FN001">XXX</FTN> Here i want to find XXX and place it in YYY's place. I am able to do this once, but in my file i have many ID's like FN001, FN002... FN008. So I want to find all the ID's and place the text to its corresponding X-REF position. Please help me in solving this. Thanks in advance Raj

update (broquaint): title change (was loop)

Replies are listed 'Best First'.
Re: replacing text in specific tags
by Abigail-II (Bishop) on Aug 21, 2003 at 09:42 UTC
    Well, the problem isn't very well defined. All you give is an example. So, I'm taking some liberties on the precise specification of how the "tags" may appear.

    Assuming your text is stored in $_, you can do it in two lines. First line grabs the YYY info, second line substitutes the XXX. You may have to tweak the regexes depending on the precise specification.

    my %data = m!<X-REF REFID="([^"]+)">([^<]+)</XREF>!g; s!<FTN ID="([^"]+)">[^<]+</FTN>!<FTN ID="$1">$data{$1}</FTN>!g;

    Abigail

      That is not an example, it is the actual content which i have to change.

      now I am using the following code, but no output in temp.out.

      open(IN, "<$file_name_xml") || die "\n Can't open file\n $!\n"; open(OUT,">temp.out"); while(<IN>) { my %data = m#<X-REF REFID="([^"]+)">([^<]+)</XREF>#g; s#<FTN ID="([^"]+)">[^<]+</FTN>#<FTN ID="$1">$data{$1}</FTN>#g; print OUT $data; } close(IN); close(OUT);
        That is not an example, it is the actual content which i have to change

        Really? It's just "YYY" that you have to replace with "XXX"? Just those three time the letter "Y"? And your tags are always X-REF followed by exactly a space, then REFID, then a =, then a ", etc? No spaces around the =, no single quotes? Good, then my regexes don't need any work!

        print OUT $data;
        Why are you printing $data? What's in that?

        Furthermore, if you read in the file line by line, you will only do the substitution if both the X-REF and the corresponding FTN are on the same line.

        Abigail

Re: replacing text in specific tags
by TomDLux (Vicar) on Aug 21, 2003 at 19:36 UTC

    You assign to %data and print $data.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA