There is a special variable $. that always contains the current line number of the file you are reading, starting with one. This can be used as the index for your two parallel arrays:
use strict; use warnings; use Data::Dumper; my @array; my @arraynew; while(<DATA>){ chomp; ( $array[$.-1], $arraynew[$.-1] ) = split ' ', $_, 2; } print Dumper \@array, \@arraynew; __DATA__ 1 http:/abcd efgh/ 2 http:/
Update 1: corrected split statement as per comments below.
Update 2: another version based on regexes instead of using split:
use strict; use warnings; use Data::Dumper; my @array; my @arraynew; ( $array[$.-1], $arraynew[$.-1] ) = /^(\S+)\s(.*)/ while <DATA>; print Dumper \@array, \@arraynew; __DATA__ 1 http:/abcd efgh/ 2 http:/
In reply to Re: Reading file into two arrays
by hdb
in thread Reading file into two arrays
by subr01
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |