Parsing text File and extracting information in perl - perl

I have a code with multiple functions
void fun_one()
{
tempa=10;
tempb=10;
}
void fun_two()
{
tempc=12;
tempd=13;
}
I want perl code that extracts function name if i input variable name;
say I search for "tempa", it should extract "fun_one" into a variable;
say I search for "tempd", it should extract "fun_two" into a variable;
I have extracted line number
open my $code, '<', 'code.txt' ;
my $keyword = "tempa";
my $regex = qr|\b($keyword)\b|;
while ($code>)
{
while (/$regex/g)
{
print "$.\n";
}
}
Output:3
I need some logic to extract fuction name.
thanks in advance

You might need a parser instead of just regexes.
But...
sub find_function_using_variable {
my $variable = shift;
my $last_func="";
while (<>) {
$last_func = $1 if m/void (\w+)\(\)/;
print $last_func."\n" if m/\Q$variable\E/;
}
}

Related

How to split string by comma into a hash in Perl?

I have the following code:
my #logs = split(",",$opts->{"logs"});
$opt_href->{"logs"} = \#logs;
It basically splits the $opts->{"logs"} by comma and keeps the array ref. Later I need to check if string exists in the $opt_href->{"logs"} array. Looking at this topic, I see that it's recommended to keep a hash, instead of array. I could just do:
my %logs;
for each my $log (split(",",$opts->{"logs"})) {
$logs{$log} = 1;
}
$opt_href->{"logs"} = \%logs;
Is there a better way to do this? Maybe a one/two liners?
my %logs = map { $_ => 1 } split /,/, $opts->{logs};
$opt_href->{logs} = \%logs;
Or, using the anonymous hash reference, constructed by { }
$opt_href->{logs} = { map { $_ => 1 } split /,/, $opts->{logs} };

how can i Create indexed variable in perl?

I am new to perl, how can i create indexed variable like $Num0, $Num1 and $value0, $value1. I have to store some value from hash in this variable.
$Num0 = $req->{value0};
$Num1 = $req->{value1};
$Num2 = $req->{value2};
is it possible to crate both variable Num0,Num1 and value0,value1 using some logic based on indexing like below.
while($i < 5)
{
$Num.$i = $req->{value$i};
}
You can use perl arrays.
my #num;
my $i=0;
while ($i<5) {
$num[$i] = $req->{"value$i"};
$i++;
}
See perl cheatsheet for concise help on perl.
#ikegami suggested some alternative ways to do the same thing in comments:
my #num = map { $ref->{"index$_"} } (0..4);
and
my #num;
for my $i (0..4) {
push #num, $ref->{"index$i"};
}
See help on map and push.

Replace a variable as string with results from a subroutine in perl

I am trying to replace the variable "ex" with it's string value(which it gets from subroutine "potatoex()" and put it in the definition of another variable "goodge",
not getting any syntax error but this seems to be not working out.
Please help.
sub potatoex {
my $potato="Junos: 17.4DCB";
if($potato = ~/"Junos: 17.4[a-zA-Z0-9_.]*"/) {
print "/var/home/smoketest/dhcpv6.pl.28709.log";
}
}
main :
{
my $ex= potatoex();
my $goodge= "Apurva $ex Arnav";
print $goodge;
}
CURRENT O/P : /var/home/smoketest/dhcpv6.pl.28709.logApurva 1 Arnav
EXPECTED O/P: Apurva /var/home/smoketest/dhcpv6.pl.28709.log Arnav
Thanks,
Apurva
You are printing from your subroutine instead of returning the value from it. Try using return in potatoex.
sub potatoex {
my $potato = q{Junos: 17.4DCB};
my $returnVal = '';
if( $potato =~ /Junos: 17\.4[a-zA-Z0-9_.]*/ ) {
$returnVal = q{/var/home/smoketest/dhcpv6.pl.28709.log};
}
$returnVal;
}
{
my $ex = potatoex();
my $goodge = qq{Apurva $ex Arnav};
print $goodge;
}
While you could use return in an if block above (instead of print), I instead decided to use a variable, which will always return either the value you are trying to or an empty string. You could explicitly state the return return returnVal;, but it isn't required as perl allows implicit returns (make note of this and figure out if it should fit in your best practices or not).

php run method and get variable in one line

I want to get variable after running class function. I usually do it in 2 lines:
$object->function();
$var = $object->function_variable;
Can I do it somehow in one line?
$var = $object->function()->function_variable;
UPDATE
In general I want to call for different vars from one function in different files
$var = $object->function()->function_variable1;
$var = $object->function()->function_variable2;
$var = $object->function()->function_variable3;
Why doesn't function() easily return your function_variable?
You need to learn about the basics of oop,
If the function returns the value that you want to assign to $var, then you can do:
$var = $object->function();
I suppose you want to access instance variable not function variable. So it will be like:
$var = $object->function()->instance_variable;
For this to work your function should return an object with those instance variables, then only you will be able to achieve this.
For more clarity see the below example:
<?php
class Name
{
public $fname, $lname;
function __construct($fname, $lname)
{
$this->fname = $fname;
$this->lname = $lname;
}
}
class Person
{
private $name;
public function __construct($fname, $lname)
{
$this->setName($fname, $lname);
}
public function getName()
{
return $this->name;
}
private function setName($fname, $lname)
{
$this->name = new Name($fname, $lname);
}
}
$p = new Person('Raj', 'Mishra');
$name = $p->getName();
print $var = $name->fname; // $var = $object->instance_variable1;
print " ";
print $var = $p->getName()->lname; // $var = $object->function()->instance_variable2;
?>

How can I return a whole hash map in Perl?

I have a hash called
%values
Now I want to return the whole hash in a subroutine
sub getvalues {
return $values;
}
But then I got an error, because $value needs a definition and my program stops. If I'm using
sub getvalues {
return %values;
}
it seems to work, but my program is very slow and don't get further... So how can I return the whole map?
It would be nice to return the hash reference instead of hash,what you need to do is
First stote the hash into the hash ref then return it like
sub getvalues {
my %values = (test => "SO");
my $values = \%values;
return $values;
}