# Challenge 4 - World Cup

**URL:** <https://forum.kx.com/t/challenge-4-world-cup/13819>\
**Category:** Community Support\
**Tags:** imported, kdb-and-q\
**Created:** [March 7, 2023, 12:00am UTC](https://forum.kx.com/t/challenge-4-world-cup/13819 "2023-03-07T00:00:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmcparland](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/mmcparland/32/54_2.png) [@mmcparland](https://forum.kx.com/u/mmcparland)\
**Post date:** [March 7, 2023, 12:00am UTC](https://forum.kx.com/t/challenge-4-world-cup/13819/1 "2023-03-07T00:00:00Z")

</div>

[https://learninghub.kx.com/forums/topic/challenge-4-world-cup](https://learninghub.kx.com/forums/topic/challenge-4-world-cup)

Hi everyone! This week I wanted to try something a little more challenging but I’m going to need a bit of help completing it. Below I have attached a text file containing the goals scored during the World Cup. The data is formatted as;

Player;country;time;

Time represents the minute in the game the goal was scored.

With this data we can produce a lot of different statistics for example;

- Total number of goals scored by a given country
- Total number of goals scored by a given player
- List the name of all the players who scored for a given country
- Total number of goals by all countries
- Total number of goals scored during the first half (45 minutes)
- Total number of goals scored during the second half (45 minutes to 90 minutes)
- Total number of goals scored during extra time (after 90 minutes of play)

For now I just want to focus on the first bullet point.

First I thought it would be ideal to transform the data to table format, then simply count the rows of the country selected;

```
q)data:flip`player`country`minute!("SSS";";")0:`:goals.txt 
q)count select from data where country=`Spain 
7
```

I also wanted to loop through the text file and print (player + from + country + scored a goal at the + minute + th minute) for each player. Which seemed easy at first but really had difficulty.

I reckoned a function was most definitely needed but couldn’t wrap my head around how to loop not only through the 3 items but then onto the rest of the list.

Any help / advice would be greatly appreciated !!

Megan

---

<div class="post-metadata">

**Author:** ![lkerr2](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/lkerr2/32/57_2.png) [@lkerr2](https://forum.kx.com/u/lkerr2)\
**Post date:** [July 3, 2023, 12:00am UTC](https://forum.kx.com/t/challenge-4-world-cup/13819/2 "2023-07-03T00:00:00Z")

</div>

You can do it through qsql syntax:

```
exec (string[player],'" from ",/:string[country],'" scored a goal in the ",/:string[minute],:"th minute") from data
```

Useful information for this can be found here:  
[https://subhabratachoudhury.com/p/2016/05/concatenating-kdb-columns/](https://subhabratachoudhury.com/p/2016/05/concatenating-kdb-columns/")  
If you want to display to console each of these you can put a **0N! each** in front of the above exec state.

---

<div class="post-metadata">

**Author:** ![mmcparland](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/mmcparland/32/54_2.png) [@mmcparland](https://forum.kx.com/u/mmcparland)\
**Post date:** [August 3, 2023, 12:00am UTC](https://forum.kx.com/t/challenge-4-world-cup/13819/3 "2023-08-03T00:00:00Z")

</div>

This is exactly the output I wanted thanks ! Plus that article you tagged is very useful.

```
q) data:flip`player`country`minute!("SSS";";")0:`:goals.txt 
q) exec (string[player],'" from ",/:string[country],'" scored a goal in the ",/:string[minute],:"th minute") from data 
"Gazinsky from Russia scored a goal in the 12th minute" 
"Cheryshev from Russia scored a goal in the 43th minute" 
"Cheryshev from Russia scored a goal in the 91th minute" 
"Dzyuba from Russia scored a goal in the 71th minute" 
"Golovin from Russia scored a goal in the 94th minute" 
"Gimenez from Uruguay scored a goal in the 89th minute" 
"Cheryshev from Russia scored a goal in the 59th minute" 
"Dzyuba from Russia scored a goal in the 62th minute" 
"OG from Russia scored a goal in the 47th minute" 
"Salah from Egypt scored a goal in the 73th minute" 
"Suarez from Uruguay scored a goal in the 23th minute" 
"Suarez from Uruguay scored a goal in the 10th minute"...
```

However when I try putting **0N! each** in front of the statement I get a parse error?

```
q) data:flip`player`country`minute!("SSS";";")0:`:goals.txt 
q) 0N! each exec (string[player],'" from ",/:string[country],'" scored a goal in the ",/:string[minute],:"th minute") from data 
parse error
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![jbetz34](https://avatars.discourse-cdn.com/v4/letter/j/5e9695/32.png) [@jbetz34](https://forum.kx.com/u/jbetz34)\
**Post date:** [August 3, 2023, 12:00am UTC](https://forum.kx.com/t/challenge-4-world-cup/13819/4 "2023-08-03T00:00:00Z")

</div>

Hi Megan,

If all you want is to print these statements, you don’t necessarily need to cast the columns to symbols or save in a table. Try the following one-liner:

{x," from “,y,” scored a goal in the “,z,“th minute”}./: flip(”\*\*\*“;”;") 0:`:goals.txt

By default this prints to the console, so I don’t see why you would need to include the “0N!” .

The reason you are getting a parse error on the "0N! each " is because “0N!” is an infix function followed by a postfix iterator. You will have to wrap "0N! in parenthesis (see [https://code.kx.com/q/basics/syntax/#prefix-infix-postfix](https://code.kx.com/q/basics/syntax/#prefix-infix-postfix")). Also worth noting that “0N!/:” will work here as well. However, I think even this is unnecessary as “0N!” is unary and natively handles a list of strings.
