Re: manipulating text file w/ associative array?
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 1 Mar 2006 11:52:25 -0800
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
.
- Follow-Ups:
- Re: manipulating text file w/ associative array?
- From: wellercs@xxxxxxxxx
- Re: manipulating text file w/ associative array?
- References:
- manipulating text file w/ associative array?
- From: wellercs@xxxxxxxxx
- manipulating text file w/ associative array?
- Prev by Date: manipulating text file w/ associative array?
- Next by Date: Re: send mail with authentication and secure connection
- Previous by thread: manipulating text file w/ associative array?
- Next by thread: Re: manipulating text file w/ associative array?
- Index(es):
Relevant Pages
|