1

I'm trying to get information from a .txt file:

function1(par1, par2)
function2(par1)
function3(par1, par2, par3)

I would like to get for example for the first line "function1" as a string, "par1" and "par2" as strings.

I now it is possible to extract a pattern from a string, but I would like to know if it was possible to get a substring using the index of its caracters.

For example as in Python :

$function = $row[0:8]

would get me "function1"

Thanks,

SLP

3
  • what is it you are trying to get? from what I see you can do the same in perl with substrings... Commented Feb 28, 2019 at 12:45
  • I want to separate the string into different substrings. For example I would ideally have "function1" as a string in a variable, "par1" and "par2" in other variables Commented Feb 28, 2019 at 12:51
  • If your input has more on each line -- or different lines mixed with the ones you show -- that makes a difference for any approach; please update the question if that is so. (In my answer I assume that you only need to process lines as shown,) Commented Feb 28, 2019 at 18:22

2 Answers 2

5

To parse programming text of a function call, as shown in the sample input

my $string = 'function1(par1, par2)';

my ($func_name, @params) = split /\s*\(\s*|\s*,\s*|\s*\)/, $string;

where split builtin uses a regex for the separator pattern, and we tell it to break up the string by either ( or , or ) (I also include the closing parenthesis so that the last element wouldn't be stuck with it). The regex pattern also has possible spaces sprinkled around.

If your .txt file has literally lines like what you show then you can simply apply the above to each line. If there is more on each line though then you'd need to preprocess that first (or use a different approach); please show the realistic input if lines have more than what is shown.

Sign up to request clarification or add additional context in comments.

Comments

2

If you want the same behaviour as your Python example, the substr function will do what you want.

The code:

my $a = "function1(par1, par2)";
print substr $a, 0, 8

produces "function" as the output

Per your comment "I want to separate the string into different substrings", if you have a variable number and length of function parameters, a regular expression is by far the best way to do this.

while (<>) {
  if (/^([^\(]*)\(([^\)]*)\)/) {
    my $f=$1;
    my @params=split ",", $2;
    print "Function: $f, Params: @params\n";
  }
}

Given the input

function1(par1, par2)
function2(par1)
function3(par1, par2, par3)

This code will print

Function: function1, Params: par1  par2
Function: function2, Params: par1
Function: function3, Params: par1  par2  par3

1 Comment

Ok thank you. I had already tried this but I must have had a mistake somewhere because it didn't produce the result expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.