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

Hi.. Actually i have a prob in extracting values from a hash table.. What i did was.. converted an huge xml file into hash table using Data::Dumper.. now when i give a parameter to the script is must find the corresponding value it holds from the hash table... the following script i used to convert the xml file into hash table...


#!/usr/bin/perl # use module use XML::Simple; use Data::Dumper; open(FILE,">Asdf.txt") or die "Can't open the file"; open(ASDF,">zxcv.txt"); # create object $xml = new XML::Simple; # read XML file $data = $xml->XMLin("E:\\progs\\my perl scripts\\resources.xml"); # print output %new = Dumper($data); print FILE %new;


can ne one suggest me a script to do the needful.. i tried the following script but it was in vain...

#!/usr/bin/perl # use module use XML::Simple; # create object $xml = new XML::Simple; # read XML file %new = $xml->XMLin("resources.xml"); print "Enter the parameter:"; chomp($name =<stdin>); $db1='cfi-oracle-jdbc'; $db2='CnetdbProvider'; $a='CFIDS'; $b='WIDDS'; $c='FDRDS'; $d='mssr3'; # access XML data if ($name eq $a || $name eq $b || $name eq $c) { print "jndiName of $name is %new->{resources.jdbc.JDBCProvider}->{cfi- +oracle-jdbc}->{factories}->{CFIDS}->{$jndiName}\n"; } elsif($name eq $d || $name eq $db2) { print "HELLO"; }



wat i tried is if the user input is either $a or$b or $c it must go to "%new->{resources.jdbc.JDBCProvider}->{cfi-oracle-jdbc}->{factories}->{CFIDS}->{$jndiName}" and get the value that jndi holds... i know i'm wrong plz suggest an appropriate code...

Thanking you in advance..

Replies are listed 'Best First'.
Re: hash table prob..
by sh1tn (Priest) on Dec 21, 2007 at 13:10 UTC
    You may want to try:
    $new->{resources.jdbc.JDBCProvider}->{cfi-oracle-jdbc}->{factories}->{ +CFIDS}->{$jndiName}

    hash and perlreftut


Re: hash table prob..
by glide (Pilgrim) on Dec 21, 2007 at 13:48 UTC
    Hi,

    I'm a frequent user of the debug .. :-)

    Try the perl -d <your Perl script>, and then, go to the line you need to check, in this case

    c 27
    And now you can see and try all the possible combination, to see what o need. For example: to print all the %new

    x %new
    or just a small part of it
    x $new->{resources.jdbc.JDBCProvider}->{cfi-oracle-jdbc}->{factories}- +>{CFIDS}
    see more about the debug in perldebug
      hi.. thank you for your reply... i tried your code but still i'm not able to get the required result... i want the value that it holds.. your code gives the key and not its value...
Re: hash table prob..
by FunkyMonk (Bishop) on Dec 21, 2007 at 23:34 UTC
    Try using
    use strict; use warnings;

    below the shebang line in all your scripts, it will help you.

    I find Data::Dumper invaluable when trying to decipher complex data structures.