Tuesday, 15 July 2014

perl - How to make an array containing strings in a file separated by space? -


I have a file perl_script_2_out_2.txt and I want to keep all the stars separated in space The array is @arr .

I have written this code but not working

  open my $ FILE4, '<', 'perl_script_2_out_2.txt' or $! ;; My @ array4 = & lt; FILE4 & gt ;; Close ($ FILE4); Open your $ FILE5, '& gt;', 'perl_script_2_out_2.txt' or $ !; Foreign currency $ _ (@ array4) {s / \ s + / \ n / g; Print $ FILE5 "$ _"; } Closed ($ FILE5); Open my $ FILE6, '& lt;', 'perl_script_2_out_2.txt' or $$! @arr = & lt; $ FILE6 & gt;  

You always use strict < / Code> and use the warning at the top of every pell program you write. In this case you must have seen the message

  The name "main :: FILE4" is used only once: the potential typo  

which indicates the statement

  my @ array4 = & lt; FILE4 & gt;  

And it helps you to see that you have opened the file handle $ FILE4 , but try to read from FILE4

If you fix this then your code will work, but this is a strange way of working and it is better in this way. I have used Data :: Dump to display the final contents of the array; This program is not necessary for working.

  Use strict; Use warnings; Open your $ FH, '& lt;', 'perl_script_2_out_2.txt' or $$! My @rr; While (& lt; $ fh & gt;) {push @arr, split; } Data: Use Dump; Dd \ @arr;  

Output

  ["uart_1_baddress", 2211, "uart_2_baddress", 3344, "uart_3_baddress", 2572, / Code> 

No comments:

Post a Comment