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

I have this sub that finds exactly what I am looking for but, I am looking to delete what it found. Having problems trying to do that. It is deleting the entire file instead. Any idea on how I should delete just what I found from the values on this peace of code and leave the rest of the file alone?
Here is the code:

ub del{ my $filename="test.txt"; my $template_data = "new/".$filename; undef $/; # Slurp mode open(DATA_IN, "$template_data") || print "Can't open output file1: + $template_data\n"; #binmode DATA_IN; $_ = <DATA_IN>; while(/<\?--([^-]*)-([^-]*)-->(.*?)<\/\?--([^-]*)-([^-]*)-->/sg){ $a=$1;$b=$2;$c=$3;$d=$4;$e=$5; if ($a eq $location){ if($b eq $obj_name) { #Code should be here, and it's where I a +m having problems... } } $list=$list. "<?--$a-$b-->$c</?--$d-$e-->\n"; } close DATA_IN; open( DATA_OUT, ">test.new" ) or die "$! +\n"; print DATA_OUT "$list\n"; close DATA_OUT;

Thanks very much!!!

Replies are listed 'Best First'.
Re: Deleting on Text File
by Chady (Priest) on Oct 22, 2002 at 18:34 UTC
    I think there's something not logical in your function:
    let's assume everything else is working... this is how the while loop can look like
    while(/<\?--([^-]*)-([^-]*)-->(.*?)<\/\?--([^-]*)-([^-]*)-->/sg){ # $a=$1;$b=$2;$c=$3;$d=$4;$e=$5; # drop all those a b c d's next if ($1 eq $location && $2 eq $obj_name); $list .= "<?--$1-$2-->$3</?--$4-$5-->\n"; }

    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
      Thank you that works too. But how do I delete this portion?
Re: Deleting on Text File
by admiraln (Acolyte) on Oct 22, 2002 at 19:16 UTC
    Your match loop matches a particular pattern in the file ignoring all other sections. The matched section that you want to remove is identified by a condition (location and obj_name) The problem is that you always add the just matched section to $list. The line adding on to $list should be in your conditional statement.
    unless (($a eq $location) and ($b eq $obj_name)) { $list=$list. "<?--$a-$b-->$c</?--$d-$e-->\n"; }
    This should add to the list except when the condition is true. I would have like to test this code but you did not give any sample data.

    P.S. I noted that your question has a low reputation. I suspect that the lack of sample data may have contributed to it.

    Disclaimer

      Thanks but, that deletes the entire file.
      Here is a sample test.txt data file
      <?--1-News1--> <b>This is option News 1 of 1</b> </?--1-News1--> <?--1-Weather2of1--> <b>This is option Weather 2 of 1</b> </?--1-Weather2of1--> <?--1-Stock3of1--> <b>This is option Stock 3 of 1</b> </?--1-Stock3of1--> <?--2-Second1of2--> <b>Option Second 1 of 2</b> </?--2-Second1of2--> <?--3-Third1of3--> <b>Option Third 1 of 3</b> </?--3-Third1of3-->

      Let's say I want to delete this part of the text data
      <?--1-Stock3of1--> <b>This is option Stock 3 of 1</b> </?--1-Stock3of1-->

      That's my question how I a get rid of that part and leave the rest of the data alone. Thank you!!!!
        Here is a sample program using the code I suggested and the data you supplied.

        The criteria for deletion are the values of the first two extracted fields of your sample record. I hard coded the values for this demo.

        Please note (as mentioned in another node) that if your data contains any kind if html code this technique will fail if the data matches your delimiters.

        $location = "1"; # hard code deletion info $obj_name = "Stock3of1"; undef $/; # Slurp mode $_ = <DATA>; print "-Before=====================================\n"; print "$_\n"; while(/<\?--([^-]*)-([^-]*)-->(.*?)<\/\?--([^-]*)-([^-]*)-->/sg){ $a=$1; $b=$2; $c=$3; $d=$4; $e=$5; # debug output # print "-A--------\n$a\n-B--------\n$b\n-C--------\n$c\n-D------ +--\n$d\n-E--------\n$e\n-eor------\n\n"; unless (($a eq $location) and ($b eq $obj_name)) { $list=$list. "<?--$a-$b-->$c</?--$d-$e-->\n"; } } print "-After======================================\n"; print "$list\n"; __END__ <?--1-News1--> <b>This is option News 1 of 1</b> </?--1-News1--> <?--1-Weather2of1--> <b>This is option Weather 2 of 1</b> </?--1-Weather2of1--> <?--1-Stock3of1--> <b>This is option Stock 3 of 1</b> </?--1-Stock3of1--> <?--2-Second1of2--> <b>Option Second 1 of 2</b> </?--2-Second1of2--> <?--3-Third1of3--> <b>Option Third 1 of 3</b> </?--3-Third1of3-->
        Here are the results.
        -Before====================================== <?--1-News1--> <b>This is option News 1 of 1</b> </?--1-News1--> <?--1-Weather2of1--> <b>This is option Weather 2 of 1</b> </?--1-Weather2of1--> <?--1-Stock3of1--> <b>This is option Stock 3 of 1</b> </?--1-Stock3of1--> <?--2-Second1of2--> <b>Option Second 1 of 2</b> </?--2-Second1of2--> <?--3-Third1of3--> <b>Option Third 1 of 3</b> </?--3-Third1of3-->
        -After====================================== <?--1-News1--> <b>This is option News 1 of 1</b> </?--1-News1--> <?--1-Weather2of1--> <b>This is option Weather 2 of 1</b> </?--1-Weather2of1--> <?--2-Second1of2--> <b>Option Second 1 of 2</b> </?--2-Second1of2--> <?--3-Third1of3--> <b>Option Third 1 of 3</b> </?--3-Third1of3-->
        Disclaimer
Re: Deleting on Text File
by Gordy (Scribe) on Oct 22, 2002 at 23:55 UTC
    This works for the sample data you provided.
    open (FILE, "sample.txt") || die "cannot open sample.txt"; while (<FILE>) { s/<\?--1-Stock3of1-->//; s/<b>This is option Stock 3 of 1<\/b>//; s/<\/\?--1-Stock3of1-->//; push (@words, $_); } close (FILE) || die "cannot close sample.txt"; open (FILE, ">sample.txt") || die "cannot open sample.txt"; foreach (@words) { print FILE; } close (FILE) || die "cannot close sample.txt";
      this sample:
      <?--1-Stock3of1--> <b>This is option Stock 3 of 1</b> </?--1-Stock3of1--> <?--2-Second1of2--> <b>This is option Stock 3 of 1</b> </?--2-Second1of2--> <?--3-Third1of3--> <b>Option Third 1 of 3</b> </?--3-Third1of3-->
      would break under your code, you are assuming that you know the contents of the specific tags... when in fact you couldn't. so you need to strip off based on the tags..maybe HTML::TokeParser can work here..
      He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

      Chady | http://chady.net/