0

Ok, I'm new to Perl but I thins this question is for Perl Gurus only :)

I need a well explain example of how to store and keep control of data read from a file. I want to store them using an Associative array with index and then use a for loop to go over the array and print it to the screen. For example:

 my %array;

$array{$1} = [0]

foreach $array (sort values $array)
 print "$value";

Something like this.

3 Answers 3

2

First, Perl refers to associative arrays as "hashes". Here's a simple example of reading the lines of a file and storing them in a hash to print them in reverse order. We use the line number $. of the file as the hash key and simply assign the line itself ($_) as the hash value.

#!/usr/bin/env perl
use strict;
use warnings;
my %hash_of_lines;
while (<>) {
    chomp;
    $hash_of_lines{$.} = $_;
}
for my $lineno ( sort { $b <=> $a } keys %hash_of_lines ) {
    printf "%3d %s\n", $lineno, $hash_of_lines{$lineno};
}
Sign up to request clarification or add additional context in comments.

5 Comments

To be fair to him, Perl used to call hashes "associative arrays", but that was far too much typing, so we stopped when Perl 5 was released in 1994. These days, the term is a useful indicator of people who have learned Perl from horribly out of date resources :)
or who have learned to program in horribly out of date Perl-inspired languages.
Your code is virtually identical to using an array, except for arrays having a zero first index. So, I would mention this, instead of just suggesting a silly solution.
@TLP: I was attempting to answer the OP question: "I need a well explain example of how to store and keep control of data read from a file". Hence, I gave an example, however contrived.
@JRFerguson People ask for all sorts of silly things they do not need and should not use, so I think its a good idea to point this out to them. (And btw, the "silly" part was in reference to the question, not your answer. Sorry, I just realized that was ambiguous.)
1

Or even easier via the slurp method from IO::All:

@lines = io('file.txt')->slurp;

If you are reading a larger file you will probably want to lock the file to prevent racing conditions and IO::All makes it really easy to lock files while you are working with them.

Comments

1

You most likely do not want to use a hash (associative array is not a perl construct any longer) at all. What you are describing is using an array. Hashes are used for storing data connected with unique keys, arrays for serial data.

open my $fh, "<", $inputfile or die $!;
my @array = <$fh>;
print @array;  # will preserve the order of the lines from the file

Of course, if you want the data sorted, you can do that with print sort @array.

Now, if that had been done with a hash, you'd do something like:

my %hash = map { $_ => 1 } <$fh>;
print sort keys %hash; # will not preserve order

And as you can see, the end result is only that you do not preserve the original order of the file, but instead have to sort it, or get a semi-random order. All the while, you're running the risk of overwriting keys, if you have identical lines. This is good for de-duping data, but not for true representation of file content.

You might think "But hey, what if I use another kind of key and store the line as the value?" Well, sure, you might take JRFerguson's advice and use a numerical index. But then you are using an array, just forsaking the natural benefits of using a proper array. You do not actually gain anything by doing this, only lose things.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.