Re: manipulating text file w/ associative array?



wellercs@xxxxxxxxx wrote:
I have a text file I want to manipulate. Below is a sample:

<sample>
FOLDER U:\files\apps\ ---A--- 0 2 9,152,576 9,152,576
FILE 2/7/2006 22:36 Acrobat-Reader-3-01-Installer.exe
FILE 2/7/2006 22:36 Java-Plugin-112-win32.exe
TOTAL U:\files\apps\ ---A--- 0 2 9,152,576 9,152,576
FOLDER U:\files\aviation\ ---A--- 0 2 99,905 99,905
FILE 2/7/2006 22:36 fm_sched.pdf
FILE 2/7/2006 22:36 schedule.pdf
TOTAL U:\files\aviation\ ---A--- 0 2 99,905 99,905
FOLDER U:\files\bluebook\ ---A--- 0 5 17,980,078 17,980,078
FILE 2/14/2006 12:33 BlueBook.doc
FILE 2/14/2006 12:33 BlueBook.pdf
FILE 2/7/2006 22:36 FrontpageInstructions.doc
FILE 2/7/2006 22:36 FTPAccess.doc
FILE 2/7/2006 22:36 styles.pdf
</sample>

I want to store this into an associative array so that I can do some
manipulation on the file line and be able to tell which folder the file
is in. The "TOTAL" line can be ignored. I can do the manipulation on
the file line, but I'm having trouble with the associative array.

"having trouble" is not a sufficient problem statement. What trouble
are you having? Syntax errors? Run time errors? Inifinte loops?
Crashes? Incorrect output?


Below is part of my improper code:

<code sample>
$folderFlag = 0;
$fileFlag = 0;

foreach $line (@lines) {
chomp($line);
if ($line =~ /^folder/i) {
$folderFlag = 1;
$folder = $line;
}
if ($line =~ /^file/i) {
$fileFlag = 1;
$file = $line;
}
if ($folderFlag != 0 && $fileFlag != 0) {
$void = "";
$folders{$folder} = 1;

Having such similarly named variables as %folders and $folder seems to
be begging for trouble, IMO.

if ($$folder{$file}) {

Earlier, you set $folder to be a string, $line. Now you're using it as
a reference to a hash. Which is it?

Turn on strict and warnings to prevent errors like this.


I'm not going to try to parse the code any further until you let us
know what the actual problem that you're trying to solve is.... and you
turn on strict and warnings by typing:
use strict;
use warnings;

Paul Lalli

.



Relevant Pages

  • Re: Can not sync email folders
    ... trouble as they have released AS4,2 in Danish. ... Are you able to sync the Inbox folder which is the root of all e-mail? ... to my old phone Ipaq6515, I can only reach ONE folder: UNWANTED MAILS, ...
    (microsoft.public.pocketpc.activesync)
  • Re: Newbie question - what AK to get?
    ... I want to get a better quality AK, ... folder. ... I don't want a gun that's going to give me ... trouble or be a poor investment. ...
    (rec.guns)
  • Re: Dialog crashing on Windows 7 64-bit
    ... archives may just carry half of the story (or I just missed ... but as .84 is giving v. little trouble I'd rather not risk ... dialog in a specific folder or folder structure outside "Program Files" ... means that I don't have to keep switching. ...
    (news.software.readers)
  • Re: Problem in Word 2004: "Change All" produces "Not Applicable"
    ... I doubt this will help but it's worth trying: Drag your Microsoft ... drag the original folder back and replace the new folder that was ... MacOffice MVP ... > I have had no trouble with files that were created or saved in Word ...
    (microsoft.public.mac.office.word)
  • High-Level Access Control APIs from Visual Basic
    ... it's regarding reading and changing NTFS permissions on a folder, ... code should work with OU's with some manipulation. ...
    (microsoft.public.vb.winapi)