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

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

Replies are listed 'Best First'.
Re: How to retrieve element in hash?
by choroba (Bishop) 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};
    Use Data::Dumper to check that your structures really contain what you think.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: How to retrieve element in hash?
by bitingduck (Chaplain) 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 (Sexton) 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 = ""; $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. "" Joe.

Re: How to retrieve element in hash?
by NetWallah (Canon) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1006440]
Approved by Ratazong
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2018-06-25 18:44 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (128 votes). Check out past polls.