in reply to Reading file into two arrays
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:/
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Reading file into two arrays
by AnomalousMonk (Archbishop) on May 25, 2015 at 10:35 UTC |