Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.

Dereference an array reference

by fmogavero (Monk)
on Apr 19, 2001 at 00:27 UTC ( [id://73651]=perlquestion: print w/replies, xml ) Need Help??

fmogavero has asked for the wisdom of the Perl Monks concerning the following question:

Brethern and cistern, I petition you. My forehead is much flatter than it was and my monitor is cracked.

I have an object where $self->things is an array. I would like to fill XXXXXXX in the code below with the name of the current $self->things and then do a query on each things name.

{ my $self = shift; my $slobbo = "select * from bleh where whatever = "; my $connection = $self->connection; foreach ($self->things) { my $name = new thang($_); my $thangsname = XXXXXXX my $sth = $connection->connection->prepare( $slobbo.$thangsname ); } }

I have tried everything that I can think of. I have used tye's References Quick Reference, which I found wonderfully enlightening. Every time I try to view that concatenated SQL query, the name of thing is missing.

Please help.

Replies are listed 'Best First'.
Re: Dereference an array reference
by Russ (Deacon) on Apr 19, 2001 at 01:06 UTC
    If $self->things is an array ref, you just need to change your loop:
    foreach (@{$self->things}){...}
    The @{} will dereference your things array and make $_ be each item. So, if $self->things is an array of names, you will replace XXXXX with $_.

    Does this address the problem?

    Brainbench 'Most Valuable Professional' for Perl

(Boo) Re: Dereference an array reference
by boo_radley (Parson) on Apr 19, 2001 at 00:46 UTC
    my $slobbo = "select * from bleh where whatever = "; ... ... my $sth = $connection->connection->prepare($slobbo.$thangsname );
    this is what placeholders were designed to do.
    foreach my $var (@list) $sql = $l_dbh->prepare("INSERT activity (activity_num) VALUES (?)"); @l_sqlparams = ($var); $l_result=$sql->execute (@l_sqlparams) or die 'no';

      This does help to clear things up. But at the same time it leaves a lot of questions.

      I am not trying to insert anything into the database. Where would I put the placeholder in a plain old select statement? || Can you direct me to a node with the answer?

      Does foreach my $var (@list); cause $var to be in list context? || Can you direct me to a node with a good explanation of the differences in context?

      Does @l_sqlparams = ($var); put $var back into an array from list context? || Can you direct me to a node with an explanation?

      Thank You! I wasn't understanding placeholders. Now I just have to figure out why I'm getting two error messages.
      1.) disallowed implicit conversion from datatype 'varchar' to datatype 'int'.
      After attempted adjustment
      2.) syntax error converting CHAR value 'ARRAY(XXXXXXX)' to an INT4 field
        It sounds like you're :
        1. stuffing non-integer values into an integer field.
        2. not deferencing the array.
Re: Dereference an array reference
by petdance (Parson) on Apr 19, 2001 at 01:19 UTC
    I have an object where $self->things is an array.

    I'm not sure I understand your intent, but $self->things is not an array. It might be a reference to an array, but it's not itself an array. Could that be your stumbling block?

    # Andy Lester  AIM:petdance
    %_=split';','.; Perl ;@;st a;m;ker;p;not;o;hac;t;her;y;ju';
    print map $_{$_}, split //,
      actually, methods (and subs in general) can return arrays... but you're quite right that if it's returning an array REF, then it will be seen in this case as if it returned an array of 1 value, "ARRAY(xxxxxx)".
        actually, methods (and subs in general) can return arrays
        No, they can return lists in a list context, or scalars in a scalar context. Nothing else. Cannot return an array.

        -- Randal L. Schwartz, Perl hacker

Re: Dereference an array reference
by suaveant (Parson) on Apr 19, 2001 at 00:35 UTC
    What do you mean by its name?

    I'm afraid I have no idea what you mean.
                    - Ant

Re: Dereference an array reference
by fmogavero (Monk) on Apr 19, 2001 at 00:42 UTC
    I would like to put either $_ or $name at the end of the query string but I keep getting an array reference.

    Sorry about that. I should have written that when I try to view the concatenated SQL query, the $thangsname is missing, because it's an array reference

      You mean the name of the array? Or a value within the array?
                      - Ant
        I am trying to create a new object and fill its name property with a value from the array/list.(I really don't know anymore) The name is actually supposed to be a database-wide key. I want to extract the information from the database and put it into the object.

        Here's the story that I didn't want to bore anyone with.

        When an "extract" is performed here at present, they perform too many steps. (create temp table in database, populate temp table, use "DataJunction" to export temp table info to file, clean up database)

        My idea was since they are using Perl already to parse data from outside sources, we could use Perl to do the "extracts." Perl would be perfect for that.

        Since the Product that I support will be going away (we hope), and changing to an OO thing, Ifigured that we could use OO in Perl to do "extracts" and the converse which is "Loads." This way our objects could WDDX all their information over to the new objects. Also the database would be left alone to do what it does best.

        My "extract" object gets the database-wide key, and creates a "profile" object that contains all the fields from the database. This is exactly what I wanted. This is exactly what I want because we can take the "profile" and write it to the database or the WDDX or a file and it can be populated from any source like the database, or WDDX or a file.

        If you are still awake, here is where I am slightly stuck. My "extract" opens all the proper files, and retrieves the database-wide key according to the proper parameters. Now I need to take this list(extract_object->list_of_database_wide_keys) and create a "profile" object for each one and then write it to the files.

        I keeping getting a SQL statement like "select * from table where database_wide_key = ARRAY(XXXXXXXX)"
        I just don't know how to properly dereference that array reference. I've gotten SQL errors like cant convert CHAR 'ARRAY(XXXXXXX)' to INT4 field.

        My forehead is flatter and embedded with small shards of busted monitor.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://73651]
Approved by root
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-13 23:17 GMT
Find Nodes?
    Voting Booth?

    No recent polls found