Storing numbers in an array from files

A

aleatory

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
 
G

Gunnar Hjalmarsson

aleatory said:
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. :)
 
J

Joe Smith

aleatory said:
| @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
 
A

aleatory

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 said:
aleatory said:
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. :)
 

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

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top