Question/Problem with foreach loop

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.

<snip>

# Iterate through all tasks

foreach ( [at] $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 '';

<snip>

--
To unsubscribe, e-mail: beginners-unsubscribe [at] pern.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
CM Analyst [ Do, 19 Mai 2011 02:06 ] [ ID #2059755 ]

Re: Question/Problem with foreach loop

On 5/18/11 Wed May 18, 2011 5:06 PM, "CM Analyst" <cmanalyst [at] yahoo.com>
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.
>
> <snip>
>
> # Iterate through all tasks
>
> foreach ( [at] $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 ( [at] $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 [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Jim Gibson [ Do, 19 Mai 2011 02:22 ] [ ID #2059763 ]

Re: Question/Problem with foreach loop

>>>>> "CA" == CM Analyst <cmanalyst [at] yahoo.com> 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 [at] 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 [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Do, 19 Mai 2011 05:14 ] [ ID #2059766 ]

Re: Question/Problem with foreach loop

Gents, =0A=0ASorry 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.=0A=0AThanks a million!=0A=0Amy $taskstate =3D $taskEnt=
ity->GetFieldValue(state)->GetValue();=0A=0A=A0 =A0 $session->OutputDebugSt=
ring ("Task's state is $taskstate\n");=0A=A0 =A0=A0=A0=0A=A0 =A0=A0=A0=A0=
=A0=A0=A0=A0next if $taskstate eq "Completed";=0A=A0=A0=A0 =A0 =0A

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
CM Analyst [ Mi, 08 Juni 2011 03:02 ] [ ID #2060728 ]
Perl » gmane.comp.lang.perl.beginners » Question/Problem with foreach loop

Vorheriges Thema: Adding file contents into hashes
Nächstes Thema: List of Perl Features