Link to home
Start Free TrialLog in
Avatar of ramizmo
ramizmo

asked on

How to delete all elements in an Array that I added using push() method?

Dear Experts,
I have an array that I added elements using push() method, I have tried to use splice, pop and shift methods, it didn't work and it keep poping an error,
TypeError: Error #1006: value is not a function. at Equation_Ad_fla::MainTimeline/clearData()

I need to delete all elements ( empty the array ) and then add again with the same function below,
thanks




function cordsData(e:MouseEvent) {
 
		dispx=Number(gGraph.board.getCordx());
		dispy=Number(gGraph.board.getCordy());
		arrX.push(dispx);
		arrY.push(dispy);
		//for (i=0 ; i<arrX.length ; i++){
			
		myDataGrid.addItem({xCol:arrX[i].toFixed(digitX),yCol:arrY[i].toFixed(digitY)});
		
		i++;
}

Open in new window

Avatar of CyanBlue
CyanBlue
Flag of United States of America image

I don't really understand the code you have, but you can easily remove the content of the array by setting the length to 0...

theArray.length = 0;

CyanBlue
Avatar of ramizmo
ramizmo

ASKER

Thanks for the easy way to empty the Array, But I want after that to push the elements again, as you see from the code this is a function that been called to add coordinates of a graph into array, the values are dispx, dispy to be pushed to arrX and arrY which both are arrays. The problem after I empty both arrays and I call that function again, it gives me this run time error:
TypeError: Error #1010: A term is undefined and has no properties.
      at Equation_Ad_fla::MainTimeline/cordsData()
So please help me to find away to get over this error and allow me to add values again to my arrays

Thanks
how have you defined/decalred your arrays ?
Avatar of ramizmo

ASKER

Here his the whole file code . . . I defined my Arrays as new Array objects:

var arrX:Array = new Array();
var arrY:Array = new Array();
            
then I call them from the function when the mouse is clicked to fill the 2 Arrays with the coordinates, and these coordinates will be presented in a Data Grid
import flashandmath.as3.tools.SimpleGraph;
import flash.net.FileReference;
import flash.events.MouseEvent;
import flash.printing.*;
import fl.controls.List;
import fl.controls.DataGrid;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.data.DataProvider;
import fl.controls.NumericStepper;
import flash.printing.PrintJob;
 
 
 
		var gGraph:SimpleGraph=new SimpleGraph(650,600);
		var myFile:FileReference = new FileReference();
		
		var dispx:Number;
		var dispy:Number;
		
		var myDataGrid:DataGrid = new DataGrid();
		var xColumn:DataGridColumn = new DataGridColumn("xCol");
		var yColumn:DataGridColumn = new DataGridColumn("yCol");
		var dp:DataProvider = new DataProvider();
		
		var numDigitX:NumericStepper = new NumericStepper();
		var numDigitY:NumericStepper = new NumericStepper();
		var digitX:Number;
		var digitY:Number;
		
		var arrX:Array = new Array();
		var arrY:Array = new Array();
		
		var pj:PrintJob = new PrintJob();
		
			
	  	var i:int;
	  		
		
		numDigitX.move(40,527);
		numDigitY.move(152,527);
	
		xColumn.headerText = "X";
		yColumn.headerText = "Y";
		
		myDataGrid.move(5.8,525);
		myDataGrid.setSize(240,130);
		myDataGrid.addColumn(xColumn);
		myDataGrid.addColumn(yColumn);
		
		helpBox_mov.visible=false;
		
		gGraph.x=265;
		gGraph.y=53;
		addChild(gGraph);
		addChild(helpBox_mov);
		addChild(myDataGrid);
		addChild(numDigitX);
		addChild(numDigitY);
 
 
 
	function showHelp(e:MouseEvent):void {
		
		helpBox_mov.visible=true;
	}
 
	function closeHelp(e:MouseEvent):void {
		
		helpBox_mov.visible=false;
 
	}	
 
	function drawGraph(m: Event):void {
		
		gGraph.setWindow("-0.7", "10","-1", "10");
		gGraph.board.drawAxes();
		gGraph.board.drawTicks();
		if (showGrid.selected==true) {
			gGraph.board.drawGrid();
	
		} else if (showGrid.selected == false) {
			gGraph.board.shGrid.graphics.clear();
	
		}
 
		gGraph.board.addLabels();
 
 
	//g.graphRectangular(txtFun.text,"x",1,2,0x0000CC);
	}
 
	function quitPlotter(e:MouseEvent):void {
	fscommand("quit");
	}
 
	function openFile(e:MouseEvent):void {
 
		myFile.browse();
	}
 
	function saveFile(e:MouseEvent):void {
 
	//myFile.save("File");
	}
	
 
	function cordsData(e:MouseEvent) {
 
		dispx=Number(gGraph.board.getCordx());
		dispy=Number(gGraph.board.getCordy());
		arrX.push(dispx);
		arrY.push(dispy);
		//for (i=0 ; i<arrX.length ; i++){
			
		myDataGrid.addItem({xCol:arrX[i].toFixed(digitX),yCol:arrY[i].toFixed(digitY)});
		
		i++;
		
		//}
		//myDataGrid.addItem({xCol:dispx.toFixed(digitX),yCol:dispy.toFixed(digitY)});
		
		trace(arrX, arrX.length);
	}
 
	function printValues(e:MouseEvent) {
		pj.start();
		pj.addPage(gGraph.board);
		
		pj.send();
 
	}
	
	function clearData(e:MouseEvent):void{
		
		myDataGrid.removeAll();
		arrX.length = 0;
		arrY.length = 0;
		trace(arrX);
		trace(arrY);
		
		//var j:int;
		
		//for(j=0;j<arrX.length;j++){
			
			//arrX.splice(0);
			
		//}
		
		
	}
	
		
		
 
	function digitStepper(e:MouseEvent){
		
		
		digitX = numDigitX.value;
		digitY = numDigitY.value;
		
		myDataGrid.removeAll();
		var j:int;
		
		for(j=0; j<arrX.length; j++){
		
	 myDataGrid.addItem({xCol:arrX[j].toFixed(digitX),yCol:arrY[j].toFixed(digitY)});
		
		}
	}
	
	
		
numDigitX.addEventListener(MouseEvent.CLICK, digitStepper);
numDigitY.addEventListener(MouseEvent.CLICK, digitStepper);
clear_btn.addEventListener(MouseEvent.CLICK, clearData);
print_btn.addEventListener(MouseEvent.CLICK, printValues);
gGraph.addEventListener(MouseEvent.CLICK, cordsData);
open_btn.addEventListener(MouseEvent.CLICK, openFile);
//save_btn.addEventListener(MouseEvent.CLICK, saveFile);
exit_btn.addEventListener(MouseEvent.CLICK, quitPlotter);
help_btn.addEventListener(MouseEvent.CLICK, showHelp);
helpBox_mov.helpClose_btn.addEventListener(MouseEvent.CLICK,closeHelp);
this.addEventListener('enterFrame', drawGraph);

Open in new window

how about doing the following:

arrX:Array = new Array();
arrY:Array = new Array();

instead of :

arrX.length = 0;
arrY.length = 0;

I know this might be a bad design, creating a new object all together, but it just might work for you, and secondly I don;t have much of an experience in Flex, so just try this out.



 
Would it be possible you are actually getting that error message because of dispx or dispy being undefined???  I am saying it because all other stuff look okay to my bare eyes...

CyanBlue
Avatar of ramizmo

ASKER

I am sorry humanonomics, But your solution is giving the same error.
For CyanBlue question, the undefined refers to the arrays I think, But I have noticed that after I insert the same number of the data before I remove the arrays' elements the Error stops appearing and the Arrays are start getting Data again, I don't understand why?! what should I do?!
what does 'trace(arrX);' and 'trace(arrY);' do ?
Avatar of ramizmo

ASKER

After setting the Arrays to length 0, the trace result is nothing just blank output lines
That means that dispx and dispy are empty, which in turn kills addItem() call because you are providing empty data in there...  Trace the gGraph.board value to see if it displays anything for you...  

CyanBlue
Avatar of ramizmo

ASKER

No, dispx and dispy are never empty, they are variables that curry the value of coordinates X and Y, OK let me explain the problem again ! when I set the array length to Zero or empty the array using any method like splice, pop or shift, it gets empty BUT, when I try to add again any element or any thing  using push() function it doesn't work and it throws this error:

TypeError: Error #1010: A term is undefined and has no properties.
      at Equation_Ad_fla::MainTimeline/cordsData()

Please help!!!
ASKER CERTIFIED SOLUTION
Avatar of CyanBlue
CyanBlue
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ramizmo

ASKER

this is the output of the function after update:

dispx = 2.81 : dispy = 4.45
digitX = NaN : arrX[i].toFixed(digitX) = 3
digitY = NaN : arrY[i].toFixed(digitY) = 4
dispx = 4.95 : dispy = 6.96
digitX = NaN : arrX[i].toFixed(digitX) = 5
digitY = NaN : arrY[i].toFixed(digitY) = 7
dispx = 5.41 : dispy = 5.8
digitX = NaN : arrX[i].toFixed(digitX) = 5
digitY = NaN : arrY[i].toFixed(digitY) = 6

Avatar of ramizmo

ASKER

Sorry I forgot a tracing line, this is the whole output:

dispx = 3.86 : dispy = 6.66
arrX = 3.86 : arrY = 6.66
digitX = NaN : arrX[i].toFixed(digitX) = 4
digitY = NaN : arrY[i].toFixed(digitY) = 7
dispx = 4.04 : dispy = 7.45
arrX = 3.86,4.04 : arrY = 6.66,7.45
digitX = NaN : arrX[i].toFixed(digitX) = 4
digitY = NaN : arrY[i].toFixed(digitY) = 7
dispx = 3.96 : dispy = 3.38
arrX = 3.86,4.04,3.96 : arrY = 6.66,7.45,3.38
digitX = NaN : arrX[i].toFixed(digitX) = 4
digitY = NaN : arrY[i].toFixed(digitY) = 3
dispx = 2.94 : dispy = 5.45
arrX = 3.86,4.04,3.96,2.94 : arrY = 6.66,7.45,3.38,5.45
digitX = NaN : arrX[i].toFixed(digitX) = 3
digitY = NaN : arrY[i].toFixed(digitY) = 5

Avatar of ramizmo

ASKER

This is the output of the function coordsData() after I empty the arrays by executing the function clearData(), what i have notice is lets say before clearing the data I added elements 2 times, which means I clicked on the coordinates board two times, then I cleared the two arrays by executing clearData(), what will happen is the Error will appear for the first two clicks only,which are the length of the array before, then the error will stop poping up and all the data will show in the trace including the one was poping up the error during the click, But of course it won't show those was poping up the error
in the Data Grid, but after those two the data will appear in the data Grid Again! , Hope that helps

dispx = 2.51 : dispy = 6.22
arrX = 2.51 : arrY = 6.22
digitX = NaN : arrX[i].toFixed(digitX) = 3
digitY = NaN : arrY[i].toFixed(digitY) = 6
dispx = 5.65 : dispy = 5.8
arrX = 2.51,5.65 : arrY = 6.22,5.8
digitX = NaN : arrX[i].toFixed(digitX) = 6
digitY = NaN : arrY[i].toFixed(digitY) = 6
dispx = 5.09 : dispy = 3.64
arrX = 2.51,5.65,5.09 : arrY = 6.22,5.8,3.64
digitX = NaN : arrX[i].toFixed(digitX) = 5
digitY = NaN : arrY[i].toFixed(digitY) = 4


dispx = 1.72 : dispy = 5.12
arrX = 1.72 : arrY = 5.12
TypeError: Error #1010: A term is undefined and has no properties.
      at Equation_Ad_fla::MainTimeline/cordsData()
dispx = 1.85 : dispy = 4.13
arrX = 1.72,1.85 : arrY = 5.12,4.13
TypeError: Error #1010: A term is undefined and has no properties.
      at Equation_Ad_fla::MainTimeline/cordsData()
dispx = 1.6 : dispy = 4.34
arrX = 1.72,1.85,1.6 : arrY = 5.12,4.13,4.34
TypeError: Error #1010: A term is undefined and has no properties.
      at Equation_Ad_fla::MainTimeline/cordsData()
dispx = 1.44 : dispy = 4.34
arrX = 1.72,1.85,1.6,1.44 : arrY = 5.12,4.13,4.34,4.34
digitX = NaN : arrX[i].toFixed(digitX) = 1
digitY = NaN : arrY[i].toFixed(digitY) = 4
dispx = 1.49 : dispy = 5.01
arrX = 1.72,1.85,1.6,1.44,1.49 : arrY = 5.12,4.13,4.34,4.34,5.01
digitX = NaN : arrX[i].toFixed(digitX) = 1
digitY = NaN : arrY[i].toFixed(digitY) = 5
dispx = 1.03 : dispy = 4.81
arrX = 1.72,1.85,1.6,1.44,1.49,1.03 : arrY = 5.12,4.13,4.34,4.34,5.01,4.81
digitX = NaN : arrX[i].toFixed(digitX) = 1
digitY = NaN : arrY[i].toFixed(digitY) = 5

Avatar of ramizmo

ASKER

Well does that actually means the error is in adding the data to the dataGrid and it has noting to do with the array! wow just noticed it, So why it doesn't want to add it again to the Data Grid, by the way I  clear the Data Grid by using removeAll() function
Avatar of ramizmo

ASKER

I solved it ...!!! its just that I didn't set i to zero again when I clear the data . . . Thank you all experts that helped and commented specially CyanBlue, thank you all for your time and effort