1

I have little experience about perl, trying to read simple text file line by line and put all objects in to array. Could you please help ?

Text File:

AAA
BBB
CCC
DDD
EEE

Needs to have access for each object in array by index to get access for DDD element for example.

THX

3 Answers 3

3

Try this:

use strict;
use warnings;

my $file = "fileName";
open (my $FH, '<', $file) or die "Can't open '$file' for read: $!";
my @lines;
while (my $line = <$FH>) {
    push (@lines, $line);
}
close $FH or die "Cannot close $file: $!";

print @lines;

To access an array in Perl, use [] and $. The index of the first element of an array is 0. Therefore,

  $lines[3] # contains DDD

see more here : http://perl101.org/arrays.html

Sign up to request clarification or add additional context in comments.

5 Comments

Holy cow, I don't think I've seen anyone link to perl101.org before. I own it and I'm thinking about letting it lapse, but maybe I shouldn't. Do you often link to it?
Why go through all this trouble to slurp a file?
@AndyLester LOL :D i've used it a few times and it shows up pretty high on Google sometimes :D It's pretty cool, why not keep it ? Sinan that's the way I learned a couple of months ago (from somewhere online - don't remember where from) !
well, as @ikegami points out, my @lines = <FH> would do it. There is no good reason to individually push each line ... unless you are doing something to the lines as your read them. Even just my @files = <>; would be sufficient.
Thanks Sinan, didn't know that. I probably was doing something with the lines. The way I wrote this code it feels easier for me to understand, without previously knowing what you said.. Thanks for pointing that out
2
open(my $fh, '<', $qfn)
   or die("Can't open $qfn: $!\n");

my @a = <$fh>;
chomp @a;

As for the last paragraph, I don't know if you mean

$a[3]

or

my @matching_indexes = grep { $_ eq 'DDD' } 0..$#a;

Comments

0

Your "requirements" here seem extremely minimal and I agree with @ikegami it's hard to tell if you want to match against text in the array or print out an element by index. Perhaps if you read through perlintro you can add to your question and ask for more advanced help based on code you might try writing yourself.

Here is a command line that does what your question originally asked. If you run perldoc perlrun on your system it will show you the various command line switches you can use for perl one-liners.

  • -0400 - slurps input
  • -a - autosplits the file into an array called @F using a single white space as the split character.
  • -n - run in an implicit while (<>) { } loop that reads input from the file on
  • -E - execute the script between ' '

So with lines.txt equal to your text file above:

   `perl -0400 -a -n -E 'say $F[3];' lines.txt`

outputs: DDD

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.