multiple arrays from one text file with one column

F

Faz

Hello

I have a text file in a single column which has name address, phone
age of each people as below

Name
Address
phone
age
Name
Address
phone
age

I want to put all the names in the names array, all the address in
address array all the phone in the phone array etc

Please help

Thanks
 
B

Bob Walton

Faz wrote:

....

I have a text file in a single column which has name address, phone
age of each people as below

Name
Address
phone
age
Name
Address
phone
age

I want to put all the names in the names array, all the address in
address array all the phone in the phone array etc
....


An excellent starting point is with the book "Learning Perl". Read
that, and if you then have problems you can't get past, please post here
again.
 
J

John W. Krahn

Faz said:
I have a text file in a single column which has name address, phone
age of each people as below

Name
Address
phone
age
Name
Address
phone
age

I want to put all the names in the names array, all the address in
address array all the phone in the phone array etc

Why four different arrays? A single array of arrays or array of hashes would make more sense.

A simple example (untested):

open IN, 'text_file' or die "Cannot open text_file: $!";
my ( @names, @addresses, @phones, @ages );
until ( eof IN ) {
chomp( my $temp = <IN> );
push @names, $temp;
chomp( $temp = <IN> );
push @addresses, $temp;
chomp( $temp = <IN> );
push @phones, $temp;
chomp( $temp = <IN> );
push @ages, $temp;
}
close IN;

for my $index ( 0 .. $#names ) {
print "Name: $name[$index]\n";
print "Address: $addresses[$index]\n";
print "Phone: $phones[$index]\n";
print "Age: $ages[$index]\n\n";
}


Or with an array of arrays:

open IN, 'text_file' or die "Cannot open text_file: $!";
my @data;
until ( eof IN ) {
push @data, [ grep chomp, map scalar <IN>, 1 .. 4 ];
}
close IN;

for my $record ( @data ) {
print "Name: $record->[0]\n";
print "Address: $record->[1]\n";
print "Phone: $record->[2]\n";
print "Age: $record->[3]\n\n";
}


Or with an array of hashes:

open IN, 'text_file' or die "Cannot open text_file: $!";
my @data;
my @fields = qw/Name Address Phone Age/;
until ( eof IN ) {
@{ $data[ @data ] }{ @fields } = grep chomp, map scalar <IN>, 1 .. 4;
}
close IN;

for my $record ( @data ) {
for my $field ( @fields ) {
print "$field: $record->{$field}\n";
}
print "\n";
}



John
 
A

Anno Siegel

Faz said:
Hello

I have a text file in a single column which has name address, phone
age of each people as below

Name
Address
phone
age
Name
Address
phone
age

I want to put all the names in the names array, all the address in
address array all the phone in the phone array etc

my ( @names, @address, @phone, @age);
push @{ ( \ ( @names, @address, @phone, @age) )[ $. % 4]}, $_ while <DATA>;

Anno
 
D

dominix

Faz said:
Hello

I have a text file in a single column which has name address, phone
age of each people as below

Name
Address
phone
age
Name
Address
phone
age

I want to put all the names in the names array, all the address in
address array all the phone in the phone array etc

Please help

Thanks

OK, I know it's a perl forum, but in that case shouldn't it be faster to use
sed like

cat myfile |sed -e " /.*/{
N
N
N
s/\n/;/g
}"

which produce
Name;Address;phone;age
Name2;Address2;phone2;age2
....


HTH
 
C

Chief Squawtendrawpet

Anno said:
my ( @names, @address, @phone, @age);
push @{ ( \ ( @names, @address, @phone, @age) )[ $. % 4]}, $_ while <DATA>;

Don't you need to use this instead: [($. - 1) % 4]

Otherwise, the items end up in the wrong arrays.

Can anyone help me understand why/how this code works?
\ ( @names, @address, @phone, @age)

Does it produce the same thing as this?
(\@names, \@address, \@phone, \@age)

Chief S.
 
A

Anno Siegel

Chief Squawtendrawpet said:
Anno said:
my ( @names, @address, @phone, @age);
push @{ ( \ ( @names, @address, @phone, @age) )[ $. % 4]}, $_
while <DATA>;

Don't you need to use this instead: [($. - 1) % 4]

Otherwise, the items end up in the wrong arrays.

Right. Or make it more obscure:

push @{ ( \ ( @age, @names, @address, @phone) )[ $. % 4]}, $_
while said:
Can anyone help me understand why/how this code works?
\ ( @names, @address, @phone, @age)

Does it produce the same thing as this?
(\@names, \@address, \@phone, \@age)

Yes. It's an obscure feature of the backslash operator, but it has its
uses. See perldoc perlref.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top