Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How to retrieve element in hash?

by MadDogSailor (Initiate)
on Nov 30, 2012 at 09:44 UTC ( #1006440=perlquestion: print w/ replies, xml ) Need Help??
MadDogSailor has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am relatively new to perl and now I am struggling with a very nasty and persistant problem, which already took me more than a day without any progress. No experienced Perl programmers in sight...... My problem: Given this piece of code:
my $recref = $dbh-> selectall_hashref("SELECT * FROM DSA_RECORD WHERE file_name = '$file_name'", 'RECORD_NAME'); die unless $recref;
and the following content of $recref:
.-> HASH(0x110916be8) 'DATA' => HASH(0x110916a98) 'FIELD_DELIMITER' => '<-->' 'FILE_NAME' => 'SCRAMBLETEST2.CSV' 'RECORD_NAME' => 'DATA' 'RECORD_SELECTOR' => '^1' 'SECTION_NAME' => undef
How can I put the contents of FIELD_DELIMITER in a scalar? So in the end I need a scalar ($fieldsep) with '<-->' as contents. My impression was :
$recname='DATA'; $fieldsep = $recref->{$recname}->{'FIELD_DELIMITER'};
but this results in error :
Use of uninitialized value in print .....
Please can someone help me, I am getting frustrated. Thanks in advance

Comment on How to retrieve element in hash?
Select or Download Code
Re: How to retrieve element in hash?
by choroba (Abbot) on Nov 30, 2012 at 10:27 UTC
    Works for me:
    #!/usr/bin/perl use warnings; use strict; my $recref = { DATA => { FIELD_DELIMITER => '<-->', FILE_NAME => 'SCRAMBLETEST2.CSV', RECORD_NAME => 'DATA', RECORD_SELECTOR => '^1', SECTION_NAME => undef, }}; my $recname = 'DATA'; print $recref->{$recname}{FIELD_DELIMITER};
    Output:
    <-->
    Use Data::Dumper to check that your structures really contain what you think.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: How to retrieve element in hash?
by bitingduck (Friar) on Nov 30, 2012 at 11:03 UTC
    It could be a scoping issue--post the continuous chunk of code that encloses the my $recref=... through the assignment and print.
Re: How to retrieve element in hash?
by t_rex_joe (Acolyte) on Nov 30, 2012 at 17:39 UTC

    MD, You may going about using the "field delimiter" in the wrong context. I prefer using "bind columns", this will keep the returned records (fields) more organized. I know you mentioned a "CSV file" however the tables/databases will change your mind with scale.

    use DBI; #### MY SQL $dbhost = "10.1.1.15"; $dbh = undef; $db = "ip_track"; $user = "mysql"; $pass = "mysql_admin"; $connect = "DBI:mysql:database=$db;$dbhost:3306"; #### EO MYSQL $dbh = undef; $query = undef; $sth = undef; $ect = 0; @row = (); $db +ok = 1; $dbh = DBI->connect($connect,$user,$pass, { PrintError => 1, RaiseEr +ror => 0, AutoCommit => 1 }) or warn "DBERR: $DBI::errstr \"" . __LIN +E__ . "\n"; $query = "SELECT cllseen FROM tblipdec where clipdec = ?"; if($debug == 1) { print "QUERY: \"$query\"\n"; } $sth = $dbh->prepare($query); $sth->execute($ipdec); my $mts = undef; $sth->bind_columns(undef, \$mts); while(@row = $sth->fetchrow_array()) { $ect++; } $sth->finish; $dbh- +>disconnect; if($debug == 1) { print "ECT: \"$ect\"\n"; } if($debug == 1) { print "MTS: \"$mts\"\n"; }

    this snippet of "clipdec = ?" is VERY important, this is how you can pass variables directly to the DB. This will help with mitigating sql injection. The book "MySQL and perl for the web" was my intro into programming with DBs. "http://www.kitebird.com/mysql-perl/" Joe.

Re: How to retrieve element in hash?
by NetWallah (Abbot) on Dec 01, 2012 at 18:14 UTC
    The command-line perl debugger is wonderful for exploring data structures - allows you to experiment without causing damage.

    use "perl -d <program>" to start, and it has a 'h' command for help.

    Use CTRL-C freely to kill output, if excessive at any time.

                 "By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."           -Confucius

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1006440]
Approved by Ratazong
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2014-12-18 00:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (41 votes), past polls