Variable Scope inside two for iterations

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Variable Scope inside two for iterations

Cristian Salinas
Hello !

i'm really new using beanshell and i have a question. I just
programmed the next piece of code:

-------begin-------

HashMap quantitySummaryByFacility = new HashMap();
HashMap quantitySummaryByProduct = new HashMap();


List productList = delegator.findAll("Product");
List facilityList = delegator.findAll("Facility");

(*)String productId=null;

(1)for (product : productList) {

  productId = product.getString("productId");

  (2)for (facility : facilityList){

        Map contextInput = null;
        Map resultOutput = null;
       
  contextInput = UtilMisc.toMap("productId", (*)productId,
"facilityId", facility.getString("facilityId"));
  resultOutput =
dispatcher.runSync("getInventoryAvailableByFacility", contextInput);
 
  HashMap quantitySummary = new HashMap();
  quantitySummary.put("productId", (*)productId);
  quantitySummary.put("facilityId", facility.getString("facilityId"));
  quantitySummary.put("totalOnHand", resultOutput.get("quantityOnHandTotal"));
 
  quantitySummaryByFacility.put(facility.getString("facilityId"),
quantitySummary);
  }

  quantitySummaryByProduct.put((*)productId, quantitySummaryByFacility);

}

-------end-----------

I declarated the variable (*) outside the two loops specting to be
'visible' inside the two loops, and for the first loop(1) is going ok,
but the second loop (2) is just accesing the last value from the
productList.

how can i read the value (*)  is getting in the first loop in the second loop?

thx a lot in advance for your answer .

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Variable Scope inside two for iterations

Cristian Salinas
here is an example output i get from the code below (this is the
HashMap named quantitySummaryByProduct) :

{
GZ-1006-1={
MyRetailStore={facilityId=MyRetailStore, productId=GC-001-H25,
totalOnHand=0.0},
WebStoreWarehouse={facilityId=WebStoreWarehouse, productId=GC-001-H25,
totalOnHand=0.0},
Demo3PL={facilityId=Demo3PL, productId=GC-001-H25, totalOnHand=0.0}},

GZ-1006-4={
MyRetailStore={facilityId=MyRetailStore, productId=GC-001-H25,
totalOnHand=0.0},
WebStoreWarehouse={facilityId=WebStoreWarehouse, productId=GC-001-H25,
totalOnHand=0.0},
Demo3PL={facilityId=Demo3PL, productId=GC-001-H25, totalOnHand=0.0}},

GC-001-H25={
MyRetailStore={facilityId=MyRetailStore, productId=GC-001-H25,
totalOnHand=0.0},
WebStoreWarehouse={facilityId=WebStoreWarehouse, productId=GC-001-H25,
totalOnHand=0.0},
Demo3PL={facilityId=Demo3PL, productId=GC-001-H25, totalOnHand=0.0}}

}

the format is:
productId(outside Loop) =
{
 idfacility = {facilityId=id, productId=productid(insideLoop), totalOnHand}
}

so, the productId for the first loop is constantly changing, but the
value of productId for the second loops stays the same always as the
last value from the product list.

Im really confused about this. its seems like a very basic and simple
scoping problem, but i really don't know what i am doing wrong :(


On 10/23/07, Travis Bulford <[hidden email]> wrote:

> I can see nothing wrong with what you have written could there be a bug in
> your data that causes the behavior you are seeing as I do not think BSH is
> causing the issue.
>
> I suggest you system.out the value from the 1st loop to be sure its not a
> data issue.
>
> Travis
>
>
> On 10/23/07, Cristian Salinas <[hidden email]> wrote:
> >
> > Hello !
> >
> > i'm really new using beanshell and i have a question. I just
> > programmed the next piece of code:
> >
> > -------begin-------
> >
> > HashMap quantitySummaryByFacility = new HashMap();
> > HashMap quantitySummaryByProduct = new HashMap();
> >
> >
> > List productList = delegator.findAll("Product");
> > List facilityList = delegator.findAll("Facility");
> >
> > (*)String productId=null;
> >
> > (1)for (product : productList) {
> >
> >   productId = product.getString("productId");
> >
> >   (2)for (facility : facilityList){
> >
> >         Map contextInput = null;
> >         Map resultOutput = null;
> >
> >         contextInput = UtilMisc.toMap("productId", (*)productId,
> > "facilityId", facility.getString("facilityId"));
> >         resultOutput =
> > dispatcher.runSync("getInventoryAvailableByFacility",
> contextInput);
> >
> >         HashMap quantitySummary = new HashMap();
> >         quantitySummary.put("productId", (*)productId);
> >         quantitySummary.put("facilityId",
> facility.getString("facilityId"));
> >         quantitySummary.put("totalOnHand",
> resultOutput.get("quantityOnHandTotal"));
> >
> >         quantitySummaryByFacility.put(facility.getString("facilityId"),
> > quantitySummary);
> >   }
> >
> >   quantitySummaryByProduct.put((*)productId, quantitySummaryByFacility);
> >
> > }
> >
> > -------end-----------
> >
> > I declarated the variable (*) outside the two loops specting to be
> > 'visible' inside the two loops, and for the first loop(1) is going ok,
> > but the second loop (2) is just accesing the last value from the
> > productList.
> >
> > how can i read the value (*)  is getting in the first loop in the second
> loop?
> >
> > thx a lot in advance for your answer .
> >
> >
> -------------------------------------------------------------------------
> > This SF.net email is sponsored by: Splunk Inc.
> > Still grepping through log files to find problems?  Stop.
> > Now Search log events and configuration files using AJAX and a browser.
> > Download your FREE copy of Splunk now >> http://get.splunk.com/
> > _______________________________________________
> > Beanshell-users mailing list
> > [hidden email]
> >
> https://lists.sourceforge.net/lists/listinfo/beanshell-users
> >
>
>
>
> --
>  www.toxicbunny.co.za << Java port site

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users