"Ashok Varma" schreef:

> ----------------------------------------------------------------------
-------
> open(FH, "/your/file/path");
> my @file = <FH>;
> close FH;
>
> open(FH, ">/your/new/file");
>
> foreach my $line (@file) {

Since you are processing the file line-by-line, you'd better use
"while-on-the-file".

>     if ($line =~ /(N1|N2)(.+)/ig) {

Why the g-modifier? Why the i-modifier?

>         if ($2 =~ /(P1|P2)(.+)/i) {
>             print FH "Field $3 : Value $4\n";

Make that $1 and $2.

>         }
>     }
> }
>
> close FH;
> ----------------------------------------------------------------------
-------

Updating your code, I get to this:

#!/usr/bin/perl
  use warnings ;
  use strict ;

  my $fni = '/your/file/path' ;
  my $fno = '/your/new/file/' ;

  open my $fhi, '<', $fni or die "open '$fni' failed: $!" ;
  open my $fho, '>', $fno or die "open '$fno' failed: $!" ;

  while (<$fhi>)
  {
      /(N1|N2).*?(P1|P2)(.+)/
        and print $fho "Field '$2' : Value '$3'\n" ;
  }
  close $fho ;
  close $fhi ;
__END__

But I assume that OP means (HT|X)ML by his 'structures', so he needs a
proper parser.

-- 
Affijn, Ruud

"Gewoon is een tijger."



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


Reply via email to