perl - How to make an array containing strings in a file separated by space? -
i have file perl_script_2_out_2.txt
, want put strings separated space in array @arr
.
i wrote code isnt working .
open $file4, '<', 'perl_script_2_out_2.txt' or die $!; @array4 = <file4>; close($file4); open $file5, '>', 'perl_script_2_out_2.txt' or die $!; foreach $_ (@array4) { s/\s+/\n/g; print $file5 "$_"; } close($file5); open $file6, '<', 'perl_script_2_out_2.txt' or die $!; @arr = <$file6>;
you must always use strict
, use warnings
@ top of every perl program write. in case have seen message
name "main::file4" used once: possible typo
which points statement
my @array4 = <file4>
and helps see have opened file handle $file4
tried read file4
, different.
if fix code work, it's strange way things , it's better this. have used data::dump
display final contents of array; it's not necessary program work.
use strict; use warnings; open $fh, '<', 'perl_script_2_out_2.txt' or die $!; @arr; while (<$fh>) { push @arr, split; } use data::dump; dd \@arr;
output
[ "uart_1_baddress", 2211, "uart_2_baddress", 3344, "uart_3_baddress", 2572, ]
Comments
Post a Comment