Reputation: 1823
I have a requirement to send my variable to array. I have something like this:
var = "abc|xyz|123";
I want to have the above values in an array.
$arr[0]="abc";
$arr[1]="xyz";
$arr[2]="123";
I used the following way, but I am not getting the array size while using this way:
$var = "abc|xyz|123";
$var =~ tr/|/\n/; # transforming "|" to new line "\n"
@a = $var;
print $a[0];
The complete transformed output is sent to only variable instead of individual variables.
Upvotes: 3
Views: 12204
Reputation: 15
you can use the regex like this
$var=~s/(\w+|\d+)/$data[$gg++]=$1;''/eg;
now the array @data holds the scalar data in $var...
Upvotes: -2
Reputation: 505
Although I'm not quite sure what you intend to do, but it seems to me like you're trying to solve a problem on your own which has already a solution?!
This should do the trick: Using the Perl split() function?
my $data = 'Becky Alcorn,25,female,Melbourne';
my @values = split(',', $data);
Upvotes: 0
Reputation: 532
You want to use split
$var = 'abc|xyz|123';
@a = split '|', $var;
print $a[0];
Upvotes: 0