homer4all has asked for the wisdom of the Perl Monks concerning the following question:

Friends.. I'm newbie in perl and doing this practice test which is I'm not able finish... What I'm trying.. 1. Parse tnsfile and search for only first alias name from the line (first "IF" gets me this i.e. DB1.UK, DB1.EU, DB3.UK, DB3.US, DB5.UK.US) ... this lets me connect to each database But problem is this will also connect same database (SID = ??) multiple times and I don't want to do that Is there anyway to have $hashref store only single alias with distinct SID? Code:
if /^(([A-Za-z][A-Za-z0-9]*)(\.([A-Za-z][A-Za-z0-9]*))*)(\s|,|=)/ { (/\(CONNECT_DATA\s+=\s+\(SID\s+=\s+(\w+\d+?)(\s+)?\)/) { $hashref->{$1}=""; } }
Sample file (tnsfile.txt)
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)) )
Expected $hashref value:
DB1.UK DB3.UK DB5.UK.US

Replies are listed 'Best First'.
Re: Perl parsing file
by Eily (Monsignor) on May 30, 2014 at 16:05 UTC

    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.

      Thank you very much Eily for looking into this and giving me your valuable time.. I will definitely look into this and check the link which you provided. Since I don't have fully functional code and also using tution school inbuilt packages to connect so didn't had much to post.
Re: Perl parsing file
by Anonymous Monk on May 30, 2014 at 15:48 UTC
      Yes you are right... I'm using both forums for my learning and solving doubts... my general tendency is spend couple of hours on single problem or syntax and then go on web instead of spending too much time reinventing wheel. On other site my inital question was different then I changed my practice test question to make logic right.
        It's considered polite to inform about crossposting so people not attending both sites don't waste their time hacking a problem already solved at the other end of the internet.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ