The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
 All Forums
 Origin Forum for Programming
 LabTalk Forum
 Delete multiple columns

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

Screensize:
UserName:
Password:
Anti-Spam Code:
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert HyperlinkUpload FileInsert Image Insert CodeInsert QuoteInsert List
   
Message:

* HTML is OFF
* Forum Code is ON
Smilies
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Clown [:o)]
Black Eye [B)] Eight Ball [8] Frown [:(] Shy [8)]
Shocked [:0] Angry [:(!] Dead [xx(] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
Check here to subscribe to this topic.
   

T O P I C    R E V I E W
Clairekun Posted - 08/11/2020 : 2:00:01 PM
Origin Ver. and Service Release (Select Help-->About Origin): 2018b
Operating System: Windows 10

Hello,

I am trying to delete multiple columns using a range. I have been browsing the help files, but nothing is addressing what I need exactly. I would need to delete all columns that do not match the specified long names.

Is there any way to achieve this?
8   L A T E S T    R E P L I E S    (Newest First)
Clairekun Posted - 08/13/2020 : 11:24:22 AM
I see, thanks!
Castiel Posted - 08/13/2020 : 11:11:37 AM
quote:
Originally posted by Clairekun


It's working! I wasn't sure if you could assign a string to a range, I initially thought it was solely for integers. It's good to know. I also forgot you could use if == 0, silly me.

I would like to understand a few things, if you were so nice to clarify them for me:

1. What's the difference between looping forwards or backwards?
2. Could you explain why you used (int i = wks.ncols; i >= 1; i--) instead of (int i = 1; i >= wks.ncols; i--), as I normally see it in help files (or point me where I can read about it)? I don't quite see the difference, but obviously the latter doesn't work.

Thank you!



Let's say, col(1) and col(2) should be deleted. When looping forward, col(2) is actually shifted to be the first column after deleting col(1) and would be skipped in the second loop (the second loop refers to col2 which was col3). Not a problem if looping backward.

Basically, when iterating a collection by index, do not alter the item index before visiting it.


------------------------------------------
       Be The Change
             You Want To See
                   In The World
------------------------------------------
Clairekun Posted - 08/13/2020 : 07:22:28 AM
quote:

See if the following works for you.

This is an example of removing columns if its name is NOT one of "Sample", "Temperature", "Pressure" or "Viscosity":

StringArray KeepCols = {"Sample", "Temperature", "Pressure", "Viscosity"};
for(int i = wks.ncols; i >= 1; i--)
{
    range rr = $(i);
    int nFound = KeepCols.Find(rr.LName$);
    if(nFound == 0)
    {
        delete rr;
    }
}




It's working! I wasn't sure if you could assign a string to a range, I initially thought it was solely for integers. It's good to know. I also forgot you could use if == 0, silly me.

I would like to understand a few things, if you were so nice to clarify them for me:

1. What's the difference between looping forwards or backwards?
2. Could you explain why you used (int i = wks.ncols; i >= 1; i--) instead of (int i = 1; i >= wks.ncols; i--), as I normally see it in help files (or point me where I can read about it)? I don't quite see the difference, but obviously the latter doesn't work.

Thank you!
Castiel Posted - 08/13/2020 : 06:36:03 AM
quote:
Originally posted by Clairekun

quote:
Originally posted by Castiel

1. find one column to be deleted
2. store the column index in a dataset or store the column short name in a stringarray
3. goto 1 untill no more columns found
4. define a range according to colum index/name from 2 in backward order
5. delete the range from 4
6. goto 4 until all found columns are deleted



Okay, please excuse me if I'm saying something stupid since my LT knowledge is very limited.

To get column short names, I wrote:

stringarray KeepCols;
KeepCols.add("Sample");
KeepCols.add("Temperature");
KeepCols.add("Pressure");
KeepCols.add("Viscosity");
loop(ii,1,KeepCols.GetSize()) 
{
string KeepLN$ = KeepCols.GetAt(ii)$;
	for(int jj = 1; jj <=wks.ncols; jj++)	
	{		
	if (col(jj)[L]$ == KeepLN$) type "Column index is col(jj)[G]$"

} 

}

Basically, I tried to say... "if any of these columns has any of those long names, return the column index of said column" just to see if it worked. However, it returns nothing. I also tried %(KeepLN$), with and without quotations, to no avail.

What am I doing wrong?

Next step would be defining a new string array to store said short names, and changing the script from If function to something that stores the short names there (which I think I can do). The rest, I will definitively need help with.

Also, I think I have read somewhere, although I cannot find where, that you can use ! in If commands to find the items that do not match that condition. This would be ideal, since what I really need is to delete all other columns. Is this true? I might use Else instead, if that was not the case.

Thank you.


See if the following works for you.

This is an example of removing columns if its name is NOT one of "Sample", "Temperature", "Pressure" or "Viscosity":

StringArray KeepCols = {"Sample", "Temperature", "Pressure", "Viscosity"};
for(int i = wks.ncols; i >= 1; i--)
{
    range rr = $(i);
    int nFound = KeepCols.Find(rr.LName$);
    if(nFound == 0)
    {
        delete rr;
    }
}



------------------------------------------
       Be The Change
             You Want To See
                   In The World
------------------------------------------
Clairekun Posted - 08/12/2020 : 3:15:53 PM
quote:
Originally posted by Castiel

1. find one column to be deleted
2. store the column index in a dataset or store the column short name in a stringarray
3. goto 1 untill no more columns found
4. define a range according to colum index/name from 2 in backward order
5. delete the range from 4
6. goto 4 until all found columns are deleted



Okay, please excuse me if I'm saying something stupid since my LT knowledge is very limited.

To get column short names, I wrote:

stringarray KeepCols;
KeepCols.add("Sample");
KeepCols.add("Temperature");
KeepCols.add("Pressure");
KeepCols.add("Viscosity");
loop(ii,1,KeepCols.GetSize()) 
{
string KeepLN$ = KeepCols.GetAt(ii)$;
	for(int jj = 1; jj <=wks.ncols; jj++)	
	{		
	if (col(jj)[L]$ == KeepLN$) type "Column index is col(jj)[G]$"

} 

}

Basically, I tried to say... "if any of these columns has any of those long names, return the column index of said column" just to see if it worked. However, it returns nothing. I also tried %(KeepLN$), with and without quotations, to no avail.

What am I doing wrong?

Next step would be defining a new string array to store said short names, and changing the script from If function to something that stores the short names there (which I think I can do). The rest, I will definitively need help with.

Also, I think I have read somewhere, although I cannot find where, that you can use ! in If commands to find the items that do not match that condition. This would be ideal, since what I really need is to delete all other columns. Is this true? I might use Else instead, if that was not the case.

Thank you.
Castiel Posted - 08/12/2020 : 08:51:47 AM
quote:
Originally posted by Clairekun

The thing is, everything I find needs to select columns to delete with column number.

In my case, I always have the same 4 columns and others that are not always the same. Neither the constant nor the additional columns share the same column number among worksheets or workbooks, that's why I need to specify long names.

Unless I'm understanding it wrongly, the code you suggested deals with colum numbers only, which wouldn't suit my needs.



1. find one column to be deleted
2. store the column index in a dataset or store the column short name in a stringarray
3. goto 1 untill no more columns found
4. define a range according to colum index/name from 2 in backward order
5. delete the range from 4
6. goto 4 until all found columns are deleted



------------------------------------------
       Be The Change
             You Want To See
                   In The World
------------------------------------------
Clairekun Posted - 08/12/2020 : 08:40:32 AM
The thing is, everything I find needs to select columns to delete with column number.

In my case, I always have the same 4 columns and others that are not always the same. Neither the constant nor the additional columns share the same column number among worksheets or workbooks, that's why I need to specify long names.

Unless I'm understanding it wrongly, the code you suggested deals with colum numbers only, which wouldn't suit my needs.
Castiel Posted - 08/12/2020 : 07:36:56 AM
quote:
Originally posted by Clairekun

Origin Ver. and Service Release (Select Help-->About Origin): 2018b
Operating System: Windows 10

Hello,

I am trying to delete multiple columns using a range. I have been browsing the help files, but nothing is addressing what I need exactly. I would need to delete all columns that do not match the specified long names.

Is there any way to achieve this?



Loop over columns in a worksheet in backward order to find the column to be deleted, then 'delete' it. For example
range rr = 1;
delete rr;



------------------------------------------
       Be The Change
             You Want To See
                   In The World
------------------------------------------

The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000