1

I have some select result from database(like on image).

db select

I need do foreach cycle for id

It's must looks like this

foreach () {
   if ($status==1) {
      if ($process_id ~~ @some_array) {
    #do something
   }
}

How do for each cycle? As I understand I need to get a result from a base in hash form $ref = $sth->fetchrow_hashref, but I never worked with him, and my attempts were unsuccessful. Help me please.

3
  • 1
    What code do you have so far? $sth->fetchrow type methods can be called in a while loop to fetch and process one at a time Commented Nov 21, 2014 at 12:40
  • I can to get the arrays of the form 943 1 0 0 But it's not true for me. Code like while($ref = $sth->fetchrow_hashref) { @keys = keys %$ref; @values = values %$ref; } does not fit too Commented Nov 21, 2014 at 13:08
  • 1
    A hash lookup would be better than using (experimental) ~~ @array because it's faster and because it won't change on you. my %some_array = map { $_ => 1 } @some_array;, then if ($some_array{$process_id}) { ... } Commented Nov 21, 2014 at 18:04

1 Answer 1

2

Is fetchrow_array what you need?

while (my ($id, $status, $process_id, $error_count) = $sth->fetchrow_array) {
    if (1 == $status and grep $_ == $process_id, @some_array) {
        # do something
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.