Question/Problem with foreach loop

Question/Problem with foreach loop

am 19.05.2011 02:06:05 von CM Analyst

Hi,

In this code, the intent is to iterate through the tasks and modify the ID field. The Task record (entity) should not be modified if it's state equals "Completed".

When I run this routing, there are two problems:

Problem 1) The "if" statement is not being evaluated. The record (even if it's in the "Completed" state is being modified.

Problem 2) Subsequent records (ie any record after the first one is not modfied). In other words, if I have multiple records, just the first one in the list gets modified. All others are skipped.

Can someone help me figure out where I am going wrong with this? Thanks.



# Iterate through all tasks

foreach (@$tasks) {

# Get a task entity for the current taskid (in $_)

my $taskEntity = $session->GetEntity ('almtask', $_);

$taskEntity->GetFieldValue(state)->GetValue();

if ($taskEntity eq "Completed") {return;}

# did not work# if ($taskEntity eq "Completed") {return '';}
# did not work# if ($taskEntity eq "Completed") {exit 1;}

else {

$session->OutputDebugString ("Setting task entity to modify\n");

# Modify this task...
$taskEntity->EditEntity ('Modify');

$session->OutputDebueString ("Modifying task\n");

$session->OutputDebugString ("Setting task's project id to $ProjectID");

# Set the project field to the new ProjectID
my $returnMsg = $taskEntity->SetFieldValue ('project', $ProjectID);

return $returnMsg unless $returnMsg eq '';



--
To unsubscribe, e-mail: beginners-unsubscribe@pern.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Question/Problem with foreach loop

am 19.05.2011 02:22:14 von Jim Gibson

On 5/18/11 Wed May 18, 2011 5:06 PM, "CM Analyst"
scribbled:

> Hi,
>
> In this code, the intent is to iterate through the tasks and modify the ID
> field. The Task record (entity) should not be modified if it's state equals
> "Completed".
>
> When I run this routine, there are two problems:
>
> Problem 1) The "if" statement is not being evaluated. The record (even if it's
> in the "Completed" state is being modified.
>
> Problem 2) Subsequent records (ie any record after the first one is not
> modfied). In other words, if I have multiple records, just the first one in
> the list gets modified. All others are skipped.
>
> Can someone help me figure out where I am going wrong with this? Thanks.
>
>
>
> # Iterate through all tasks
>
> foreach (@$tasks) {

You would be better to use an explicit variable here rather than the default
$_, especially since you are calling methods in the loop that might modify
the value of $_:

foreach my $task ( @$tasks ) {

>
> # Get a task entity for the current taskid (in $_)
>
> my $taskEntity = $session->GetEntity ('almtask', $_);

We don't know what $session contains and what GetEntity does.

>
> $taskEntity->GetFieldValue(state)->GetValue();

You have a bareword here (state). If this is supposed to be a string, then
you should quote it. state could be a function (or even a keyword in later
Perls):

$taskEntity->GetFieldValue('state')->GetValue();

Do you have 'use strict;' in your program.

>
> if ($taskEntity eq "Completed") {return;}

You probably don't want 'return' here. If you want to skip this task and go
to the next one in the loop, then you should use 'next'. 'return' will exit
from a function (although we don't know if this code is in a function since
you haven't shown us the whole code.):

next if $taskEntity eq 'Completed';




--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Question/Problem with foreach loop

am 19.05.2011 05:14:47 von Uri Guttman

>>>>> "CA" == CM Analyst writes:

CA> my $taskEntity = $session->GetEntity ('almtask', $_);

that gets a perl object in $taskEntity.

CA> $taskEntity->GetFieldValue(state)->GetValue();

where is the value being assigned to? $taskEntity is not being modified
or set in that line of code. it is just the object used to make the
call.

CA> if ($taskEntity eq "Completed") {return;}

since that is the object, it will never eq Completed. you need to save
the value from the previous line and compare that. this should work:

my $value = $taskEntity->GetFieldValue(state)->GetValue();

if ( $value eq "Completed") {
return;
}

see how that saves the value in a variable and then compares to that
variable. you need to learn more about OO in perl and how to
differentiate an object from the values that methods can return.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Question/Problem with foreach loop

am 08.06.2011 03:02:34 von CM Analyst

Gents, Sorry for my delayed response. Thank you for your suggestions.=
Based on your feedback, I made the following changes, and the hook is now =
working as expected. Thanks a million! my $taskstate =3D $taskEnt=
ity->GetFieldValue(state)->GetValue(); =A0 =A0 $session->OutputDebugSt=
ring ("Task's state is $taskstate\n");       =A0     =
    next if $taskstate eq "Completed";     =A0 =0A

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/