Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a code like:
my ($line_1, $line2);
variables $line_1 and $line_2 are getting values from other function, that may be a defined or undefined value.
Now I am getting an error like " Use of uninitialized value" evenafter i have initialised like
$line_1 = " " if(!$line_1);
PLease help me in this
To check for the definedness (whether it is undef or not) of a variable, use the defined operator.
If you still get the warning, perl is right and your code is wrong. No magic here.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am looking at Perl script and I have a line:
my (#parmTypesList) = #$function_type_ref;
$function_type_ref is a string that is passed to the current function.
What does #$ means ?????
#$var is short for #{ $var } and equivalent to $var->#*. It is an array dereference.
It expects $var to hold a reference to an array, and it produces one of the following:
In scalar context, the number of elements of the array
In list context, the value of the elements of the array.
When an array is expected (e.g. #$var = ..., push #$var, ...), the array itself.
In this case, it's the second.
Despite the name, in no way would a reference to a function work.
But a string might work, since a string can be used as a reference. In this situation, the string is expected to be the name of the variable. This is a horrible, horrible thing to do, so we tell Perl not to let us do this by using use strict; or use v5.12;.
Just to be clear: You should ALWAYS use use strict; or equivalent.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This is my sample code
#!/usr/bin/perl
$file = SUN;
$file1 = abc.$file.cde
print "Value is : $file1\n"
I want Output like this abcSUNcde
What regular expression need for this.
As well ikegami said in his comment you are missing the semicolons at the end of the line (terminating the line or line should be ended).
#!/usr/bin/perl
$file = SUN;
$file1 = "abc${file}cde";
print "Value is : $file1\n";
This is will print abcSUNcde as an output of your code.
In case if you want to remove the special characters (what I understand except keyboarding characters)
$file2=~s/[^A-Za-z]//g;
Let try this and modify as well you can.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Here by i tried to swap the variables using perl.
#!/usr/local/bin/perl
use strict;
use warnings;
my $v1=23;
my $v2=43;
$v1,$v2)=($v2,$v1)
print $v1,$v2;
Error:
syntax error at exchange.pl line 7, near ")
print"
Execution of exchange.pl aborted due to compilation errors.
Excepted output:
43,23
You forgot open parens and semicolon:
($v1,$v2)=($v2,$v1);
print $v1,$v2;
Expected output should be 4323 (4 chars and no comma), not 43,23.
To print 43,23:
print "$v1,$v2";
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
.a_b_c(x_y_z), this is my string so the question is how to get only a_b_c
Thanks in advance
It is probably best to use a regex pattern for this
use v5.10;
my $s = '.a_b_c(x_y_z)';
my ($ss) = $s =~ /(\w+)/;
say $ss;
output
a_b_c
my $s = '.a_b_c(x_y_z)';
print substr $s =~ s/\(.*\)//r, 1;
The substitution removes the parenthesised part, substr removes the dot.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is the difference between sprintf and printf in Perl?
I'm really confused with those functions.
I know about printf. It is used for STDOUT, but I want know in depth of these functions.
sprintf just returns a formatted string, printf prints it to a filehandle.
printf HANDLE "%s", $arg
can (very redundantly) be written as
$formatted = sprintf "%s", $arg
print HANDLE $formatted
Of course, this specific example is most naturally written as
print HANDLE $arg
because the format string I used for an example is so trivial as to be useless.
Of course, HANDLE is optional, and defaults to STDOUT, although you can also change the default with select.