in reply to Extract from text file

I started this before the other PMs gave an answer, so this is just another way to do it!

use strict; use warnings; my $ish = $ARGV[0]; if ( ! defined $ish ) { die "1: $!"; } ## It helps to know which d +ie? open (ISH, "<", $ish) || die "2: $!"; open (OUT, ">", "out.txt") || die "3: $!"; open (my $LOG, ">", "log.txt") || die "4: $!"; # $/ = "//"; my $no = 0; our %hash = (); while (<ISH>) { my $var = $_; $no++; print $LOG "$no\t$var"; ## This is to help know that you are readi +ng and what it is chomp($var); if ( $var eq "//" ) { if ( %hash ) { my ( $a, $b, $c, $x ) = Process_Hash(); ## You could do more work on %hash here or move the sub her +e print OUT "$b\t$a\t$c\t$x\n"; } %hash = (); ## Clear %hash for next sequence next; } ## Note: If you data contains real tabs (0x09), then make the '\\' +a '\' my ( $key, $value ) = split(/\\t/,$var ); # print $LOG "\t$var => |$key|$value|\n"; $hash{ $key } = $value; } if ( %hash ) { my ( $a, $b, $c, $x ) = Process_Hash(); ## You could do more work on %hash here or move the sub her +e print OUT "$b\t$a\t$c\t$x\n"; } exit; # optional but I like to see it and be able to search on. ## Process the element of %hash sub Process_Hash { my ( $a, $b, $c, $x ) = ( "", "#", "for-you", "for-you" ); if ( defined $hash{"DEV_STAGE"} ) { $a = $hash{"DEV_STAGE"}; } if ( defined $hash{"PREDICTED_GENE"} ) { $b = $hash{"PREDICTED_GENE"}; } # while (m/\tSTAINED_REGION:\\t(.*?)\tSTAINED_MOL:\t(.*?)\n/g) return( $a, $b, $c, $x ); } 1;

If you start using hashes, you'll find that they save you a lot of code and help solve some very difficult problems. But there is no right or wrong, as long as it works correctly. Also, I left in the script the log that helped me figure what you were trying to do. Using a log will help you generate solid code. Delete or comment out when it's working the way you want. I usually declare a '$Debug' variable with a debug value. Once it works, I just declare

my $Debug = 0; ## 0-Clean 1-light debugging 2- ...

That way, if I working on or adding to the script, I can set $Debug to 4, and have a log of what's going on. (Note: This is my technique and not necessarily what others would do.)

Good Luck!

"Well done is better than well said." - Benjamin Franklin