It's easier to see what you were trying to do when you provide codes that work. Yours won't even compile (you forgot the parenthesis in the first if, and I suppose you forgot the second if).

A hash key is unique, so if you want something to be unique, put it in a hash key. In your case, since you don't want to erase the previous value each time you find a new one, you have to check if a key already exists. This can be done in perl with the keyword exists: $hashref->{$sid} = $alias unless exists $hashref->{$sid};

If you set the special variable $/ to the empty string, perl will read your input file paragraph by paragraph instead of line by line. This means from "DB1.UK" until the ")" just before the next alias. Maybe you already used that, but since the code you have posted can't even compile, I can't tell.

So with all this (I didn't want to give you a fully fonctionnal solution, but there's not much left to change):

use Data::Dumper; my %result; # Be careful here, I used a hash, not a hashref { local $/ = ""; # make sure the change to $/'s value doesn't leek to +the rest of the program while(<DATA>) # Read the lines at the end like a input file { if (/^([a-z][\w.]+)/i) # a line starting by a letter, followed by +letters, numbers or a dot { my $alias = $1; my $sid = whoops!; # here some code to find the value of $sid $result{$sid} = $alias unless exists $result{$sid}; # keep the +alias unless the sid is already known } } print Dumper \%result; # show result __DATA__ DB1.UK, DB2.UK = ( (ADDRESS = (PROTOCAL = TCP)) (CONNECT_DATA = (SID = db1)) ) DB1.EU, DB2.CH = ( (ADDRESS = (PROTOCAL = TCP)) (CONNECT_DATA = (SID = db1)) ) DB3.UK = ( (ADDRESS = (PROTOCAL = TCP)) (CONNECT_DATA = (SID = db3)) ) DB3.US = ( (ADDRESS = (PROTOCAL = TCP)) (CONNECT_DATA = (SID = db3)) ) DB5.UK.US, DB5.US = ( (ADDRESS = (PROTOCAL = TCP)) (CONNECT_DATA = (SID = db5)) )

When this works, look at values and reverse to see what you can do with that hash.


In reply to Re: Perl parsing file by Eily
in thread Perl parsing file by homer4all

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.