array question

Posted: 06-26-2003, 12:37 PM
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!!
Reply With Quote

Responses to "array question"

Brian McCauley
Guest
Posts: n/a
 
Re: array question
Posted: 06-26-2003, 01:04 PM
magelord@t-online.de (Math55) writes:
> 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=<READFILTEREDLIST>; # Read remainder of file into array
> 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\\
Reply With Quote
Anno Siegel
Guest
Posts: n/a
 
Re: array question
Posted: 06-26-2003, 01:04 PM
Math55 <magelord@t-online.de> wrote in comp.lang.perl.misc:
> 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
Reply With Quote
Graham Wood
Guest
Posts: n/a
 
Re: array question
Posted: 06-26-2003, 01:11 PM
This is balderdash. Ignore me.

Graham

Graham Wood wrote:
> > while(<READFILTEREDLIST>) {
> >
> > @filterArray=<READFILTEREDLIST>;
> >
> > }
> > close READFILTEREDLIST;
>
> 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.
>
> If you do both of these at the same time you will end up with nothing in
> @filterArray because this is happening
>
> 1. While loop reads a line
> 2. @filterArray gets all the rest of the lines
> 3. While loop reads a line
> 4. @filterArray gets the rest of the lines
>
> This will repeat until the while loop reads the last line and then
> <READFILTEREDLIST> will return nothing to @filterArray then your script will
> proceed with the lines after the while loop.
>
> You either need to use a while loop or @array=<FILEHANDLE> not both at the same
> time.
>
> while(<READFILTEREDLIST>){
> push(@filterArray,$_);
> }
>
> OR
>
> @filterArray=<READFILTEREDLIST>;
>
> Hope this helps
>
> Graham
Reply With Quote
Alan J. Flavell
Guest
Posts: n/a
 
Re: array question
Posted: 06-26-2003, 01:24 PM

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
Reply With Quote
Tad McClellan
Guest
Posts: n/a
 
Re: array question
Posted: 06-26-2003, 02:30 PM
Math55 <magelord@t-online.de> wrote:
> 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 );


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Reply With Quote
Tad McClellan
Guest
Posts: n/a
 
Re: array question
Posted: 06-26-2003, 03:27 PM
Alan J. Flavell <flavell@mail.cern.ch> wrote:
> On Thu, Jun 26, by accident I became aware that Graham Wood
> had caused the following to appear:
>> This is balderdash. Ignore me.
>
> Usenet postings with attachments normally get ignored by
> my newsreader.

By mine too.

Sometimes I think my scorefile is prescient. :-)


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Reply With Quote
Dale Hersh
Guest
Posts: n/a
 
ARRAY QUESTION
Posted: 07-24-2003, 07:40 PM
I have a question regarding the count function. Here goes:

Lets pretend I have this array called myStuff. If I add two elements to
myStuff and call the count function, I will get a result of 2. My question
is how do I re-initialize the array after adding elements so when I call the
count function on the array, I get a result of 0.

Thanks,
Dale


Reply With Quote
Kevin Stone
Guest
Posts: n/a
 
Re: ARRAY QUESTION
Posted: 07-24-2003, 08:18 PM

"Dale Hersh" <dalehersh@hotmail.com> wrote in message
news:20030724193903.81534.qmail@pb1.pair.com...
> I have a question regarding the count function. Here goes:
>
> Lets pretend I have this array called myStuff. If I add two elements to
> myStuff and call the count function, I will get a result of 2. My question
> is how do I re-initialize the array after adding elements so when I call
the
> count function on the array, I get a result of 0.
>
> Thanks,
> Dale
I assume you want to KEEP the other values in the array? If not then you
can just unset($myStuff).

If you do want to keep the array intact then I suppose you could store the
count in a variable $counta, add more elements to the array (or not), take
the count again and store it in a new variable $countb, then subtract the
values $count = abs($counta - $countb);

abs() converts to the absolute value if the result is negative.

- Kevin


Reply With Quote
John Hoge
Guest
Posts: n/a
 
Array question
Posted: 07-24-2003, 11:19 PM
I'm looking to make an admin page for a web photo gallery, and I need
to make a drop down menu showing all files in a given directory that
do not have comments in a database for the given filename.

I'm curious about the most efficient way to do this. I don't want to
query the database separately for each file as this would be horribly
inefficient. I'm thinking about two arrays, one containing all
filenames in the photo directory, and one containing filenames from
the database that have descriptions.

Is there a simple way in ADO to do a set operation on arrays, that is
to produce array C that has all elements in array A that are not
contained in array B?

I can do this with while loops, but there must be a better way!

John
Reply With Quote
 
LinkBack Thread Tools Search this Thread Display Modes
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Action Script Array Question javawebgrrl Macromedia Flex General Discussion 3 03-07-2005 03:44 PM
Passing value to Array pooknikk Macromedia Flash Data Integration 1 03-04-2005 01:11 PM
XML to flash array Wounded Paw Macromedia Flash Data Integration 0 02-17-2005 06:12 PM
What is this objects name in the array?! equinox007 webforumsuser@macromedia.com Macromedia Flash Actionscript 1 01-13-2004 05:47 AM
Large Multidimensional Array Question Mark Schupp ASP 0 07-02-2003 03:56 PM