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 8579203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:33:43+00:00 2026-06-11T20:33:43+00:00

I have 3 different arrays in Perl (namely A, B and C). Now, I

  • 0

I have 3 different arrays in Perl (namely A, B and C). Now, I have a table in mysql which also has 3 fields.

What I am trying to do is get all the contents of array A in first field of mysql table, contents of array B into second field and so on. I tried doing this using foreach loops but it works fine for the first array but does not insert anything for the second and third array.

The code is used is as below:

foreach my $a (@a) {

    my $sql = "insert into es(a) VALUES(\"$a\")";
    my $sth = $dbh->prepare($sql);
    $sth->execute or die "SQL Error: $DBI::errstr\n";
}


foreach my $b (@b) {

    my $sql = "insert into es(b) VALUES(\"$b\")";
    my $sth = $dbh->prepare($sql);
    $sth->execute or die "SQL Error: $DBI::errstr\n";

}

and similarly for third.
The column a of the table gets populated correctly but there is no data for column b and c in the table. What am I doing wrong.

  • 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-06-11T20:33:45+00:00Added an answer on June 11, 2026 at 8:33 pm

    I guess you’re not very familiar with relational databases. What you’ve done is:

    1. iterate over @a
    2. prepare a new statement for each item in @a
    3. add one line per insert with only the a value in it; that means:
      • you create a dataset for each item of @a
      • the columns b and c are NULL.

    Now you do that for @b and @c likewise. That is not very efficient.

    DBI is designed to help you out here. You should consider the following rules (guidelines):

    • Always use DBI’s quote method or better placeholders. That saves you the hassle of addind quotes yourself.
    • If there’s a loop and several INSERTs or UPDATEs, always prepare your query outside of the loop and just execute it in the loop.

    Let’s take a look at your problem then. I assume @a, @b and @c all have the same number of items, and you want one line per index of @a, @b and @c. So if you have this data:

    my @a = (1, 2, 3);
    my @b = qw(foo bar baz)
    my @c = (999, 998, 997);
    

    My bet is that you want it to look like this in the db:

    a b   c
    1 foo 999
    2 bar 998
    3 baz 997
    

    We therefore need to combine your three INSERTs into one statement. That can be done by iterating over all of them at once. We can use the each_array function from List::MoreUtils to handle the iteration for us. We’ll also add the guidelines from above to the code.

    use List::MoreUtils qw(each_array);
    my $dbh = DBI->connect(); # connect to db here
    
    # prepare the INSERT statement once
    my $sth_insert = $dbh->prepare('INSERT INTO es SET a=?, b=?, c=?')
      or die $dbh->errstr;
    
    # create the array iterator
    my $ea = each_array(@a, @b, @c);
    # iterate over all three arrays step by step
    while ( my ($val_a, $val_b, $val_c) = $ea->() ) {
      $sth_insert->execute($val_a, $val_b, $val_c) or die $dbh->errstr;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have two different arrays and all I can do is check whether
I'm trying to come up with a way combine two arrays that have different
How would I compare two arrays that might have different lengths and get the
I have a foreach in which I have three different arrays. I want to
Hello i have a app which makes 3 wheels from 3 different arrays of
I am trying to compare two arrays that always have different dimensions. eg. arr1
I have two different arrays, one with strings and another with ints. I want
I have two different arrays. One array, a, for a list of people. My
How can I store arrays in single array? e.g. I have four different arrays,
I have 20 different variable-length arrays, each holding a unique value, and I need

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.