[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Deleting files and moving text to arrays



At 01:13 PM 1/31/97 -0000, Ryan Webb wrote:

>I am a relative begiiner to MacPerl, and ran into a couple of problems. I 
>searched the manual, but couldn't find any code for deleting a file. It 
>doesn't have to do anything special, just erase the file of the hard 
>drive. Also I have a text file that I need read into a array of some 
>sorts (though preferably associative). Any body have any ideas, this is 
>kind of an emergency. (Projects due in 2 hours...)
>

Try "unlink ($filename);" to delete a file.

One way to read a text file into an array is this snippet:

open(TEXTFILE, "NameOfFile");
open(OUT, "NewFile");
while (<TEXTFILE>){ # reads text in line by line

  # create array (@nameOfArray) and 
  # add the current line being read to the end of it ($_) 

  push(@nameOfArray,$_); 

  [...rest of code to process file line by line..]

}
close(TEXTFILE);
close(OUT);

You didn't say what you want to do with the associative array (now referred
to as hash, i think, in 5.0).
An associative array is referenced using "%" rather than "@", or
"$assocArrayName{$namedIndexEntry}" when subscripting yr way into it.

Here is one example of creating an associative array (hash), which contains
three elements:

# name of associative array
# named index into the array
# contents of the particular associative array entry

$figNum{$fileNumber}=$_;

This says: 

create an associative array named %figNum (when creating or referring to a
hash in this fashion, you use the $, not %. Use the % when you are not
indexing your way into the assoc. array.)

the way the contents of the entry will be indexed will be by a variable
(which will be different for each entry) holding the corresponding file
number for the $figNum .

the contents of the assoc. array itself is whatever line is being processed
at the moment ($_);

Hope this helps. I'm a newcomer to Perl as well, btw. I've found the
O'Reilly books (Learning Perl and Programming Perl) to be invaluable.

Good luck.

Ben