Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6321015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:04:32+00:00 2026-05-24T16:04:32+00:00

In my perl script I’m collecting data and building a hashmap. The hashmap keys

  • 0

In my perl “script” I’m collecting data and building a hashmap. The hashmap keys represent field names, and the values represent the value I want to insert into the corresponding field.

The hashmap is built, and then passed to the saveRecord() method which is supposed to build a SQL query and eventually it will execute it.

The idea here is to update the database once, rather than once per field (there are a lot of fields).

The problem: I’m having trouble passing the hashmap over to the sub and then pulling the fields and values out of the hashmap. At this point my keys and values are blank. I suspect the data is getting lost during the passing to a sub.

The output of the script indicates no keys and no values.

Need help passing the data to the sub in a way that lets me pull it back apart as shown – with join().

Thanks!

Code snippet:

for my $key (keys %oids) {
        $thisField = $key;
        $thisOID = $oids{$thisField};
        # print "loop: thisoid=$thisOID field=$thisField\n";

        # perform the SNMP query.
        $result = getOID ($thisOID);
        # extract the information from the result.
        $thisResult = $result->{$thisOID};

        # remove quotation marks from the data value, replace them with question marks.
        $thisResult =~ s/\"|\'|/\?/g;

        # TODO: instead of printing this information, pass it to a subroutine which writes it to the database (use an update statement).
        # printf "The $thisField for host '%s' is '%s'.\n", $session->hostname(), $result->{$thisOID};

        # add this key/value pair to the mydata hashmap.
        $mydata{$thisField} = $thisResult;

        # print "$thisField=$thisResult\n";
}


# write one record update for hashmap %mydata.
saveRecord (%mydata);


# write all fields to database at once...
sub saveRecord ($) {
        my $refToFields=shift;


        my @fieldlist = keys %$refToFields;
        my @valuelist = values %$refToFields;
        my $sql = sprintf ("INSERT INTO mytable (%s) VALUES (%s)",join(",",@fieldlist), join(",",@valuelist) );

        # Get ID of record with this MAC, if available, so we can perform SQL update
        my $recid = getidbymac ($MAC);

        print "sql=$sql\n";
    # TODO: use an insert or an update based on whether recid was available...
        # TODO: ID available, update the record
        # TODO: ID not available, insert record let ID be auto assigned.
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:04:34+00:00Added an answer on May 24, 2026 at 4:04 pm

    I cleaned up your code a little. Your main problem was not using a reference when calling your sub. Also note the commented regex which is cleaned up:

    Code:

    use strict;
    use warnings;
    
    # $thisResult =~ s/["']+/?/g;
    my %mydata = ( 'field1' => 12, 'field2' => 34, );
    
    saveRecord (\%mydata); # <-- Note the added backslash
    
    sub saveRecord {
        my $ref = shift;
        my $sql = sprintf "INSERT INTO mytable (%s) VALUES (%s)",
            join(',', keys %$ref),
            join(',', values %$ref);
        print "sql=$sql\n";
    }
    

    Output:

    sql=INSERT INTO mytable (field1,field2) VALUES (12,34)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Perl script and would like to use a n-ary tree data
I am writing a Perl script which does this : Reads the values of
I have a Perl script which uses a not-so-common module, and I want it
I have a Perl script to analyze many megabytes of data line-by-line. As an
I am writing a perl script. I want to filter out lines that do
I have a perl script that processes millions of lines of performance data, so
I have a perl script which calls another perl script using backticks. I want
I have a Perl script that I'm attempting to set up using Perl Threads
I am using a perl script to POST to Google Appengine application. I post
I have a Perl script where I maintain a very simple cache using a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.