# Assign value() output to a Groovy variable

**URL:** <https://community.seqera.io/t/assign-value-output-to-a-groovy-variable/1609>\
**Category:** Ask for help\
**Tags:** nextflow\
**Created:** [January 13, 2025, 9:53pm UTC](https://community.seqera.io/t/assign-value-output-to-a-groovy-variable/1609 "2025-01-13T21:53:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![StevenWingett](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/stevenwingett/32/838_2.png) [@StevenWingett](https://community.seqera.io/u/StevenWingett)\
**Post date:** [January 13, 2025, 9:53pm UTC](https://community.seqera.io/t/assign-value-output-to-a-groovy-variable/1609/1 "2025-01-13T21:53:36Z")

</div>

Hi Everybody,

Sorry if I’ve missed something obvious, as this question seems quite basic.

**The code below writes 5 to standard out. How do I capture that value and assign to a Groovy variable?**

```
x = Channel
    .from(1, 2, 3, 4, 5)  

x.count().view()

```

(As an aside: the reason I want to do this is that I’ve written a pipeline that maps FASTQ files and quantifies the results. The pipeline then needs to combine the results from each replicate. However, if there is only 1 replicate then the pipeline should not run the combining script.

Therefore, I’ve tried to add logic to my pipeline which assesses the number of samples in a channel and runs the combining script accordingly.)

There must be an easy answer to this, but I just can’t see it. Any help would be much appreciated.

Thanks very much!

Steven

---

<div class="post-metadata">

**Author:** ![mahesh.binzerpanchal](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/mahesh.binzerpanchal/32/253_2.png) [@mahesh.binzerpanchal](https://community.seqera.io/u/mahesh.binzerpanchal)\
**Post date:** [January 14, 2025, 8:11am UTC](https://community.seqera.io/t/assign-value-output-to-a-groovy-variable/1609/2 "2025-01-14T08:11:17Z")

</div>

The reason it’s not easily findable is because this isn’t the way you should approach the problem.

What you want to do is use either `branch`, or probably better, `filter`, to do conditional process execution.

Using either `collect` or more likely `groupTuple`, you can then `filter` inputs.

```groovy
ch_analysed = ANALYSE_REPLICATE.out.report
    .groupTuple() // Group together replicates or samples based on key
MERGE_REPORT( ch_analysed.filter{ meta, reports -> reports.size() > 1 } )
ch_reports = ch_analysed.filter{ meta, reports -> reports.size() == 1 }
    .map { meta, reports -> tuple( meta, reports.head() ) } // Unlist singleton report
    .mix( MERGE_REPLICATES.out.report )

```

---

<div class="post-metadata">

**Author:** ![StevenWingett](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/stevenwingett/32/838_2.png) [@StevenWingett](https://community.seqera.io/u/StevenWingett)\
**Post date:** [January 14, 2025, 4:47pm UTC](https://community.seqera.io/t/assign-value-output-to-a-groovy-variable/1609/3 "2025-01-14T16:47:25Z")

</div>

Thanks very much for your reply. I shall try this.
