Hitachi Vantara Pentaho Community Forums
Results 1 to 4 of 4

Thread: Modified Java Script Value

  1. #1
    Join Date
    May 2007
    Posts
    24

    Unhappy Modified Java Script Value

    After spending the last few hours trying to figure out how lpad and rpad work I am completley baffled.
    In my code I want to prepend a single character to the target string; hence lpad(target,"x",1). This provides a no change result and only when I increase the number of character parameter to 19 do I get a positive result of 1 "x" prepended to the string. It seems as though the interpreter is using 18 decimal = 0 as a base. This make no sense in hex or octal so I am lost.
    Then I try the sample script that is loaded with lpad and it seems that it uses a 12 deciaml = 0 as the base. rpad appears to work the same way. It has to be a configuration parameter somewhere but I have no idea where to look. Any help would be appreciated greatly as I am an old 'C' programmer trying to understand OO at the same time as digesting all the nuances of JAVA, the development tools, Pentaho and JavaScript and the frustration level is getting extremely high.
    Thanks

  2. #2
    Join Date
    May 2006
    Posts
    4,882

    Default

    This is the code for lpad:

    Code:
    	 public static String lpad(Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext){
    		 
    		 //(String valueToPad, String filler, int size) {
    	     try{
    	    	 if(ArgList.length==3){
    	    		 if(isNull(ArgList, new int[]{0,1,2})) return null;
    	    		 else if(isUndefined(ArgList, new int[]{0,1,2})) return (String)Context.getUndefinedValue();
    	    		 String valueToPad = Context.toString(ArgList[0]);
    	    		 String filler = Context.toString(ArgList[1]);
    	    		 int size = (int)Context.toNumber(ArgList[2]);
    	    		 
    	    		 while (valueToPad.length() < size){
    	    			 valueToPad = filler + valueToPad;
    	    		 }
    	    		 return valueToPad;
    	    	 }
    	     }catch(Exception e){
    	    	 throw Context.reportRuntimeError("The function call lpad requires 3 arguments.");
    	     }
    	     return null;
    	 }
    And last week I put unit testcases in for lpad/rpad... they should work as expected.

    Regards,
    Sven

  3. #3
    Join Date
    May 2006
    Posts
    4,882

    Default

    The third argument is the size to pad to, not the times to append the second argument.

    Regards,
    Sven

  4. #4
    Join Date
    May 2007
    Posts
    24

    Smile Modified Java Script Value

    Ah...to be brought in from the dark is wonderful.. Thank you very much. Still having some problems but I imagine it is just my not knowing the language.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2019 Hitachi Vantara Corporation. All Rights Reserved.