multiple arrays from one text file with one column

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. getting multiple arrays
      Chaps, on my first data entry page, I have the following forms: <SELECT NAME="DriverName"> <SELECT NAME="TrackName"> <input type="text"...
    2. 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,...
    3. 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...
    4. single text file into multiple arrays
      hello all I have a text file as below a b c d e a1
    5. 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...
  3. #2

    Default 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

  4. #3

    Default Re: multiple arrays from one text file with one column

    Faz wrote:
    > 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
    So all people are called "Name", live at "Address" and their phone
    number is p h o n e and their age is 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 $filename = "textfileinasinglecolumnwhichhasetc.txt";
    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

  5. #4

    Default 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
    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
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  6. #5

    Default Re: multiple arrays from one text file with one column

    Faz <lfazal@hotmail.com> wrote in comp.lang.perl.misc:
    > 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
    Anno Siegel Guest

  7. #6

    Default 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...
    > 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

    --
    dominix


    dominix Guest

  8. #7

    Default Re: multiple arrays from one text file with one column

    Anno Siegel wrote:
    > 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.
    Chief Squawtendrawpet Guest

  9. #8

    Default Re: multiple arrays from one text file with one column

    Chief Squawtendrawpet <cs@edu.edu> wrote in comp.lang.perl.misc:
    > Anno Siegel wrote:
    > > 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 <DATA>;
    > 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
    Anno Siegel Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139