Ask a Question related to PERL Miscellaneous, Design and Development.
-
Faz #1
multiple arrays from one text file with one column
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
Faz Guest
-
getting multiple arrays
Chaps, on my first data entry page, I have the following forms: <SELECT NAME="DriverName"> <SELECT NAME="TrackName"> <input type="text"... -
Trouble with arrays from external text file
I'm trying to display an image from an array that is stored in an external text file. For example, I have an image directory that has 4 jpgs,... -
Saving multiple text fields to a txt file and then loading them back
Hi I am trying to create a movie where the user can edit the text within the movie and save what they have entered. I know i need to use the... -
single text file into multiple arrays
hello all I have a text file as below a b c d e a1 -
undeclaring multiple arrays
I've looked on deja a little but couldn't find a definite answer. I created an array of hashes to store a lot of data. I have everything declared... -
Bob Walton #2
Re: multiple arrays from one text file with one column
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.
--
Bob Walton
Bob Walton Guest
-
John Bokma #3
Re: multiple arrays from one text file with one column
Faz wrote:
So all people are called "Name", live at "Address" and their phone> 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
number is p h o n e and their age is age?
my $filename = "textfileinasinglecolumnwhichhasetc.txt";> 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 $state = 0;
my(@names, @address, @phone, @age);
open(FILE, $filename) or die "Can't open '$filename': $!";
while (my $line = <FILE>) {
chomp($line);
SWITCH: {
$state == 0 && do { push @names, $line; last SWITCH; };
$state == 1 && do { push @address, $line;last SWITCH; };
$state == 2 && do { push @phone, $line; last SWITCH; };
$state == 3 && do { push @age, $line; last SWITCH; };
};
$state++;
$state = 0 if $state == 4;
}
close(FILE) or die "Can't close '$filename': $!";
Not tested.
probably a better way to store data like this would be to use an array
of arrays. Or even better an array of hashes. But I don't know what you
are going to do with it :-).
Notice it doesn't check anything. Normally I would read a line, validate
it and store if it is what I expected and change the state.
I expect much nicer, probably more perlish ways to do it :-)
Note I focussed on readability ;-).
--
Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
virtual home: [url]http://johnbokma.com/[/url] ICQ: 218175426
John web site hints: [url]http://johnbokma.com/websitedesign/[/url]
John Bokma Guest
-
John W. Krahn #4
Re: multiple arrays from one text file with one column
Faz wrote:
Why four different arrays? A single array of arrays or array of hashes would make more sense.>
> 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
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
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
Anno Siegel #5
Re: multiple arrays from one text file with one column
Faz <lfazal@hotmail.com> wrote in comp.lang.perl.misc:
my ( @names, @address, @phone, @age);> 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
push @{ ( \ ( @names, @address, @phone, @age) )[ $. % 4]}, $_ while <DATA>;
Anno
Anno Siegel Guest
-
dominix #6
Re: multiple arrays from one text file with one column
"Faz" <lfazal@hotmail.com> wrote in message
news:13ddbd75.0309081829.1a6803cc@posting.google.c om...OK, I know it's a perl forum, but in that case shouldn't it be faster to use> 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
sed like
cat myfile |sed -e " /.*/{
N
N
N
s/\n/;/g
}"
which produce
Name;Address;phone;age
Name2;Address2;phone2;age2
....
HTH
--
dominix
dominix Guest
-
Chief Squawtendrawpet #7
Re: multiple arrays from one text file with one column
Anno Siegel wrote:
Don't you need to use this instead: [($. - 1) % 4]> my ( @names, @address, @phone, @age);
> push @{ ( \ ( @names, @address, @phone, @age) )[ $. % 4]}, $_ while <DATA>;
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.
Chief Squawtendrawpet Guest
-
Anno Siegel #8
Re: multiple arrays from one text file with one column
Chief Squawtendrawpet <cs@edu.edu> wrote in comp.lang.perl.misc:
Right. Or make it more obscure:> Anno Siegel wrote:> while <DATA>;> > my ( @names, @address, @phone, @age);
> > push @{ ( \ ( @names, @address, @phone, @age) )[ $. % 4]}, $_
>
> Don't you need to use this instead: [($. - 1) % 4]
>
> Otherwise, the items end up in the wrong arrays.
push @{ ( \ ( @age, @names, @address, @phone) )[ $. % 4]}, $_
while <DATA>;
Yes. It's an obscure feature of the backslash operator, but it has its> 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)
uses. See perldoc perlref.
Anno
Anno Siegel Guest



Reply With Quote

