# Functions with Strings

**URL:** https://discourse.julialang.org/t/functions-with-strings/96054
**Category:** New to Julia
**Tags:** question
**Created:** [March 14, 2023, 2:05pm UTC](https://discourse.julialang.org/t/functions-with-strings/96054 "2023-03-14T14:05:49Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Sunshine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sunshine/32/47687_2.png) [@Sunshine](https://discourse.julialang.org/u/Sunshine)
#### Post date: [March 14, 2023, 2:05pm UTC](https://discourse.julialang.org/t/functions-with-strings/96054/1 "2023-03-14T14:05:49Z")

</div>

Hi All,

I am trying to build a function which takes words as the input and checks them against a table:

Colour = collect(“BROYWGIP”)

Colourtable = Table(name = [“Black”, “Red”, “Orange”, “Yellow”, “White”, “Green”, “Blue”, “Purple”], symbol = [Colour[1], Colour[2], Colour[3], Colour[4], Colour[5], Colour[6], Colour[7], Colour[8]], number = [0, 1, 2, 3, 4, 5, 6, 7])

function Check(x)  
v = map(@Compute($name == x), Colourtable)  
s = Int  
for i in 1:8  
if v[i] == true  
push!(s,1)  
elseif v[i] == false  
push!(s,0)  
else  
return “Error”  
end  
end  
a = Table(name = [“Black”, “Red”, “Orange”, “Yellow”, “White”, “Green”, “Blue”, “Purple”], present = [s[1],s[2],s[3],s[4],s[5],s[6],s[7],s[8]]),  
b = Table(Colourtable; present = a.present)  
end

I wrote the code as this but I am having an issue getting the initial variable x to be recognised in the v = map part.

Can anyone help suggest a work around? 🙂

All the best,

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [March 14, 2023, 2:25pm UTC](https://discourse.julialang.org/t/functions-with-strings/96054/2 "2023-03-14T14:25:20Z")

</div>

Welcome! I cannot run your code, can you please follow the guidelines below?

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

### Author: ![Sunshine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sunshine/32/47687_2.png) [@Sunshine](https://discourse.julialang.org/u/Sunshine)
#### Post date: [March 14, 2023, 2:38pm UTC](https://discourse.julialang.org/t/functions-with-strings/96054/3 "2023-03-14T14:38:27Z")

</div>

Thank you for responding, I have an issue running the code too, but the error comes from the Check(x). The possible inputs for x are supposed to be Black, Red, Orange, Yellow, White, Green, Blue, Purple. Anything else is supposed to return an error

I have tried to follow the guidelines:

```julia
using Tables, TypedTables

Colour = collect("BROYWGIP")

Colourtable = Table(name = ["Black", "Red", "Orange", "Yellow", "White", "Green", "Blue", "Purple"], symbol = [Colour[1], Colour[2], Colour[3], Colour[4], Colour[5], Colour[6], Colour[7], Colour[8]], number = [0, 1, 2, 3, 4, 5, 6, 7])

function Check(x)
	v = map(@Compute($name == x), Colourtable)
	s = Int[]
		for i in 1:8
			if v[i] == true
			push!(s,1)
			elseif v[i] == false
			push!(s,0) 
			else
			return "Error"
			end
		end
end

```

The additionally defined bits (a,b, and c) are just mappings I built to form a final table.

\*\*Edit, the indentations are not working when I post into the forum. Is there a way around this?

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [March 14, 2023, 2:40pm UTC](https://discourse.julialang.org/t/functions-with-strings/96054/4 "2023-03-14T14:40:04Z")

</div>

Please use triple backticks around your code so that discourse can format it:  
```  
_paste your code here_  
```

Also, include all packages needed to run your code.

---

<div class="post-metadata">

### Author: ![M-PERSIC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/m-persic/32/51968_2.png) [@M-PERSIC](https://discourse.julialang.org/u/M-PERSIC)
#### Post date: [March 14, 2023, 2:44pm UTC](https://discourse.julialang.org/t/functions-with-strings/96054/5 "2023-03-14T14:44:23Z")

</div>

There are a few issues with your present code:

- `collect(“BROYWGIP”)`: you have used an initial quote in the beginning, you must use " for the beginning and end of strings otherwise the first line will return an error (“BROYWGIP”)
- The `@Compute` macro and `Table` type are not part of the standard library, if they are part of a package please specify which packages they belong to
- Best to return an error via the `@error` macro, i.e. `@error "<error message here>"`

---

<div class="post-metadata">

### Author: ![Sunshine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sunshine/32/47687_2.png) [@Sunshine](https://discourse.julialang.org/u/Sunshine)
#### Post date: [March 14, 2023, 2:56pm UTC](https://discourse.julialang.org/t/functions-with-strings/96054/6 "2023-03-14T14:56:52Z")

</div>

Hi M-Persic,

Thank you for coming back. I have corrected the thread to show the package (apologies first time using the forum)

the

```julia
collect("BRYWGIP") 

```

returns an error when I use two single quotes, but provides a vector of size 8 when I use the double quote. perhaps this was a formating issue when I initially shows the code?

To help explain:

I want to build a map from colour to numbers. So if I have a colour that matches with one of the colours in the name row of the table, it will return a number vector.

Initially the number vector is in boolean (v), and so I convert it to a integer vector (s)

Finally I will want to glue the integer vector (s) to a copy of the table (Colourtable).

The ouptut of the function would be (ideally) the table produced by gluing the integer vector (s) to the table (Colourtable) copy

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [March 14, 2023, 3:03pm UTC](https://discourse.julialang.org/t/functions-with-strings/96054/7 "2023-03-14T15:03:08Z")

</div>

It seems to me like what you are doing should be a one or two-liner at most, but I struggle to see what exactly you are trying to do.

Maybe something like this?

```julia
julia> colour_dict = Dict(collect("BROYWGIP") .=> 0:7)
Dict{Char, Int64} with 8 entries:
  'P' => 7
  'I' => 6
  'Y' => 3
  'R' => 1
  'W' => 4
  'G' => 5
  'O' => 2
  'B' => 0

julia> get.(Ref(colour_dict), ['B', 'W', 'Z'], "Error")
3-element Vector{Any}:
 0
 4
  "Error"

```

---

<div class="post-metadata">

### Author: ![M-PERSIC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/m-persic/32/51968_2.png) [@M-PERSIC](https://discourse.julialang.org/u/M-PERSIC)
#### Post date: [March 14, 2023, 3:03pm UTC](https://discourse.julialang.org/t/functions-with-strings/96054/8 "2023-03-14T15:03:43Z")

</div>

> [@Sunshine](#):
>
> `collect("BRYWGIP") `

Yes, in Julia, a pair of single quotes ’ ’ delimit a character (`Char`), whereas pair of double quotes " " or “”" “”" delimit string literals.

In this instance, we don’t know how the `@Compute` macro functions, so it’s hard to diagnose why it is spitting out errors.

---

<div class="post-metadata">

### Author: ![Sunshine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sunshine/32/47687_2.png) [@Sunshine](https://discourse.julialang.org/u/Sunshine)
#### Post date: [March 14, 2023, 3:07pm UTC](https://discourse.julialang.org/t/functions-with-strings/96054/9 "2023-03-14T15:07:03Z")

</div>

Thank you Nilshg,

I am likely over complicating this as I get my head around the possible ways to code it!!

I will try this approach as well

---

<div class="post-metadata">

### Author: ![Sunshine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sunshine/32/47687_2.png) [@Sunshine](https://discourse.julialang.org/u/Sunshine)
#### Post date: [March 14, 2023, 3:09pm UTC](https://discourse.julialang.org/t/functions-with-strings/96054/10 "2023-03-14T15:09:26Z")

</div>

I found the @Compute macro in this part of the manual:

[https://typedtables.juliadata.org/stable/man/map/](https://typedtables.juliadata.org/stable/man/map/)

🙂

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [March 14, 2023, 4:12pm UTC](https://discourse.julialang.org/t/functions-with-strings/96054/11 "2023-03-14T16:12:36Z")

</div>

> [@Sunshine](#):
>
> part of the manual

you’re looking at the documentation of a package called `TypedTable`, not Julia’s own “manual”

---

<div class="post-metadata">

### Author: ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)
#### Post date: [March 14, 2023, 4:19pm UTC](https://discourse.julialang.org/t/functions-with-strings/96054/12 "2023-03-14T16:19:48Z")

</div>

Have you taken a look at enums? They are built in or there is a package with extended [functionality](https://discourse.julialang.org/t/ann-enumx-jl-improved-enums-for-julia/78096).
