array question

M

Math55

hi, this is a piece of code from a program.

----
my $size;
my $realPath;
my @filterArray;
open READFILTEREDLIST,'</tmp/FILTEREDLIST' or die "Datei kann nicht
geoeffnet werden: $!\n";

while(<READFILTEREDLIST>) {

@filterArray=<READFILTEREDLIST>;

}
close READFILTEREDLIST;

open (WRITEPATHS, ">/tmp/FILTEREDPATHS") or die "Datei konnte nicht
gefunden werden";
print "open \n";
foreach(@filterArray){

print "array: $_\n"; #NOTHING!!!
(my $datei, my $pfad)=fileparse($_);
($size, $realPath)=split('\s', $pfad);
print "path: $realPath\n";
print WRITEPATHS "$realPath\n";

}
close WRITEPATHS;

----

the file FILTEREDLIST contains something like:

132k /var/backups/dpkg.status.1.gz
132k /var/backups/dpkg.status.2.gz
124k /var/backups/dpkg.status.3.gz
8.0k /var/log/auth.log.1.gz
12k /var/log/auth.log.2.gz
4.0k /var/log/auth.log.3.gz
12k /var/log/daemon.log.1.gz
12k /var/log/daemon.log.2.gz
8.0k /var/log/daemon.log.3.gz
8.0k /var/log/debug.1.gz
12k /var/log/debug.2.gz
4.0k /var/log/debug.3.gz
8.0k /var/log/kern.log.1.gz
4.0k /var/log/kern.log.2.gz
8.0k /var/log/kern.log.3.gz
8.0k /var/log/messages.1.gz
8.0k /var/log/messages.2.gz
8.0k /var/log/messages.3.gz
4.0k /var/log/setuid.changes.1.gz

but when i want to read that into the array, something does wrong. the
array is always empty, do you see the mistake? the file is not
empty...

THANKS!!
 
B

Brian McCauley

Subject: array question

Please put the subject of your post in the Subject of your post. If
in doubt try this simple test. Imagine you could have been bothered
to have done a search before you posted. Next imagine you found a
thread with your subject line. Would you have been able to recognise
it as the same subject?

If your subject truely refects your level of understanding of your
problem (which appears to be a question about reading files) then you
have not thought about your problem anywhere near enough thought to
dream of asking anyone else for help.

Math55, your poor subject lines have been pointed out to you before by
a number of people. Do you realise how rude you are being by still
not making any effort?
my @filterArray;
while(<READFILTEREDLIST>) {

@filterArray=<READFILTEREDLIST>;

}

Where did you learn that construct?

The above is an obfucated way of writing...

scalar <READFILTEREDLIST>; # Discard first line
my @filterArray= said:
the file FILTEREDLIST contains something like:

132k /var/backups/dpkg.status.1.gz
132k /var/backups/dpkg.status.2.gz
124k /var/backups/dpkg.status.3.gz
8.0k /var/log/auth.log.1.gz
12k /var/log/auth.log.2.gz
4.0k /var/log/auth.log.3.gz
12k /var/log/daemon.log.1.gz
12k /var/log/daemon.log.2.gz
8.0k /var/log/daemon.log.3.gz
8.0k /var/log/debug.1.gz
12k /var/log/debug.2.gz
4.0k /var/log/debug.3.gz
8.0k /var/log/kern.log.1.gz
4.0k /var/log/kern.log.2.gz
8.0k /var/log/kern.log.3.gz
8.0k /var/log/messages.1.gz
8.0k /var/log/messages.2.gz
8.0k /var/log/messages.3.gz
4.0k /var/log/setuid.changes.1.gz

but when i want to read that into the array, something does wrong. the
array is always empty, do you see the mistake?

Are you sure the file actually contains more than one line (as
determined by the EOL sequence for your OS)?

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
A

Anno Siegel

Math55 said:
hi, this is a piece of code from a program.

----
my $size;
my $realPath;
my @filterArray;
open READFILTEREDLIST,'</tmp/FILTEREDLIST' or die "Datei kann nicht
geoeffnet werden: $!\n";

while(<READFILTEREDLIST>) {

This reads one line from the file into $_
@filterArray=<READFILTEREDLIST>;

This reads the rest of the lines into @filterArray.
}
close READFILTEREDLIST;

open (WRITEPATHS, ">/tmp/FILTEREDPATHS") or die "Datei konnte nicht
gefunden werden";
print "open \n";
foreach(@filterArray){

print "array: $_\n"; #NOTHING!!!
(my $datei, my $pfad)=fileparse($_);
^^^^^^^^^
The function "fileparse()" is nowhere defined. So the program exits
the first time through the loop.
($size, $realPath)=split('\s', $pfad);
print "path: $realPath\n";
print WRITEPATHS "$realPath\n";

The variable "$realPath" is never assigned to. "$pfad" isn't even
declared. If the program compiles for you, you are not using "strict",
which is bad.
}
close WRITEPATHS;

----

the file FILTEREDLIST contains something like:

132k /var/backups/dpkg.status.1.gz
132k /var/backups/dpkg.status.2.gz
124k /var/backups/dpkg.status.3.gz

[more data snipped]

Your diagnosis is wrong. The file *is* read into the array (save for
the first line, and after getting compile-time errors out of the way).
I don't know why you think it isn't.

To get the whole file, do away with the while-loop and just say

@filterArray=<READFILTEREDLIST>;

Anno
 
G

Graham Wood

This is balderdash. Ignore me.

Graham

Graham said:
Here's where I think your problem lies.

while(<READFILTEREDLIST>){ }
steps through each line of the file returning the contents in $_ each time

@filterArray=<READFILTEREDLIST>;
reads the rest of the file contents into @filterArray.
 
A

Alan J. Flavell

On Thu, Jun 26, by accident I became aware that Graham Wood
had caused the following to appear:

Parts/Attachments:
1 Shown 48 lines Text (charset: UTF-8)
2 OK 8 lines Text (charset: UTF-8), "Card for Graham Wood"
----------------------------------------

[ The following text is in the "UTF-8" character set. ]


and had then inscribed on the eternal scroll:
This is balderdash. Ignore me.

That's OK. Usenet postings with attachments normally get ignored by
my newsreader. I had temporarily lifted the normal filters for a
different reason, otherwise I'd never have seen those non-conformant
postings.

all the best
 
T

Tad McClellan

Math55 said:
while(<READFILTEREDLIST>) {

@filterArray=<READFILTEREDLIST>;


The while() reads the first line into the $_ variable, then
you never do anything with that first line...

($size, $realPath)=split('\s', $pfad);


A pattern match should *look like* a pattern match.

Space characters are not a scarce resource. Feel free to use as
many as you like to make your code easier to read and understand:


($size, $realPath) = split( /\s/, $pfad );
 
T

Tad McClellan

Alan J. Flavell said:
On Thu, Jun 26, by accident I became aware that Graham Wood
had caused the following to appear:

Usenet postings with attachments normally get ignored by
my newsreader.


By mine too.

Sometimes I think my scorefile is prescient. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top