Is there a quick way to put quotation marks around strings in an array?

  • #1
Darkmisc
203
27
TL;DR Summary
I'd like to cut and paste a list of words into an array. I'll need to put quotation marks around them all. Is there a way to do this other than to go through each word one by one?
Hi everyone

I'd like to cut and paste a list of words into an array. I'll need to put quotation marks around them all. Is there a way to do this other than to go through each word one by one?

Thanks
 
Technology news on Phys.org
  • #2
You could make one long string and then parse it to get the words.

Depends on the language you're using.

As an example, in Python:
Python:
s="aaa bbb ccc ddd eee fff"
d=s.split(" ")
print(d)

and you'll see:
Code:
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff']
 
  • Like
Likes FactChecker and Darkmisc
  • #3
Is your list comma-deliminted? If so, import it into a word processor that can handle text files and replace all commas with quote-comma-quote. Fix the first and last entry and you';re done.
 
  • Like
Likes Darkmisc
  • #4
jedishrfu said:
You could make one long string and then parse it to get the words.

Depends on the language you're using.

As an example, in Python:
Python:
s="aaa bbb ccc ddd eee fff"
d=s.split(" ")
print(d)

and you'll see:
Code:
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff']
This code is basically just splitting the words. The OP also says that they want quotation marks around each word. I presume that when the OP parses that list of words, for each element in that list, they would want the character at positions 0 and -1 to be '. This can be achieved by a simple modification of your code:
Python:
>>> d = ["\'" + word + "\'" for word in s.split(" ")]
>>> d
["'aaa'", "'bbb'", "'ccc'", "'ddd'", "'eee'", "'fff'"]
>>> (d[0])[0]
"'"
>>> (d[0])[-1]
"'"
 
  • Like
Likes Darkmisc
  • #5
Vanadium 50 said:
Is your list comma-deliminted? If so, import it into a word processor that can handle text files and replace all commas with quote-comma-quote. Fix the first and last entry and you';re done.
Some care is needed with word processors, which sometimes replace quotes with the better looking "smart quotes". But I'd do similar with :s/,/","/ in vi. 😁
 
  • Like
Likes Darkmisc
  • #6
Ibix said:
in vi.
I was going to suggest sed. Usually sed won't solve your problems - but it will put them in perspective.
 
  • #7
Is this something that needs to be done just once, or many times?

Do you want to prepare an array declaration statement in a program, using a fixed set of words? Then you run the program repeatedly using the same set of words. Maybe update the set of words only occasionally, such that editing the code is not a problem.

Or are you going to use a different set of words every time you run the program? In that case, you'd probably want to have the words in a separate file that the program reads in order to generate the array.
 
  • Like
Likes Darkmisc
  • #8
jtbell said:
Is this something that needs to be done just once, or many times?

Do you want to prepare an array declaration statement in a program, using a fixed set of words? Then you run the program repeatedly using the same set of words. Maybe update the set of words only occasionally, such that editing the code is not a problem.

Or are you going to use a different set of words every time you run the program? In that case, you'd probably want to have the words in a separate file that the program reads in order to generate the array.
For now, it's just once. Easiest method was Vanadium50's. I'd completely forgotten that I could use something other than Godot to solve the problem.
 

Similar threads

  • Programming and Computer Science
Replies
1
Views
380
  • Programming and Computer Science
Replies
4
Views
489
  • Programming and Computer Science
Replies
1
Views
456
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
11
Views
953
  • Programming and Computer Science
Replies
3
Views
256
  • Engineering and Comp Sci Homework Help
2
Replies
43
Views
4K
  • Programming and Computer Science
Replies
15
Views
2K
Replies
6
Views
1K
  • Programming and Computer Science
Replies
12
Views
11K
Back
Top