in reply to Vertical Regex

Not sure how stable is the file structure, maybe you really should write a parser, as suggests JavaFan, but the following example would work on the given input:

use strict; use warnings; open my $fh, "<", "file.frg"; while(<$fh>) { if (/^{FRG/../^}/){ if (/^acc:(\d+)/) { print ">$1\n"; } elsif ( /^seq:/ ) { while(<$fh>) { last if /^\./; print; } } } }

Replies are listed 'Best First'.
Re^2: Vertical Regex
by joomanji (Acolyte) on Jun 04, 2009 at 16:27 UTC
    Thank you very much zwon! Your script work just fine for me. I've made some changes to the code so that I can define what is the input file. But I'm not sure whether this is the correct way of doing it or not. Thank you for showing me how the regex work to match certain text i want. Thank you
    my ($frg) = @ARGV; $frgfile = "$frg"; open(frgfile) or die("Unable to open FRG file"); while(<frgfile>) { if (/^{FRG/../^}/){ if (/^acc:(\d+)/) { print ">$1\n"; } elsif ( /^seq:/ ) { while(<frgfile>) { last if /^\./; print; } } } }
      my ($frg) = @ARGV; $frgfile = "$frg"; open(frgfile) or die("Unable to open FRG file");

      That's very confusing form. It's a good practice to always start your scripts with

      use strict; use warnings;
      this may help you to avoid many problems. In this case you would see some warnings. I'd write this as follows:
      open my $fd, '<', $ARGV[0] or die $!; while (<$fd>) { ...
      Note how you can store filehandle in scalar variable.