Storing numbers in an array from files

Discussion in 'Perl Misc' started by aleatory, Mar 1, 2004.

  1. aleatory

    aleatory Guest

    Hi perl gurus,

    I've been struggling for maniplating a line of
    numbers. I'd greatly appreciate if you could
    offer me some help.

    A file, for instance, contains a bunch of numbers:

    | 1218, 8, 29
    | 3211, 9, 15

    I would like to first read in a line of numbers
    and then would like to uniquely store each number
    in an array as:

    For 1st loop
    | print "$my_array[0]"; <--displays 1218
    | print "$my_array[1]"; <--displays 8
    | print "$my_array[2]"; <--displays 29

    For 2nd loop
    | print "$my_array[0]"; <--displays 3211
    | print "$my_array[1]"; <--displays 9
    | print "$my_array[2]"; <--displays 15

    However, I've been having difficulty uniquely
    storing each number in an array. The following
    is my code:

    | #!/usr/bin/perl
    | use strict;
    | use warnings;
    |
    | open IN, 'infile' || die$!;
    |
    | my @line;
    | my @my_array;
    |
    | while ( <IN> )
    | {
    | @line = $_; <--@line contains 1218, 8, 29
    | @my_array = @line How could I store each as:
    | } $my_array[0] = 1218
    | $my_array[1] = 8
    | $my_array[2] = 29

    Then after separating each, how could I put
    them together as a line of numbers separated
    by a comma?

    I'm quite desperate and any information is
    highly appreciated.

    Many thanks in advance,

    alea
     
    aleatory, Mar 1, 2004
    #1
    1. Advertising

  2. aleatory wrote:
    > A file, for instance, contains a bunch of numbers:
    >
    > | 1218, 8, 29
    > | 3211, 9, 15
    >
    > I would like to first read in a line of numbers
    > and then would like to uniquely store each number
    > in an array as:
    >
    > For 1st loop
    > | print "$my_array[0]"; <--displays 1218
    > | print "$my_array[1]"; <--displays 8
    > | print "$my_array[2]"; <--displays 29
    >
    > For 2nd loop
    > | print "$my_array[0]"; <--displays 3211
    > | print "$my_array[1]"; <--displays 9
    > | print "$my_array[2]"; <--displays 15
    >
    > However, I've been having difficulty uniquely
    > storing each number in an array. The following
    > is my code:
    >
    > | #!/usr/bin/perl
    > | use strict;
    > | use warnings;
    > |
    > | open IN, 'infile' || die$!;
    > |
    > | my @line;
    > | my @my_array;
    > |
    > | while ( <IN> )
    > | {
    > | @line = $_; <--@line contains 1218, 8, 29
    > | @my_array = @line How could I store each as:
    > | } $my_array[0] = 1218
    > | $my_array[1] = 8
    > | $my_array[2] = 29


    perldoc -f split;

    But if you just use @my_array, only the last line will be preserved.
    You'd better use an array of arrays.

    perldoc perllol
    perldoc perlreftut

    > Then after separating each, how could I put
    > them together as a line of numbers separated
    > by a comma?


    perldoc -f join

    > I'm quite desperate and any information is
    > highly appreciated.


    Happy reading. :)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Mar 1, 2004
    #2
    1. Advertising

  3. aleatory

    Joe Smith Guest

    aleatory wrote:

    > | @line = $_; <--@line contains 1218, 8, 29


    No, $line[0] contains "1218, 8, 29\n" and the rest of the array is empty.

    > | @my_array = @line How could I store each as:
    > | } $my_array[0] = 1218
    > | $my_array[1] = 8
    > | $my_array[2] = 29


    @my_array = split /[,\s]/, $_; # Split on comma and/or whitespace.

    -Joe
     
    Joe Smith, Mar 1, 2004
    #3
  4. aleatory

    aleatory Guest

    Hi Gunnar,

    Thanks for the tips! In particular your recommendation
    on the split function nicely has solved my problem.
    Even though I haven't tried the join function, I'm
    quite sure it will work fine just considering your
    perfect advice.

    Thanks again for helping me out!

    alea

    Gunnar Hjalmarsson <> wrote in message news:<c1vqhm$1n8oe0$-berlin.de>...
    > aleatory wrote:
    > > A file, for instance, contains a bunch of numbers:
    > >
    > > | 1218, 8, 29
    > > | 3211, 9, 15
    > >
    > > I would like to first read in a line of numbers
    > > and then would like to uniquely store each number
    > > in an array as:
    > >
    > > For 1st loop
    > > | print "$my_array[0]"; <--displays 1218
    > > | print "$my_array[1]"; <--displays 8
    > > | print "$my_array[2]"; <--displays 29
    > >
    > > For 2nd loop
    > > | print "$my_array[0]"; <--displays 3211
    > > | print "$my_array[1]"; <--displays 9
    > > | print "$my_array[2]"; <--displays 15
    > >
    > > However, I've been having difficulty uniquely
    > > storing each number in an array. The following
    > > is my code:
    > >
    > > | #!/usr/bin/perl
    > > | use strict;
    > > | use warnings;
    > > |
    > > | open IN, 'infile' || die$!;
    > > |
    > > | my @line;
    > > | my @my_array;
    > > |
    > > | while ( <IN> )
    > > | {
    > > | @line = $_; <--@line contains 1218, 8, 29
    > > | @my_array = @line How could I store each as:
    > > | } $my_array[0] = 1218
    > > | $my_array[1] = 8
    > > | $my_array[2] = 29

    >
    > perldoc -f split;
    >
    > But if you just use @my_array, only the last line will be preserved.
    > You'd better use an array of arrays.
    >
    > perldoc perllol
    > perldoc perlreftut
    >
    > > Then after separating each, how could I put
    > > them together as a line of numbers separated
    > > by a comma?

    >
    > perldoc -f join
    >
    > > I'm quite desperate and any information is
    > > highly appreciated.

    >
    > Happy reading. :)
     
    aleatory, Mar 2, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    5
    Views:
    658
    Scott Allen
    Dec 15, 2004
  2. toton
    Replies:
    11
    Views:
    742
    toton
    Oct 13, 2006
  3. Jonathan Wood
    Replies:
    1
    Views:
    531
    Jonathan Wood
    Jun 2, 2008
  4. perditi0n2002
    Replies:
    4
    Views:
    386
    Willem
    Jan 2, 2011
  5. GIMME
    Replies:
    5
    Views:
    201
    Thomas 'PointedEars' Lahn
    Jul 26, 2004
Loading...

Share This Page