Sunday, 15 April 2012

Getting many values in an array in perl -


I am writing a prel program in which I have an input file that contains a pattern:

  field = (1,2,3,4)  

OR

  field = (1,10,3, A, 11,10 , 7, D, 9, 10, 11, A)  

The number of values ​​is not stable, but in the bunch of 4. May be 4,8,12 or 16 or more.

I want to save these field values ​​in different variables. For which I am setting the value

  if ($ filed1 = ~ m / ^ \ "SORT FIELDS" \ s * = \ s * "(" \ s * (. *?) [,] + (. *?) [,] + (. *?) [,] + (. *?) [,] + [,] * ")" / Sgim). $ Val1 = $ 1; $ Val2 = $ 2; $ Val3 = $ 3; $ Val4 = $ 4;  

But this will not serve my purpose because every time the values ​​are different (4 or 8 or 12 ..).

The solution is to save it to an array, but I do not know how to save these values ​​in the array. Please tell me that I can do this. Tell me what is the other way to get results.

You can use it,

  Use strict ; Use warnings; My $ str = 'FIELDS = (1,10,3, A, 11,10,7, D, 9, 10, 11, A)'; If ($ str = ~ / field \ s * = \ s * \ (([^)] *) \) / x} {my @fields = split /, /, $ 1; Print "$ _ \ n" for @fields; }  

Output

  1 10 3a11 10 7d 9 10 11a  

No comments:

Post a Comment