Two issues:
  1. You can't declare the lexical variables inside the loop. It creates new variables for each iteration, i.e. for each input line, so the values are not preserved. Move the declaration before the while.
  2. After adding a new record to the hash, clear the variables. It's enough to add the following line after the assignment:
    undef $cycLoc14;

#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my %Ho14Loc2GeNm; my ($cycID14, $cycLoc14, $cycNm14); while (<DATA>) { next unless /^(?:UNIQUE-ID|ACCESSION-1|COMMON-NAME)/; if (/^UNIQUE-ID - (GJDZ-[0-9]+)/) { $cycID14 = $1; } elsif (/^COMMON-NAME - (\S+)/) { $cycNm14 = $1; } elsif (/^ACCESSION-1 - (STM14_[0-9]+)/) { $cycLoc14 = $1; } if (defined($cycLoc14)){ $Ho14Loc2GeNm{$cycLoc14} = $cycNm14; undef $cycLoc14; } } print Dumper(\%Ho14Loc2GeNm); __DATA__ # UNIQUE-ID - GJDZ-5046 TYPES - BC-4 TYPES - Unclassified-Genes COMMON-NAME - STM14_5042 ACCESSION-1 - STM14_5042 CENTISOME-POSITION - 90.96536 COMPONENT-OF - CHROMOSOME-1-100 COMPONENT-OF - TUJDZ-2494 COMPONENT-OF - CHROMOSOME-1 LEFT-END-POSITION - 4430254 PRODUCT - GJDZ-5046-MONOMER RIGHT-END-POSITION - 4430427 TRANSCRIPTION-DIRECTION - - // UNIQUE-ID - GJDZ-1101 TYPES - BC-4 TYPES - Unclassified-Genes COMMON-NAME - focA ACCESSION-1 - STM14_1100 CENTISOME-POSITION - 20.85712 COMPONENT-OF - CHROMOSOME-1-23 COMPONENT-OF - TUJDZ-587 COMPONENT-OF - CHROMOSOME-1 LEFT-END-POSITION - 1015797 PRODUCT - GJDZ-1101-MONOMER RIGHT-END-POSITION - 1016774 TRANSCRIPTION-DIRECTION - - //
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

In reply to Re: Parsing line by line by choroba
in thread Parsing line by line by AWallBuilder

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.