How can i remove special character from a variable using perl [closed] - perl

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.

Related

Need to grep a string from a line in a variable and store it in another variable [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a variable $abc that contains the line like below:
abc.txt -> check a/b/c test
I need to get abc.txt in another variable say $bcd and a/b/c in the variable say $xyz. I have the regex to do so but I don't know how I can do it using perl as in my knowledge perl grep and perl map can be used on arrays only not variables.
my ($bcd) = split(/\s*->\s*/, $abc, 2);
my $bcd = $abc =~ s/\s*->.*//sr;
my ($bcd) = $abc =~ /^(?:(?!\s*->).)*)/s;
my ($bcd) = $abc =~ /^(.*?)\s*->/s;
All but the last returns the entire input string if there's no ->.

Perl : How to store line by line of a file in an array and read only last 4 lines of the array [closed]

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
Perl : How to store line by line of a file in an array and read only last 4 lines of the array .
Now I want to compare with few values to the last 4 values of the array .
Here is a quick way to read the entire file into an array:
use feature qw( say );
my #text = do { local #ARGV = $file ; <> };
say "#text[-4 .. -1]"; # last 4 lines of the array
say "#text[($#text-3) .. $#text]"; # same
say $text[-1]; # last line of the file
say $text[-4]; # 4th line from the end of the file

How to print part of string which is of kind mentioned below [closed]

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.

Use of uninitialized value in sprintf [closed]

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.

Processing Time [closed]

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 8 years ago.
Improve this question
Why does the following script take so many cycles to complete?
The document it is sifting through is 20590 lines long and each line consists of the following, or a variation thereof;
"10160354001 ALGIERS ALGERIA 36.70 3.60 290"
I am trying to make a database to match the first number to the last 3.
The actual program is however taking several seconds per line.
Script:
#! /usr/bin/perl
$path = shift(#ARGV);
open COORDS, $path or die "Couldn't open file\n";
my %coords = {};
foreach $line (<COORDS>){
$stat = substr $line,0,11;
#coords = grep(($_!=undef),split(/ /,substr $line,42));
$coords[$stat]=#coords;
}
print $coords['10160354001'];
close(COORDS);
$coords['10160354001'] = ... is an assignment to an array element, and a large one at that. This statement will cause Perl to allocate an array with room for at least 10160354002 elements.
You meant to use a hash:
$coords{$stat} = "#coords";
...
print $coords{'10160354001'};
use strict and use warnings would have alerted you to this and other problems with your code.