Array Question

Hi all,

I have 3 arrays (each with the same number of elements) and i want to do an insert foreach element of the first array ($chart_id). What's the best way to loop through it?  

The data is pulled from user input and going into a table for lookups. I'm using Perl. :p

sub SubmitNewDaily {
   my $user_id       = param('user_id');
   my @chart_id      = param('chart_id');
   my @cat_id        = param('cat_id');
   my @quantity      = param('quantity');
   my $date          = param('date');

   my @data = (@chart_id, $date, @cat_id, $user_id, @quantity); ## I know this doesn't work... :)
   
   StartTemplate();
   print "<h2>Test Page: Params Passed to Insert</h2>";
   print "Date: $date <p>
          UserId:$user_id <p>
          Food Id: @chart_id <p>
          CategoryId: @cat_id <p>
          Quantity: @quantity <p>";
   EndTemplate();


### I want to insert a record for each one of the chart_id's in the array but i also want to have the other
### fields populated with their values. ie - the values in: @cat_id, @quantity


   foreach $chart_id(@chart_id) {
            my $out = $dbh->do("INSERT INTO diet_content(chart_id, date, cat_id, user_id, chart_quantity)
      VALUES(?,?,?,?,?)",{},@data);
   }
}

Any help would be great! Thank you.
-mookie


 
mookie13Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

markhoyCommented:
This:

my $user_id       = param('user_id');
  my @chart_id      = param('chart_id');
  my @cat_id        = param('cat_id');
  my @quantity      = param('quantity');
  my $date          = param('date');


is chart_id an array or does it contain a single value?
0
ozoCommented:
@data=map{"$chart_id[$_] $date $cat_id[$_] $user_id $quantity[$_]"}0..$#chart_id;
0
mookie13Author Commented:
chart_id is an array with multiple values depending on how many items were selected by the user on the previous page.

There will be the exact amount of values for each of the arrays.  The only single values are in user_id and date.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

dc197Commented:
my @chart_id      = param('chart_id');

If param('chart_id') returns something in scalar context (e.g. if it returns a single value) then @chart_id will be an array with only one element.

Is this really what you want? You say "chart_id is an array with multiple values depending..."
0
mookie13Author Commented:
it's an array with multiple values so long a user selected more than one item from the food chart.  in that case it would be an array with a single item or a scalar.

the way it works is a user goes to a food chart and selects the items he/she has consumed and applicable quantity.  this data is then inserted into a content table.

the content table are:

content_id
date
quantity (ie - i ate 3 carrots)
chart_id (foodchart item id)
cat_id (category)

that's why i'm passing the values to the submit part of the script as an array since there are multiple food items that can be selected.  each one of the food items selected will have a corresponding quantity (1 or more) and category.

each array will have the same amount of elements.

so when i'm doing the insert i want to insert for each @chart_id that was passed but i want the corresponding @quantity and @cat_id too.  
0
mookie13Author Commented:
To ozo:

Thank you for the map function idea. This works and gives me an array of all the elements.

So when I select two items I get this:

21 2003-3-12 2 1 1 14 2003-3-12 2 1 1

Which is all of the params passed:
21= chart_id
2003-3-12 = date
2 = category
1= user_id
1 = quantity

and so on.

What I'm trying to do here is insert a row for each one of the chart_id's passed.

I guess I'm confused on how to do that since they are all one string now in the @data array because it trys to insert all of the values even though there aren't that many fields.

Thoughts?







0
ozoCommented:
my @data = (@chart_id, $date, @cat_id, $user_id, @quantity); ## I know this doesn't work... :)

what are you trying to do here?
What are the contents of (@chart_id, $date, @cat_id, $user_id, @quantity), and what do you want @data to contain?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mookie13Author Commented:
oof... i was talking with the d00d from the Perl Journal (http://www.tpj.com) and he laughed at me and gave the code i needed.

   for ($i=0; $i<=$#chart_id; $i++) {
     @data=($chart_id[$i], $date, $cat_id[$i], $user_id, $quantity[$i]);

     my $out = $dbh->do("INSERT INTO diet_content(chart_id, date, cat_id, user_id, chart_quantity)
                        VALUES(?,?,?,?,?)",{},@data);

   }
0
mookie13Author Commented:
Hi all,

Not too sure about how to close this question up since I found the answer outside of EE.  

I do want to say THANKS! to all that helped. I learned something from those responses.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.