Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to use the outcome of prgram in another program

+8 votes
asked Jun 6, 2025 by Max (200 points)
I have a Ruby program that transforms a tabel to a grammar.

The output looks like "GRAMMAR = {" bla bla bla }

in a second program a use this grammar as input for performing another task.

What I do now is copy/paste the outcome of the first program in the second program. I want to combine the programs into one program. In other words: how can I make the outcome of the fist as a public variable in the second?

1 Answer

0 votes
answered Jun 10, 2025 by Peter Minarik (101,340 points)

There are 2 fundamental ways to deal with this.

Pass data between functions

You could turn your programs into individual functions, and you could pass data between these functions. Whatever Function 1 returns (make it return outpu as data, instead of printing it on the screen) should be consumed as a parameter by Function 2.

I do not know Ruby, but you can look at this tutorial to learn about functions.

In C, the code would look something like this.

#include <stdio.h>

char * Function1()
{
    char * data = NULL/* produce your data, instead of NULL */;
    return data;
}

void Function2(const char * data)
{
    // Do someting with data
}

int main()
{
    char * dataFromFunction1 = Function1();
    Function2(dataFromFunction1);
    
    return 0;
}

Pass data between programs

You can keep your individual programs and connect them together. This assumes a Program1 writes the standard output while Program2 reads the standard input.

Then, in a command shell you could invoke Program1 and pipe its output to Program2. It would look something like this:

Program1 | Program2

This needs you to run your command in a shell. If you use the OnlineGDB, then you can save the output as a file instead in Program1 and read from a file in Program2

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...