Gossamer Forum
Home : General : Perl Programming :

About substr

Quote Reply
About substr
Hi! I'm wondering if the below codes could be shrunk to be more efficient.
Basically, I have a 9-digit number which is randomly generated and I want to store each number into $n0 to $n8. I then will need to further manipulate $n0 to $n8 individually later in the script.
I was wondering if the below codes could be optimised. Thank you all in advanced.
$n0 = substr($number,0,1);
$n1 = substr($number,1,1);
$n2 = substr($number,2,1);
$n3 = substr($number,3,1);
$n4 = substr($number,4,1);
$n5 = substr($number,5,1);
$n6 = substr($number,6,1);
$n7 = substr($number,7,1);
$n8 = substr($number,8,1);
Julian
Quote Reply
Re: [vampy] About substr In reply to
This code should help:
Code:
perl -e '$foo = "0123456789"; @bar = split("", $foo); use Data::Dumper; print Dumper(\@bar);'
$VAR1 = [
'0',
1,
2,
3,
4,
5,
6,
7,
8,
9
];

Adrian
Quote Reply
Re: [brewt] About substr In reply to
Umm.. Sorry but I'm new at this but how do I then call for each of the $var values. Meaning say i want o manipulate '0' how do i reference it?

Thanks.
Julian
Quote Reply
Re: [vampy] About substr In reply to
It's all stored in the array @bar. You can access the first digit by $bar[0], 2nd in $bar[1], and so on.

Adrian
Quote Reply
Re: [brewt] About substr In reply to
Thank you very much. I'll try it.
Julian