vigneshwara balaji wrote:
>
> Thank you for that input. I was also enable to successfully complete the
> same with the below script. (its a mix up from various scripts :D)

You must always

  use strict;
  use warnings;

at the start of your code, and declare all variables with 'my'. That way a lot
of simple problems will be revealed.

> $file1 ="XXXX";  # input file
> $file2="XXXX.new";

my $file1 = 'XXXX';
my $file2 = 'XXXX.new';

(These should really be called something more meaningful)

> my $flag=0;   #output file
> open(INFO,"$file1");  # Open the first file

You shouldn't put double-quotes around scalar variables unless you know exactly
what it does. It is best to use lexical file handles and the three-parameter
form of open, and you should always check whether an file open has been 
successful.

  open my $info, '<', $file1 or die $!;

> open(OUT,">$file2");            # open the second file

  open my $out, '>', $file2 or die $!;

> my $t2="/resource-ref>";

There is no need to escape the forward-slash in this string.

> chomp ($t2);

$t2 is a constant value that doesn't end with a newline, so why chomp it?

> while (my $line=<INFO>)
> {
>  chomp($line1);

There is no such variable as $line1. If you had used 'strict' as above this
mistake would have shown up as an error.

>   
>  if ($line =~ /<resource-ref/ and $line =~ /DataSource_/ and $flag==0)
>  {
>   print "$line\n";

  $line hasn't been chomped, so your output will be followed by two newlines.

>   #print OUT "$line";
>   $flag=1;
>  
>  }elsif ($line =~ /$t2/  and $flag==1)
>  {
>   $flag=0;
>   
>  }elsif ( $flag==0)
>  {
>   
>   print OUT "$line";

You shouldn't put double-quotes around scalar variables unless you know exactly
what it does.

>  }
> }
> 
> close(INFO);
> close(OUT);
> 
> Depending on what i give as param for t1 and t2 i will be able to
> manipulate what i want to delete.

There is no $t1.

> I am just posting this as there may be a possibility someone may not be
> able to use the libraries.

You should use a proper XML library unless you are absolutely sure that your XML
input is consistently formed. Your source data would still be valid XML if it
was written like this instead

<resource-ref
    id="ResourceRef_LSSimulator_DataSource_3">
  <res-ref-name>java:/LockTestDb</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Application</res-auth>
  <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

etc.

and while an XML library would handle this correctly your program would fail.

Meanwhile, how about a simpler solution like mine below, which I think is much
more readable.

HTH,

Rob




use strict;
use warnings;

my $source = 'XXXX';
my $new = 'XXXX.new';

open my $in, '<', $source or die $!;
open my $out, '>', $new or die $!;

my $t2 = '/resource-ref>';

while (my $line = <$in>) {

  if ($line =~ /<resource-ref\s/ and $line =~ /DataSource_/) {
    my $line;
    do { $line = <$in> } until $line =~ /$t2/;
  }
  else {
    print $out $line;
  }
}


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to