Functions with Strings

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? :slight_smile:

All the best,

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

1 Like

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:

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?

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.

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>"

Hi M-Persic,

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

the

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

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> 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"
3 Likes

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.

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

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

https://typedtables.juliadata.org/stable/man/map/

:slight_smile:

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

1 Like

Have you taken a look at enums? They are built in or there is a package with extended functionality.