Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how to read file from sub-directories and do an average?

Reply
Thread Tools

how to read file from sub-directories and do an average?

 
 
Tad McClellan
Guest
Posts: n/a
 
      08-20-2005
Ross <> wrote:

> $curdir = $curdir . "/$ARGV[0]";


> opendir(SUBDIR, $curdir.$inputdirname) || die ("unable to open dir

^^^ no slash character?
> named $inputdirname $!");



Don't you need a directory separator character between $curdir
and $inputdirname?

Your diagnostic message is misleading, you should have the same name
there as used in the opendir():

opendir(SUBDIR, "$curdir/$inputdirname") or
die "unable to open dir named '$curdir/$inputdirname' $!";


> chdir ($curdir);



You should check the return value to ensure that you actually
got what you asked for:

chdir $curdir or die "could not change to '$curdir' $!";


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      08-20-2005
Tad McClellan wrote:
> Brian McCauley <> wrote:
>
>>Tad McClellan wrote:
>>>
>>>Are you being rude on purpose? It sure looks like it.

>>
>>Tad, I think such strong statements are a little over the top for a
>>first offence. (If this wasn't a first offence then they are justified).

>
> I wouldn't go to assuming "on purpose" on a first offence either.


OK, I'm satisfied that your criticism of the OP was indeed justified.
However we should avoid giving the new-commers the impression that the
Perl community is an unfreindly and unforgiving place.

Could I humbly suggest it would have been better to have said...

This has been explained to you before. Are you being rude on purpose?
It sure looks like it.

 
Reply With Quote
 
 
 
 
Ross
Guest
Posts: n/a
 
      08-21-2005
Thanks Brian and sorry Tad. Still i can't quite get what you are talking
about. Besides the typo, it seems there are some etiquettes of posting here,
where should i find them? I once encountered this situation before in
another newsgroup, is that every newsgroup having their rules so a newcomer
had better check them up first? if so, where are they?


 
Reply With Quote
 
Ross
Guest
Posts: n/a
 
      08-21-2005

"Tad McClellan" <> wrote in message
news:...
> Ross <> wrote:
>
>> $curdir = `pwd`;
>> chop $curdir;

>
>
> You should not use chop() to remove newlines.
>
> You should use chomp() to remove newlines.
>

Thanks for letting me know there is a better (in a sense that's what i want)
function.


 
Reply With Quote
 
Scott Bryce
Guest
Posts: n/a
 
      08-21-2005
Ross wrote:

> it seems there are some etiquettes of posting here, where should i
> find them?


I haven't been following this thread, so I don't know if this has been
explained to you.

There are general rules for posting to newsgroups. Some newsgroups are
more lax than others about the rules. In some newsgroups, particularly
the bussier technical newsgroups like this one, you will be expected to
play by the rules. It makes it easier for the people here to help you.

Also, many newsgroups, this one included, have posting guidelines that
you will be expected to follow. Tad posts them to this group about twice
a week. You can also find them here:

http://mail.augustmail.com/~tadmc/cl...uidelines.html

This newsgroup gets a lot of traffic. There are some very knowledgeable
people here who devote a lot of time to helping others. People who don't
follow the guidelines take up more of other people's time, and will
eventually get less and less help.

> I once encountered this situation before in another newsgroup, is
> that every newsgroup having their rules so a newcomer had better
> check them up first? if so, where are they?


Every newsgroup is different. It is a good idea to read a couple of
week's worth of posts before you post to a newsgroup for the first time.
That will help you get a feel for what is expected. As you are reading,
look for a link to posting guidelines or a FAQ.
 
Reply With Quote
 
Ross
Guest
Posts: n/a
 
      08-21-2005
Is there any built-in function/parameters in Perl not to take . and .. into
account when opening all the subdirectories?

When i run the code:

the error appears:

unable to open dir named /home/sunlab/AAA/Reb/rawdat/4601-4.SMP No such file
or directory at <the absolute path for this perl>/SMP2XLSAVG2.pl line 42

<the absolute path for this perl> is replaced by me.

indeed when ls -al rawdat

drwxr-xr-x 2 sunlab 4096 Aug 21 12:25 4601-4.SMP/


I've tried both the with and without slash at the end versions.


 
Reply With Quote
 
Ross
Guest
Posts: n/a
 
      08-21-2005
"Scott Bryce" <> wrote in message
news:T4WdnZ2dnZ3heWLInZ2dnaVgmt6dnZ2dRVn-...
> Ross wrote:
>
>> it seems there are some etiquettes of posting here, where should i
>> find them?

>
> I haven't been following this thread, so I don't know if this has been
> explained to you.
>
> There are general rules for posting to newsgroups. Some newsgroups are
> more lax than others about the rules. In some newsgroups, particularly
> the bussier technical newsgroups like this one, you will be expected to
> play by the rules. It makes it easier for the people here to help you.
>
> Also, many newsgroups, this one included, have posting guidelines that
> you will be expected to follow. Tad posts them to this group about twice
> a week. You can also find them here:
>
> http://mail.augustmail.com/~tadmc/cl...uidelines.html
>
> This newsgroup gets a lot of traffic. There are some very knowledgeable
> people here who devote a lot of time to helping others. People who don't
> follow the guidelines take up more of other people's time, and will
> eventually get less and less help.
>
>> I once encountered this situation before in another newsgroup, is
>> that every newsgroup having their rules so a newcomer had better
>> check them up first? if so, where are they?

>
> Every newsgroup is different. It is a good idea to read a couple of week's
> worth of posts before you post to a newsgroup for the first time. That
> will help you get a feel for what is expected. As you are reading, look
> for a link to posting guidelines or a FAQ.


oh, i traced past messages and find out a term "attribution", now i
understand what it means and thanks for directing me to the link


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      08-21-2005
Ross <> wrote:

> Is there any built-in function/parameters in Perl not to take . and .. into
> account when opening all the subdirectories?



No, but there _is_ a way to avoid processing them.

while ( my $item = readdir DIR ) {

next if $item eq '.' or $item eq '..';
# next if $item /^\./; # skip ALL items that start with dot

# process non-dot files here
}


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      08-23-2005
Brian McCauley <> wrote in comp.lang.perl.misc:
>
>
> Tad McClellan wrote:
>
> > Ross <> wrote:
> >> chdir ($ARGV[0]);

> >
> >
> > You should probably ensure that it is an existing directory
> > before you try to open it:
> >
> > die "'$inputdirname' is not a directory" unless -d $inputdirname;

>
> I disagree. It's better to just try the chdir() and die if it fails.
> There are numerous reasons why this is better to do with race conditions
> and permissions.


Apart from that, $inputdirname is coming directly out of a readdir().
The possibility that chdir($inputdirname) fails because $inputdirname
isn't a directory is remote.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      08-23-2005
Tad McClellan <> wrote in comp.lang.perl.misc:
> Ross <> wrote:
>
> > Is there any built-in function/parameters in Perl not to take . and .. into
> > account when opening all the subdirectories?

>
>
> No, but there _is_ a way to avoid processing them.
>
> while ( my $item = readdir DIR ) {
>
> next if $item eq '.' or $item eq '..';
> # next if $item /^\./; # skip ALL items that start with dot
>
> # process non-dot files here
> }


File::Spec can even do that portably (untested):

use File::Spec qw( no_upwards);
for my $item ( no_upwards readdir DIR ) {
# no "." and ".." here
}

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
File.read(fname) vs. File.read(fname,File.size(fname)) Alex Dowad Ruby 4 05-01-2010 08:20 AM
file.read() doesn't read the whole file Sreejith K Python 24 03-24-2009 12:20 PM
Database file and folder are not read only but error message says it is read only?? keithb ASP .Net 2 06-07-2006 03:40 PM
jython/python: read file and allow read to be terminated py Python 0 01-27-2006 08:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57