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


In reply to Re: Extract from text file by flexvault
in thread Extract from text file by mocnii

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.