Regular Expressions and For Statement With Counter

An expression like this works by itself:

 for i in 0..10 do
 printf "%d" i

What makes it work when one of these values is substituted by a variable preluded by another complex expression such as a regular expression?

let str = File.ReadAllText "input.csv"
let result = Regex.Match(str, "\d+").ToString()
 
 for i in 0..result do
 printf "%d" i 

The error I get when I attempt to execute your code is

  for i in 0..result do
  ------------^^^^^^

stdin(14,13): error FS0001: This expression was expected to have type
    'int'
but here has type
    'string'

It’s pointing out that it can’t implicitly convert a string (result) to an int, but it has to be an int for that loop to work. You’ll need to parse or otherwise convert result to an int for it to compile.

1 Like

Perfect. Thanks NT. This worked for me:

let str = File.ReadAllText “input.csv”
let result = Regex.Match(str, “\d+”)
let Str2Int = Res.ToString() |> int

for i in 0…Str2Int do
printfn “%A” i

1 Like