Print a substring of an array value [closed] - perl

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 8 years ago.
Improve this question
I have an array in the third element dataArr[2], I know it contains a 10 digit phone. I need to only read or print the first 6 digits. For instance if my phone is 8329001111, I need to print out the 832900. I tried to see if I can use substr but I keep reading or printing the full list. Do I need to dereference..

Try this :
$dataArr[2] =~ s/\s//g; # ensure there's no spaces
print substr($dataArr[2], 0, 6);
# ^ ^ ^
# variable | |
# offset start|
# |
# substring length

Related

How to extract only version number from a string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am extracting oracle version from windows using powershell command and i get result as 10.2.0.3.0Patch2, however i need to extract only numeric value i.e. 10.2.0.3.0 (only version number). Any way we can do it ?
Version info extracted is =
10.2.0.3.0 Production, 10.2.0.3.0Patch2 Production, 10.2.0.5.0 Production, 11.2.0.4.0 Production
You can use a regular expression to extract a substring. Example:
"10.2.0.3.0Patch2" | Select-String '((?:\d{1,3}\.){4}\d{1,3})' | ForEach-Object {
$_.Matches[0].Groups[1].Value
}
# Outputs the string '10.2.0.3.0'
You can read more about regular expressions by reading the about_Regular_Expressions help topic:
PS C:\> help about_Regular_Expressions

"isn't numeric" warning from perl [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 7 years ago.
Improve this question
Perl keeps telling me that '11' from my input file is not numeric. Last time I checked 11 was a number, so I'm a bit confused what the problem is here and hence where to start looking.
Interestingly, it's line 1 of my input file causing the problem, the numbers on lines 2 onwards are interpreted correctly.
Argument "11" isn't numeric in numeric eq (==) at ./extract.pl line 200, <INPUTFILE> line 1.
Line 200 is
if ($_data[0] == $_ch && !exists $_ch_sn{$_data[1]}) { $_ch_sn{$_data[1]} = undef; }
Any help much appreciated!
The error was caused by a BOM in my inputfile. Using setlocal nobomb in Vi solved the problem!
It could be that the source doesn't actually contain 11 (e.g. maybe it contains a character that resembles a one but isn't), but I suspect the warning message is incorrectly identifying the expression that's non-numeric[1].
That fact that nothing remotely resembling 11 appears on that line appears to support that[2].
So just pretend you obtained the following warning:
Argument isn't numeric in numeric eq (==) at ./extract.pl line 200, <INPUTFILE> line 1.
This means that $_data[0] or $_ch is a non-numeric string[2].
If both should be numbers, determine which one isn't, possibly using the following code:
use Data::Dumper; { local $Data::Dumper::Useqq = 1; warn(Dumper($_data[0], $_ch)); }
Once you determine which variable is wrong, debug to find out why it is that way.
If they could be legitimately be non-numeric, add appropriate checks to handle that situation.
For performance reasons, that part of the error message is deduced from the compiled code, a complicated and unreliable process.
I'm assuming the if has no elsif clause. Warnings from an elsif condition can appear to be from an if statement.

Why does this not give the 2nd last element of the array? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
print $some_array[$#some_array - 1];
My intuitive understanding is the above code should print the 2nd last element in #some_array.
However in reality it does not. It prints something I cannot made sense of.
It does print the second to last element in the array:
my #nums = qw(a b c d e);
print "$nums[$#nums-1]\n"; # prints 'd'
Here, $#nums gives the last index of an array (in this case 4 - arrays are zero-based), and hence $#nums - 1 gives array element 3.
A more common way of writing this would be:
$array[-2]
The behavior of $some_array[$#some_array - 1] depends on how many elements #some_array has.
If #some_array has 2 or more elements (scalar #array >= 2, or equivalently $#array >= 1), then your statement:
print $some_array[$#some_array - 1];
should work correctly. Say the array has 10 elements, then $#some_array == 9, and you'll print $some_array[8], the second-to-last element.
If #some_array has just one element, then $#some_array == 0, and you'll print the "-1th" element. Negative array indices are treated specially: -1 gives you the last element of the array, -2 the second-to-last, and so forth. And if your goal is to print the second-to-last element of the array, you should be using this feature.
If #some_array is empty (scalar #some_array == 0, $#some_array == -1), then your statement will try to print $some_array[-2], which doesn't exist, resulting in an error message.

Converting single line to multiple lines in perl [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I have file as below.
ID || DATE || AMOUNT
XX||20130801##20130901##20131001##20131101||100##200##300##400
and I want the output as below using perl.
xx||20130801||100
xx||20130901||200
xx||20131001||300
xx||20131101||400
Please help me how to convert using perl.
perl -F'\|\||##' -lanE'$.>1 or $" ="||",next; say "#F[0,$_,$_+4]" for 1..4' file
perl -F'\|\|' -lane '#a=split(/##/,$F[1]); #b=split(/##/,$F[2]); print "$F[0]||$a[$_]||$b[$_]" foreach 0..$#a;' file
Output:
ID || DATE || AMOUNT
XX||20130801||100
XX||20130901||200
XX||20131001||300
XX||20131101||400

Comparing floating point with another number [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
How to check if one floating point number is bigger than another number in Perl?
i.e: 100.4 > 90
I tried to work with use POSIX, rounding the 100.4 to 101.0 and converting both of them with int, but Perl still thinks that my 100.4 is smaller than my 90.
Edit: The bug was somewhere else. The privious code returned me sometimes true and sometimes false.
No, Perl doesn't 'think' that, and that's easy to check:
print 100.4 > 90 ? 'Greater' : 'Lesser'; # Greater
My (wild) guess is that you've tried to sort an array of floats, and got 100.4 standing before 90. For example, like this:
my #floats = (100.4, 50, 9);
print $_, "\n" for sort #floats;
# 100.4
# 50
# 9
The catch is that by default Perl uses string comparison in sort. So both 100.4 and 90 are cast to string first, and '100.4' is indeed lesser than '9', because '1' is lesser than '9' (strings are compared char-by-char).
The solution is simple: override the sorting routine when dealing with numbers.
print $_, "\n" for sort { $a <=> $b } #floats;
# 9
# 50
# 100.4