3

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 to 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.

Please help me on this.

Regards, Sriharsha Kalluru.

4

3 回答 3

8

Use split:

@a = split(/\|/, $var);
于 2012-04-15T19:18:41.867 回答
0

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);
于 2012-04-15T19:18:29.793 回答
0

You want to use split

$var = 'abc|xyz|123';
@a = split '|', $var;
print $a[0];
于 2012-04-15T19:18:41.930 回答