[pm-h] populate an array from data in a text file
Robert Boone
robo4288 at gmail.com
Sun Apr 20 20:09:08 PDT 2008
I think you maybe a little confused. here-documents are just a
convenient way to print or assign multiple lines of text.
You had it right with the diamond operator. Here is one of the many
ways to read a file into an array:
#!/usr/bin/perl
use strict;
use warnings;
my @phonetic;
while (my $word = <>) {
push @phonetic, $word;
}
print $phonetic[0];
print $phonetic[1];
print $phonetic[2];
print $phonetic[3];
print $phonetic[4];
print $phonetic[5];
assuming:
perl prog.pl words
On Sun, Apr 20, 2008 at 9:24 PM, Russell L. Harris <rlharris at oplink.net> wrote:
> I am trying to read data from a text file into an array, the goal
> being to copy various elements from the array. From chapter 2 (pages
> 66-67) of the third edition of "Programming Perl", it appears to me
> that the text file is termed a "here-document".
>
> Because of the need to read a file, I suspect that I should be using
> the diamond operator, but I haven't been able to figure out how it
> would fit into this script.
>
> Here is my Perl script:
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>
> #!/usr/bin/perl
>
> @ARGV = qw# words #; # read the here-document
>
> @phonetic = <<EOF =~ m/^\s*.+/gm;
> EOF
>
> print $phonetic[0];
> print $phonetic[1];
> print $phonetic[2];
> print $phonetic[3];
> print $phonetic[4];
> print $phonetic[5];
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>
>
> Here is the content of my here-document, which is a text file named
> "words":
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>
> able
> baker
> charlie
> delta
> echo
> foxtrot
> EOF
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> _______________________________________________
> Houston mailing list
> Houston at pm.org
> http://mail.pm.org/mailman/listinfo/houston
> Website: http://houston.pm.org/
>
More information about the Houston
mailing list