# Variables declared in function seem to get overridden when called multiple times in parallel

**URL:** <https://community.seqera.io/t/variables-declared-in-function-seem-to-get-overridden-when-called-multiple-times-in-parallel/579>\
**Category:** Ask for help\
**Created:** [March 21, 2024, 9:09am UTC](https://community.seqera.io/t/variables-declared-in-function-seem-to-get-overridden-when-called-multiple-times-in-parallel/579 "2024-03-21T09:09:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bskubi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/bskubi/32/545_2.png) [@bskubi](https://community.seqera.io/u/bskubi)\
**Post date:** [March 21, 2024, 9:09am UTC](https://community.seqera.io/t/variables-declared-in-function-seem-to-get-overridden-when-called-multiple-times-in-parallel/579/1 "2024-03-21T09:09:18Z")

</div>

I have a function to extract the filename prefix from an absolute path.

```auto
def string_prefix(String file_path, String where_from, String sep = '.') {
    
    f = new File(file_path)
    full_filename = f.getName()
    print file_path + " " + where_from + " " + full_filename
    last_dot = full_filename.lastIndexOf(sep)
    if (last_dot > 0) {
        prefix = full_filename.substring(0, last_dot)
    }
    else {
        prefix = full_filename
    }
    
    return prefix
}

```

This is called several times in my workflow to extract a sample ID prefix from a non-fastq path. Here’s an example:

```auto
        channel
            .fromPath(params.sam)
            .map{tuple(string_prefix(it.toString(), "convert_sam_to_bam"), it)}
            .combine(genrf_ch)
            .set{sam_ch}

```

I’m finding that although `file_path` and `where_from` are consistently correct, the `full_filename` variable does not consistently get named after the prefix in `file_path`. Instead, it seems to randomly be set to the prefix for any one of the `file_path` values submitted from the various calls to `string_prefix` that occur during the workflow.

Here’s an example. In each line, the final path should have the same prefix as the absolute path that precedes it. Instead, the first one is set to the same value as the third one. The mix-up is different with each run.

```auto
/home/benjamin/Documents/hich/sambam/KO_br1_tr2.sam convert_sam_to_bam KO_br1_tr1.bam
/home/benjamin/Documents/hich/pairs/test.pairs.lz4 parsing_wf test.pairs.lz4
/home/benjamin/Documents/hich/sambam/KO_br1_tr1.bam start_as_bam KO_br1_tr1.bam

```

It seems like somehow, the values of variables declared in the string\_prefix function are getting overwritten by the values of the same variables in other calls to `string_prefix`. I assume this is somehow due to a behavior of Nextflow, but I’m not experienced enough with either Groovy or Nextflow to figure it out. Any help is appreciated!

---

<div class="post-metadata">

**Author:** ![mribeirodantas](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/mribeirodantas/32/235_2.png) [@mribeirodantas](https://community.seqera.io/u/mribeirodantas)\
**Post date:** [March 21, 2024, 11:44am UTC](https://community.seqera.io/t/variables-declared-in-function-seem-to-get-overridden-when-called-multiple-times-in-parallel/579/2 "2024-03-21T11:44:43Z")

</div>

Try declaring these variables as local variables by setting them with `def`. Read more about it [here](https://code-maven.com/groovy-variable-scope#:~:text=Declare%20local%20variable%20with%20def,saw%20in%20the%20first%20example).

---

<div class="post-metadata">

**Author:** ![bskubi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/bskubi/32/545_2.png) [@bskubi](https://community.seqera.io/u/bskubi)\
**Post date:** [March 21, 2024, 4:39pm UTC](https://community.seqera.io/t/variables-declared-in-function-seem-to-get-overridden-when-called-multiple-times-in-parallel/579/3 "2024-03-21T16:39:16Z")

</div>

That solve the problem, thank you so much! And thanks for the link as well.
