-1

I know this is a fairly simple question, but I cannot figure out how to store all of the values in my array the way I want to. Here is a small portion what the .txt file looks like:

0 A R N D
A 2 -2 0 0
R -2 6 0 -1
N 0 0 2 2
D 0 -1 2 4

Each value is delimited by either two spaces - if the next value is positive - or a space and a '-' - if the next value is negative

Here is the code:

use strict;
use warnings;

open my $infile, '<', 'PAM250.txt' or die $!;
my $line;
my @array;

while($line = <$infile>) 
{
    $line =~ /^$/ and die "Blank line detected at $.\n";
    $line =~ /^#/ and next; #skips the commented lines at the beginning
    @array = $line;
    print "@array"; #Prints the array after each line is read
};

print "\n\n@array"; #only prints the last line of the array ?

I understand that @array only holds the last line that was passed to it. Is there a way where I can get @array to hold all of the lines?

Stefan Becker
  • 5,201
  • 9
  • 17
  • 27
William
  • 39
  • 7
  • 2
    Possible duplicate of [The correct way to read a data file into an array](https://stackoverflow.com/questions/8963400/the-correct-way-to-read-a-data-file-into-an-array) – Stefan Becker Feb 23 '19 at 17:13

4 Answers4

1

You are looking for push.

push @array, $line;

You undoubtedly want to precede this with chomp to snip any newlines, first.

ikegami
  • 322,729
  • 15
  • 228
  • 466
JRFerguson
  • 6,926
  • 1
  • 28
  • 34
1

If file is small as compared to available memory of your machine then you can simply use below method to read content of file in to an array

open my $infile, '<', 'PAM250.txt' or die $!;
my @array = <$infile>;
close $infile;

If you are going to read a very large file then it is better to read it line by line as you are doing but use PUSH to add each line at end of array.

push(@array,$line);

I will suggest you also read about some more array manipulating functions in perl

topmop75
  • 69
  • 1
  • 2
  • 10
  • 2
    While it's great to provide solutions to the bigger problems, it would be nice if you also answered the question that was asked. – ikegami Feb 23 '19 at 17:47
0

You're unclear to what you want to achieve.

Is every line an element of your array?

Is every line an array in your array and your "words" are the elements of this array?

Anyhow.

Here is how you can achieve both:

use strict;
use warnings;
use Data::Dumper;

# Read all lines into your array, after removing the \n
my @array= map { chomp; $_ } <>;

# show it
print Dumper \@array;

# Make each line an array so that you have an array of arrays
$_= [ split ] foreach @array;

# show it
print Dumper \@array;
Skeeve
  • 3,233
  • 2
  • 12
  • 19
-1
try this...


sub room
{
   my $result = "";
   open(FILE, <$_[0]);
   while (<FILE>) { $return .= $_; }
   close(FILE);
   return $result;
}

so you have a basic functionality without great words. the suggest before contains the risk to fail on large files. fastest safe way is that. call it as you like...

my @array = &room('/etc/passwd');

print room('/etc/passwd');

you can shorten, rename as your convinience believes.

to the kidding ducks nearby: by this way the the push was replaced by simplictiy. a text-file contains linebreaks. the traditional push removes the linebreak and pushing up just the line. the construction of an array is a simple string with linebreaks. now contain the steplength...

TRSi
  • 1