SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Remove first two columns of text

    Hi,

    Let's say I have a textArea which contains this text structure:

    PHP Code:
    A  B  C  D  E
    01 02 03 04 05
    06 07 08 09 10 
    How to remove first two columns to obtain this result:

    PHP Code:
    C  D  E
    03 04 05
    08 09 10 
    Thanks in advance,
    minopharma

  2. #2
    SitePoint Guru glenngould's Avatar
    Join Date
    Nov 2005
    Posts
    662
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code Actionscript:
    // first and last columns to include into the output
    var START_COL=3;
    var END_COL=5;
     
    // myInput is a text area on stage
    var input=myInput.text; 
     
    //output string
    var output='';
     
    //the output array
    var outputRows:Array=new Array();
     
    //splitting each line (row) into an array
    var myRows=input.split(/\r/);
     
    //temporary array to hold columns in a row
    var tempCol:Array;
     
    for (var i=0;i<myRows.length;i++) {
    	//find the column values in a row
    	tempCol=myRows[i].split(/\s*\s/);
     
    	//slice the desired columns
    	outputRows[i]=tempCol.slice(START_COL-1,END_COL);	
    }
     
    //now converting the output array to string
     
    //use 2 spaces for the first row values
    output+=outputRows[0].join("  ")+"\r";
     
    //add the rest
    for (i=1;i<outputRows.length;i++) {
    	output+=outputRows[i].join(" ")+"\r";
    }
     
    // myOutput is a text area on stage
    myOutput.text=output;
    Tweep List adds an avatar menu to Twitter (open source) ---------- Word Stats shows your most used words on Twitter

  3. #3
    SitePoint Guru glenngould's Avatar
    Join Date
    Nov 2005
    Posts
    662
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another one with regex although I'm not good at regular expressions. I would be glad to see how it can be improved.

    Code Actionscript:
    var REMOVE_INDEX=2; //columns to be removed from start
    var input="A  B  C  D  E\r01 02 03 04 05\r06 07 08 09 10\r";
    var output="";
     
    	//pattern
    	var patStr="";
    	for (var i=0;i<REMOVE_INDEX;i++) {
    		patStr+="\\S*\\s*\\s";
    	}
    	var myPattern:RegExp=new RegExp(patStr);
     
    var myRows=input.split(/\r/);
     
    for (i=0;i<myRows.length;i++) {
    	output+= myRows[i].replace(myPattern, "")+"\r";
    }
     
    trace(output);
    Tweep List adds an avatar menu to Twitter (open source) ---------- Word Stats shows your most used words on Twitter

  4. #4
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you glenngould for your replies and codes also. I've tested the two codes in Flash and Flex also and work great.

    I have only one question for you. If I have an array like this:

    PHP Code:
     1.0
      2.0
      3.0
      4.0
      5.0
      6.0 
    How to split it let's say into two columns:

    PHP Code:
     1.0   4.0
      2.0   5.0
      3.0   6.0 
    Thanks again,
    minopharma

  5. #5
    SitePoint Guru glenngould's Avatar
    Join Date
    Nov 2005
    Posts
    662
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What's the general rule you need to apply? Do you want to divide an array of values into two columns, or do you need to divide it into x number of columns each having 3 values?
    Tweep List adds an avatar menu to Twitter (open source) ---------- Word Stats shows your most used words on Twitter

  6. #6
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Infact I want to divide an array of values into two or more columns, depending on the number of the values in the array (primary column).

    Thanks,
    minopharma

  7. #7
    SitePoint Guru glenngould's Avatar
    Join Date
    Nov 2005
    Posts
    662
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code Actionscript:
    var input="1.0\r2.0\r3.0\r4.0\r5.0\r6.0\r7.0\r8.0";
    var output="";
    var COL_SIZE=3; //maximum number of values in a column
     
    var allValues:Array=input.split(/\r/);
     
    var j:int=0;
     
    for (var i:int=0;i<COL_SIZE && i<allValues.length;i++) {
    	j=0;
     
    	while (i+j<allValues.length) {
    		output+=allValues[i+j]+" ";
    		j+=COL_SIZE;
    	}
     
    	output+="\r";
    }
     
    trace(input+"\r");
    trace(output);
    Tweep List adds an avatar menu to Twitter (open source) ---------- Word Stats shows your most used words on Twitter

  8. #8
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's it!

    Thank you very much glenngould once again!

    Best regards,
    minopharma


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •