Array

Discussion in 'Perl Misc' started by Faith Greenwood, May 25, 2009.

  1. Hi,

    Seems like I've been googling for hours and can't find the solution.
    Maybe someone here can help:

    I have 3 arrays, each with 3 items:
    @array1=(1,2,3); #names
    @array2=(4,5,6); #email addresses
    @array3=(7,8,9); #cities

    I need to put the values of each into a database, in order. This is
    the idea I have so far, which is obviously wrong:

    foreach (@array1){
    my $sql = "INSERT INTO example (names,email,city) VALUES (?,?,?)";
    my $sth = $dbh->prepare($sql);
    $sth->execute($array1,$array2,$array3);
    }

    In other words I want to put the array items in the 'example' table so
    that I have the following 3 records in my database:

    ####Record 1
    name: 1, email: 4, city: 7;
    ####Record 2
    name: 2, email: 5, city: 8;
    ####Record 3
    name: 3, email: 6, city: 9;

    How would I do that?
    Faith Greenwood, May 25, 2009
    #1
    1. Advertising

  2. Faith Greenwood <> wrote in news:a2350343-1e6c-4ec7-
    :

    > Seems like I've been googling for hours and can't find the solution.
    > Maybe someone here can help:
    >
    > I have 3 arrays, each with 3 items:
    > @array1=(1,2,3); #names
    > @array2=(4,5,6); #email addresses
    > @array3=(7,8,9); #cities


    Don't do that:

    my @customers = (
    { name => 1, email => 4, city => 7 },
    { name => 2, email => 5, city => 8 },
    { name => 3, email => 6, city => 9 },
    );

    >
    > I need to put the values of each into a database, in order. This is
    > the idea I have so far, which is obviously wrong:
    >
    > foreach (@array1){
    > my $sql = "INSERT INTO example (names,email,city) VALUES (?,?,?)";
    > my $sth = $dbh->prepare($sql);
    > $sth->execute($array1,$array2,$array3);
    > }


    There are no variables $array1, $array2, $array3 in your script.

    Untested code follows:

    use List::MoreUtils qw( each_array );

    my $sql = "INSERT INTO example (names,email,city) VALUES (?,?,?)";
    my $sth = $dbh->prepare($sql);

    my $it = each_array( @array1, @array2, @array3 );
    while ( my ( $name, $email, $city ) = $it->() ) {
    $st->execute( $name, $email, $city );
    }

    In the future, choose better data structures.

    Sinan

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
    A. Sinan Unur, May 25, 2009
    #2
    1. Advertising

  3. Faith Greenwood

    smallpond Guest

    On May 25, 9:39 am, Faith Greenwood <> wrote:
    > Hi,
    >
    > Seems like I've been googling for hours and can't find the solution.
    > Maybe someone here can help:
    >
    > I have 3 arrays, each with 3 items:
    > @array1=(1,2,3);  #names
    > @array2=(4,5,6);  #email addresses
    > @array3=(7,8,9); #cities
    >
    > I need to put the values of each into a database, in order. This is
    > the idea I have so far, which is obviously wrong:
    >
    > foreach (@array1){
    > my $sql = "INSERT INTO example (names,email,city) VALUES (?,?,?)";
    > my $sth = $dbh->prepare($sql);
    > $sth->execute($array1,$array2,$array3);
    >
    > }
    >
    > In other words I want to put the array items in the 'example' table so
    > that I have the following 3 records in my database:
    >
    > ####Record 1
    > name: 1, email: 4, city: 7;
    > ####Record 2
    > name: 2, email: 5, city: 8;
    > ####Record 3
    > name: 3, email: 6, city: 9;
    >
    > How would I do that?


    You need to learn how arrays work. The array
    as a whole is called @array1. The individual
    elements in the array are $array1[0], $array1[1] ...
    $array1 is, confusingly, an unrelated scalar
    variable. That's how perl works.

    You only need to do the prepare once.

    my $sql = "INSERT INTO example (names,email,city) VALUES (?,?,?)";
    my $sth = $dbh->prepare($sql); # error check?

    Then execute 3 times in a for loop:

    for ($i=0; $i<3; $i++) {
    $sth->execute($array1[$i], $array2[$i], $array3[$i]);
    # error check?
    }

    You aren't checking for errors. If you don't care whether
    your call succeeds or not, why do it at all?
    smallpond, May 25, 2009
    #3
  4. On May 25, 10:20 am, smallpond <> wrote:
    > On May 25, 9:39 am, Faith Greenwood <> wrote:
    >
    >
    >
    > > Hi,

    >
    > > Seems like I've been googling for hours and can't find the solution.
    > > Maybe someone here can help:

    >
    > > I have 3 arrays, each with 3 items:
    > > @array1=(1,2,3); #names
    > > @array2=(4,5,6); #email addresses
    > > @array3=(7,8,9); #cities

    >
    > > I need to put the values of each into a database, in order. This is
    > > the idea I have so far, which is obviously wrong:

    >
    > > foreach (@array1){
    > > my $sql = "INSERT INTO example (names,email,city) VALUES (?,?,?)";
    > > my $sth = $dbh->prepare($sql);
    > > $sth->execute($array1,$array2,$array3);

    >
    > > }

    >
    > > In other words I want to put the array items in the 'example' table so
    > > that I have the following 3 records in my database:

    >
    > > ####Record 1
    > > name: 1, email: 4, city: 7;
    > > ####Record 2
    > > name: 2, email: 5, city: 8;
    > > ####Record 3
    > > name: 3, email: 6, city: 9;

    >
    > > How would I do that?

    >
    > You need to learn how arrays work. The array
    > as a whole is called @array1. The individual
    > elements in the array are $array1[0], $array1[1] ...
    > $array1 is, confusingly, an unrelated scalar
    > variable. That's how perl works.
    >
    > You only need to do the prepare once.
    >
    > my $sql = "INSERT INTO example (names,email,city) VALUES (?,?,?)";
    > my $sth = $dbh->prepare($sql); # error check?
    >
    > Then execute 3 times in a for loop:
    >
    > for ($i=0; $i<3; $i++) {
    > $sth->execute($array1[$i], $array2[$i], $array3[$i]);
    > # error check?
    >
    > }
    >
    > You aren't checking for errors. If you don't care whether
    > your call succeeds or not, why do it at all?



    thx Sinan and smallpond. I ended up using smallpond's suggestion as it
    seemed simpler to me. I do have error checking in my code, which is
    >200 lines. For simplicity I only posted the relevant parts of the

    code that pertain to my question.

    thx again guys!
    Faith Greenwood, May 25, 2009
    #4
  5. On 2009-05-25 13:39, Faith Greenwood <> wrote:
    > Seems like I've been googling for hours and can't find the solution.


    "perldoc DBI" almost always provides the right answer to the question
    "how do I ... with DBI".

    > Maybe someone here can help:
    >
    > I have 3 arrays, each with 3 items:
    > @array1=(1,2,3); #names
    > @array2=(4,5,6); #email addresses
    > @array3=(7,8,9); #cities
    >
    > I need to put the values of each into a database, in order. This is
    > the idea I have so far, which is obviously wrong:
    >
    > foreach (@array1){


    You don't need the loop.

    > my $sql = "INSERT INTO example (names,email,city) VALUES (?,?,?)";
    > my $sth = $dbh->prepare($sql);
    > $sth->execute($array1,$array2,$array3);


    $sth->execute_array({}, \@array1, \@array2, \@array3);

    hp
    Peter J. Holzer, May 25, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel
    Replies:
    1
    Views:
    1,050
    Kevin Collins
    Aug 14, 2003
  2. S300
    Replies:
    4
    Views:
    21,993
    Roedy Green
    Aug 19, 2003
  3. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    488
    David RF
    Sep 3, 2009
  4. Tom
    Replies:
    3
    Views:
    208
    salsablr
    Dec 20, 2004
  5. Tuan  Bui
    Replies:
    14
    Views:
    472
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page