Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Populating an array

by Adam (Vicar)
on Mar 28, 2001 at 02:12 UTC ( #67657=note: print w/ replies, xml ) Need Help??


in reply to Populating an array

What do you already know? Do you expect us to write the script for you? Why should we?

How to read from a file (The cmd-line way):
prompt> perl -we '@filecontents =<>' filename

How to read from a file (The script way):

#!/usr/bin/perl -w use strict; open FILEHANDLE, "< filename" or die "Failed to open 'filename', $!"; my @contents = <FILEHANDLE>; close FILEHANDLE;
How to read from a file (The OO way):
#!/usr/bin/perl -w use strict; use IO::File; my $file = IO::File->new( "< filename" ) or die "Failed to open 'filename', $!"; my @contents = <$file>; $file->close();
So you see, your question is not straight forward. The answer that I would give is:
#!/usr/bin/perl -w use strict; use IO::File; use constant FILENAME => filename; my $file = IO::File->new( "< ". FILENAME ) or die "Failed to open '@{[FILENAME ]}', $!"; my @elements; { local $/ = $/.$/; @elements = <$file>; } $file->close();
With the hopes that my way contains so many perlisms that if your question is for HW that your teacher will recognize the plagarism, and that if you are truly trying to learn, then you will have plenty to figure out while perusing the library.


Comment on Re: Populating an array
Select or Download Code
Re: Re: Populating an array
by sierrathedog04 (Hermit) on Mar 28, 2001 at 04:13 UTC
    When our student friend turns in his assignment he should be sure to add single quotes around filename on line number 4.

    He should also add comments explaining what is going on and add some code to print out the array values. A final result (containing the obfuscatory dereferenced reference) is thus:

    #!/usr/bin/perl -w use strict; use IO::File; use constant FILENAME => 'filename'; #Replace filename with name of in +put file. my $file = IO::File->new( "< ". FILENAME ) or die "Failed to open '@{[FILENAME ]}', $!"; # Dereferenced array + ref and OS error my @elements; { local $/ = $/.$/; #Replace default input delimeter (\n) with (\n\n +) @elements = <$file>; } $file->close(); #Now test to see that everything reads out okay print "Here are the elements of the array:\n"; for (my $i = 0; $i < @elements; $i++) { print "Element number $i of the array is:\n$elements[$i]\n"; }
    If our friend makes some effort to understand what he has submitted then he will have learned his lesson well. Maybe we will see him here with Tye and Merlyn someday.
      Good catch on the quotes around the filename. I intentionally left it all uncommented. But lets have some more fun with it:
      #!/usr/bin/perl -w use strict; use IO::File; use constant FILENAME => 'filename'; { package cntr; sub TIESCALAR{bless[pop],__PACKAGE__} sub FETCH{ $_[0][0]++ } } my $file = IO::File->new( "< ". FILENAME ) or die "Failed to open '@{[FILENAME]}', $!"; my @elements; { local ( $a, $/ ) = $/.$/; @elements = split $a => <$file>; } $file->close(); print "Here are the elements of the array:\n"; tie my $i, 'cntr', 1; print map {$i.': {'."$_}$/"} @elements;
      Yeah, that would go over well with a teacher or professor.

      Update: So I changed the code a little from the original post. I actually tested it, and didn't like the way the newlines hung around. This fixes that. View source for the old code.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://67657]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2014-10-30 17:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls