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
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.
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