Surrounding 2 or more columns of text with other text - string

I'm debugging some ColdFusion code (although the question is really language-agnostic), and from the debug output, have two columns of text.
Those columns are field name <tab> value and want to be able to quickly convert this into test code.
The text I start off with:
a 1
b 2
c 3
etc
The code I want to end up with:
structInsert(myStruct, "a", 1);
structInsert(myStruct, "b", 2);
structInsert(myStruct, "c", 3);
etc
Ordinarily, I'd use Excel, pasting the two columns of data into columns A and B, and create a formula in column C that concatenates A and B something like
="structInsert(myStruct, """ & A1 & """, " & B1 & ");"
This works fine (and is one of the main reasons I love Excel).
But I'm wondering... given that the whole world doesn't have Excel, how does everyone else do this?
Thanks!

Well I like to do with Notepad++ or Eclipse with search and replace feature with regular expression.
Like search for
([a-z]*)\t(\d)
replace with
structInsert(myStruct,"\1",\2);
So simple.. right?

You could do this with regular expressions.
In CFEclipse/CFBuilder open the Find/Replace dialogue
Find: ^(.+?)\t(.+?)$
Replace with: structInsert(myStruct, "$1", $2);
Check Regular Expressions
Click Replace All

Related

How to add multiple slashes to a single cell

I have a column with about 5k rows that I need to add slashes to. I need to add slashes to the first, second, and last space (" ") in the cell. Each cell has different lengths so there are different amounts of spaces between the second and last in each row.
Here's an example of some rows
NYLA D DURA FEMUR BCN LG
NULO D FZD GF BF 5oz
OMNI D BRTSH SLP LD GRN 4ft
OMNI D LIGHT S-HOOK
OMNI D SS BOWL 3qt
I need these converted to
NYLA/D/DURA FEMUR BCN/LG
NULO/D/FZD GF BF/5oz
OMNI/D/BRTSH SLP LD GRN/4ft
OMNI/D/LIGHT S-HOOK
OMNI/D/SS BOWL/3qt
I have tried using the substitute formula but that will only add one slash when I need to add 1-3. I'm not sure if nesting the substitute formula is possible in this scenario. If so, that should do it but I can't get it to work.
Nested SUBSTITUTE is the way to go:
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1," ","/",LEN(A1)-LEN(SUBSTITUTE(A1," ","")))," ","/",2)," ","/",1)
You can achieve this with the following formula:
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1," ","/",LEN(SUBSTITUTE(A1," "," "))-LEN(A1))," ","/",1)," ","/",1)
It replaces the last instance of " " followed by the first two instances.
This might however return an error if there are less than three " " in the string.

Ms Excel IF ISNUMBER SEARCH for multiple text using AND or OR operator

This is my excel data
As you can see in B2, this formula only search for the first text it found and will ignore the rest.
What I'm trying to accomplish is if 2 or more different texts are found like B2, is it possible to print another message ... let say Apple & Banana found
This is my original excel formula for your reference:
=IF(ISNUMBER(SEARCH("apple",A2)),"Apple",
IF(ISNUMBER(SEARCH("banana",A2)),"Banana",
IF(ISNUMBER(SEARCH("cher",A2)),"Cherries",
"Other")))
I have tried and tried and thought hmmm why don't I simply add a SUBSTITUTE function...and it worked ;o)
Just in case there is someone out there looking for this:
=SUBSTITUTE(TRIM(CONCATENATE(IF(ISNUMBER(SEARCH("apple",A2)),"Apple "," "),
IF(ISNUMBER(SEARCH("banana",A2)),"Banana "," "),
IF(ISNUMBER(SEARCH("cher",A2)),"Cherries "," "),
IF(SUM((ISNUMBER(SEARCH({"apple","banana","cher"},A2,1)))+0)=0,"Other "," "))),"Apple Banana","Both")
See if following formula tweak helps you:
=TRIM(CONCATENATE(IF(ISNUMBER(SEARCH("apple",A2)),"Apple "," "),
IF(ISNUMBER(SEARCH("banana",A2)),"Banana "," "),
IF(ISNUMBER(SEARCH("cher",A2)),"Cherries "," "),
IF(SUM((ISNUMBER(SEARCH({"apple","banana","cher"},A2,1)))+0)=0,"Other "," ")))&" found"
Here is my proposal :
You create a Table with your KeyWord and Result in two columns
(columns K for KeyWord and L for Result).
You change your formula like this
=IF(ISNUMBER(SEARCH(K2,A2)),L2, IF(ISNUMBER(SEARCH(K3,A2)),L3, IF(ISNUMBER(SEARCH(K4,A2)),L4, "Other")))
After that, is more easy to add new KeyWord, to change the order if you want to prioritize a KeyWord more than an other.
You can't use more than 63 KeyWords, but you can create a second formula from 64 to 126.

Power Query: Adding characters to a set limit across several columns/rows

Very new to PQ, and I'm pretty sure it can do what I need in this situation, but I need help figuring out how to get there.
I have a timesheet report with 20 columns covering 50 rows that will need to be formatted to a word doc for uploading into a separate system. The original data in the cells range from 0 to any negative 2 digit number (ex: "-20"), but they need to be formatted to a seven-character set ending in ".00".
Examples:
0 will need to become "0000.00"
-4 will need to become "-004.00"
-25 will need to become "-025.00"
I think I should be able to use the text.insert function, but I'm not familiar enough with M Language to get it to do what I want it to do.
Any solutions/suggestions?
Here's my previous answer revisited...set up to use a function. You can just invoke the function once for each column you want to reformat. You'll just pass the name of the column you want to reformat to the function as you invoke the function each time.
Create a new blank query:
Open the new query in Advanced Editor and highlight everything in it:
Paste this over the highlighted text in the Advanced Editor:
let
FormatIt = (SourceColumn) =>
let
Base = Number.Round(SourceColumn,2)*.01,
Source = try Text.Start(Text.Range(
if Base < 7 then Text.From(Base) & "001" else
Text.From(Base),0,7),2) & Text.Range(Text.Range(
if Base < 7 then Text.From(Base) & "001" else
Text.From(Base),0,7),3,2) & "." & Text.End(Text.Range(
if Base < 7 then Text.From(Base) & "001" else
Text.From(Base),0,7),2)
otherwise "0000.00"
in
Source
in
FormatIt
...and click Done.
You'll see a new function has been created and listed in the Queries list on the left side of the screen.
Then go to your query with the columns you want to reformat (click on the name of your query that has the numbers you want to change in it, on the left side of the screen) and...
Click Invoke Custom Function
And fill out the pop-up like this:
- You can make up a different New column name than Custom.1.
- Function Query is the name of your query you are calling (the one you just created when you pasted the code)...for me, it's called Query1.
- Source Column is the column with the numbers you want to format.
...and click OK.
You can invoke this function once for each column. It will create a new formatted column for each.
You can use this formula = Text.PadStart(Text.From([Column1]),4,"0")&".00") in PQ to add new column that looks similar to your needs.
Here's an admittedly "busy" formula to do it:
= Table.AddColumn(#"Changed Type", "Custom", each Text.Start(Text.Range(if Number.Round([Column1],2)*.01 < 7 then Text.From(Number.Round([Column1],2)*.01) & "001" else Text.From(Number.Round([Column1],2)*.01),0,7),2) & Text.Range(Text.Range(if Number.Round([Column1],2)*.01 < 7 then Text.From(Number.Round([Column1],2)*.01) & "001" else Text.From(Number.Round([Column1],2)*.01),0,7),3,2) & "." & Text.End(Text.Range(if Number.Round([Column1],2)*.01 < 7 then Text.From(Number.Round([Column1],2)*.01) & "001" else Text.From(Number.Round([Column1],2)*.01),0,7),2))
It assumes your numbers that you want formatted are in Column1 to start. It creates a new column...Custom...with the formatted result.
To try it out, start with Column1 already populated and loaded into Power Query; then click the Add Column tab and then the Custom Column button, and populate the pop-up window like this:
...and click OK.
With more time, the repetitive parts could be made with variables to shorten this up a bit. This could also be turned into a function, given some time. But I don't have the time right now, so I figured I'd give you at least "something."

Copying all #mentions and #hashtags from column A to Columns B and C in Excel

I have a really large database of tweets. Most of the tweets have multiple #hashtags and #mentions. I want all the #hashtags separated with a space in one column and all the #mentions in another column. I already know how to extract the first occurrence of a #hashtag and a #mention. But I don't know to get them all? Some of the tweets have as much as 8 #hashtags. Manually going through the tweets and copy/pasting the #hashtags and #mentions seem an impossible task for over 5,000 tweets.
Here is an example of what I want. I have Column A and I want a macro that would populate columns B and C. (I'm on Windows &, Excel 2010)
Column A
-----------
Dear #DavidStern, #spurs put a quality team on the floor and should have beat the #heat. Leave #Pop alone. #Spurs a classy organization.
Live broadcast from #Nacho_xtreme: "Papelucho Radio"http://mixlr.com nachoxtreme-radio … #mixlr #pop #dance
"Since You Left" by #EmilNow now playing on KGUP 106.5FM. Listen now on http://www.kgup1065.com  #Pop #Rock
Family Night #battleofthegenerations Dad has the #Monkeys Mom has #DonnieOsman #michaelbuble for me #Dubstep for the boys#Pop for sissy
#McKinzeepowell #m0ore21 I love that the PNW and the Midwest are on the same page!! #Pop
I want Column B to look like This:
Column B
--------
#DavidStern #Pop #Spurs
#mixlr #pop #dance
#Pop #Rock
#battleofthegenerations #Monkeys #DonnieOsman #Dubstep #Pop
#pop
And Column C to look like this:
Column C:
----------
#spurs #heat
#Nacho_xtreme
#EmilNow
#michaelbuble
#McKinzeepowell #m0ore21
Consider using regular expressions.
You can use regular expressions within VBA by adding a reference to Microsoft VBScript Regular Expressions 5.5 from Tools -> References.
Here is a good starting point, with a number of useful links.
Updated
After adding a reference to the Regular Expressions library, put the following function in a VBA module:
Public Function JoinMatches(text As String, start As String)
Dim re As New RegExp, matches As MatchCollection, match As match
re.pattern = start & "\w*"
re.Global = True
Set matches = re.Execute(text)
For Each match In matches
JoinMatches = JoinMatches & " " & match.Value
Next
JoinMatches = Mid(JoinMatches, 2)
End Function
Then, in cell B1 put the following formula (for the hashtags):
=JoinMatches(A1,"#")
In column C1 put the following formula:
=JoinMatches(A1,"#")
Now you can copy just the formulas all the way down.
you could convert text to columns using the other character #, then against for #s and then concatenate the rest of the text back together for column A, if you are not familiar with regular expressions see (#Zev-Spitz)

Excel formula contains error

I have an error in this excel formula and I can't just figure it out:
=LEFT(B3,FIND(",",B3&",")-1)&","&RIGHT(B3,LEN(B3)-FIND("&",B3&"&")),RIGHT(B3,LEN(B3)-SEARCH("#",SUBSTITUTE(B3," ","#",LEN(B3)-LEN(SUBSTITUTE(B3," ",""))))&", "&SUBSTITUTE(RIGHT(B3,LEN(B3)-FIND("&",B3&"&")-1),RIGHT(B3,LEN(B3)-SEARCH("#",SUBSTITUTE(B3," ","#",LEN(B3)-LEN(SUBSTITUTE(B3," ",""))))),""))
It may seem like a big formula, but all it's intended to do is if no ampersand is in a cell, return an empty cell, if no comma but ampersand exists, then return this, for example:
KNUD J & MARIA L HOSTRUP
into this:
HOSTRUP,MARIA L
Otherwise, there is no ampersand but there is a comma so we just return: LEFT(A1,FIND("&",A1,1)-1).
Seems basic, but formula has been giving me error message and doesn't point to the problem.
Your error is here:
=LEFT(B3,FIND(",",B3&",")-1)&","&RIGHT(B3,LEN(B3)-FIND("&",B3&"&")),
At this point, the comma doesn't apply to anthing, because the right operator has matching parens
As far as what you want? Let's break that up into what you actually asked for:
if no ampersand in a cell, return empty cell,
B4=Find("&", B3&"&")
B5=IF(B4>LEN(B3),"",B6)
if no comma but ampersand exists
B6=IF(FIND(",", B3&",")>LEN(B3),B8,B7)
then turn this, for example:
KNUD J & MARIA L HOSTRUP
into this:
HOSTRUP,MARIA L
I'm presuming you mean to put the last whole word? Let's mark the last whole word:
B9=SUBSTITUTE(B3," ","#",LEN(B3)-LEN(SUBSTITUTE(B3," ","")))
B10=RIGHT(B7,LEN(B9)-FIND("#",B9))
And the stuff between the ampersand and the last word
B11=TRIM(MID(B9,B4 + 1, LEN(B9)-FIND("#",B9)-1))
Then calculating it is easy
B7=B10&","&B11
Otherwise, there is no ampersand but there is a comma so we just return:
LEFT(A1,FIND("&",A1,1)-1).
Well, if you want that, let's just put that in B8
B8=LEFT(A1,FIND("&",A1,1)-1)
(But I think you actually mean B3 instead of A1)
B8=LEFT(B3,FIND("&",B3,1)-1)
And there you have it (B5 contains the information you're looking for) It took a few cells, but it's easier to debug this way. If you want to collapse it, you can (but doing so is more code, because we can reduce duplication by referencing a previously calculated cell on more than one occasion).
Summary:
B3=<Some Name with & or ,>
B4=FIND("&", B3&"&")
B5=IF(B4>LEN(B3),"",B6)
B6=IF(FIND(",", B3&",")>LEN(B3),B7,B8)
B7=B10&","&B11
B8=LEFT(B3,FIND("&",B3,1)-1)
B9=SUBSTITUTE(B3," ","#",LEN(B3)-LEN(SUBSTITUTE(B3," ","")))
B10=RIGHT(B9,LEN(B9)-FIND("#",B9))
B11=TRIM(MID(B9,B4 + 1, LEN(B9)-FIND("#",B9)-1))
When I put in "KNUD J & MARIA L HOSTRUP", I get "HOSTRUP,MARIA" in B5.

Resources