Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Re: Re: Populating an array

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


in reply to Re: Re: Populating an array
in thread Populating an array

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.


Comment on Re: Re: Re: Populating an array
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2015-07-04 20:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls