[23760] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 5964 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Dec 20 14:10:31 2003

Date: Sat, 20 Dec 2003 11:10:08 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sat, 20 Dec 2003     Volume: 10 Number: 5964

Today's topics:
        sort <martin@gmx.de>
    Re: sort <jurgenex@hotmail.com>
    Re: sort <martin@gmx.de>
    Re: sort <gnari@simnet.is>
    Re: sort <martin@gmx.de>
    Re: sort (Randal L. Schwartz)
    Re: sort <jurgenex@hotmail.com>
    Re: sort <jurgenex@hotmail.com>
    Re: sort <gnari@simnet.is>
    Re: sort <gnari@simnet.is>
    Re: sort <martin@gmx.de>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sat, 20 Dec 2003 17:44:18 +0100
From: "Martin" <martin@gmx.de>
Subject: sort
Message-Id: <bs1u94$4pv$04$1@news.t-online.com>

I   want to walk through a directory path and get the sorted filepaths of
my files.
I tried this:

  push (@Links, "$File::Find::name\n") ;
     my @SortLinks = sort (@Links);
    print @SortLinks;

but the filenames do not get sorted.  I can print them with print
@SortLinks, but ther is on sorting.
Filenames are all something like ../a
                                                 ../b
                                                etc....




------------------------------

Date: Sat, 20 Dec 2003 16:54:16 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: sort
Message-Id: <Y8%Eb.14838$jG4.3013@nwrddc02.gnilink.net>

Martin wrote:
> I   want to walk through a directory path and get the sorted
> filepaths of my files.
> I tried this:
>
>   push (@Links, "$File::Find::name\n") ;
>      my @SortLinks = sort (@Links);
>     print @SortLinks;
>
> but the filenames do not get sorted.

Well, this code snippet doesn't tell us much.
Do your really push _one_ name to @Links and then sort this list that
contains just _one_ name and print it?
Or are those three lines from different places in our program? You cut them
out of context, therefore it is pretty much impossible to tell what is
happening.

A wild guess: are the sort() and print() lines part of the wanted() function
for File::find()?

jue




------------------------------

Date: Sat, 20 Dec 2003 18:06:21 +0100
From: "Martin" <martin@gmx.de>
Subject: Re: sort
Message-Id: <bs1vif$lfs$05$1@news.t-online.com>

 To be clearer:

What i do is push a filepath to @Links.
This is working well.
So it boils down to a problem of type:

@Links = (../abc, ../def, ../ghi);
@SortLinks = sort (@Links);

print @Links is same as print @SortLinks, but not sorted.


"Jürgen Exner" <jurgenex@hotmail.com> schrieb im Newsbeitrag
news:Y8%Eb.14838$jG4.3013@nwrddc02.gnilink.net...
> Martin wrote:
> > I   want to walk through a directory path and get the sorted
> > filepaths of my files.
> > I tried this:
> >
> >   push (@Links, "$File::Find::name\n") ;
> >      my @SortLinks = sort (@Links);
> >     print @SortLinks;
> >
> > but the filenames do not get sorted.
>
> Well, this code snippet doesn't tell us much.
> Do your really push _one_ name to @Links and then sort this list that
> contains just _one_ name and print it?
> Or are those three lines from different places in our program? You cut
them
> out of context, therefore it is pretty much impossible to tell what is
> happening.
>
> A wild guess: are the sort() and print() lines part of the wanted()
function
> for File::find()?
>
> jue
>
>




------------------------------

Date: Sat, 20 Dec 2003 17:00:40 -0000
From: "Ragnar Hafstað" <gnari@simnet.is>
Subject: Re: sort
Message-Id: <bs1v7j$6dq$1@news.simnet.is>

"Martin" <martin@gmx.de> wrote in message
news:bs1u94$4pv$04$1@news.t-online.com...
> I   want to walk through a directory path and get the sorted filepaths of
> my files.
> I tried this:
>
>   push (@Links, "$File::Find::name\n") ;
>      my @SortLinks = sort (@Links);
>     print @SortLinks;
>
> but the filenames do not get sorted.  I can print them with print
> @SortLinks, but ther is on sorting.
> Filenames are all something like ../a
>                                                  ../b
>                                                 etc....

you do not describe your problem very vlearly.
when you have a problem like this, you should make a minimal program
that demonstrates it, and post that. It is much easier to look at a workin
example
than to deduce what you are doing from a snippet. as a side effect, the act
of simplifying
the issue can make the bug clear to yourself before having to ask for help
here.

that said, it looks like you are sorting once for each file found. is that
correct?

gnari





------------------------------

Date: Sat, 20 Dec 2003 18:33:25 +0100
From: "Martin" <martin@gmx.de>
Subject: Re: sort
Message-Id: <bs2158$86p$01$1@news.t-online.com>

since I am unclear, here is the whole script, what i get is an unsorted
List.

#!/usr/bin/perl -w

use strict;
use File::Find;

my $start_verzeichnis = "../";

unless (-d $start_verzeichnis) {
    die "Startverzeichnis '$start_verzeichnis' ist kein Verzeichnis.\n";
}

find(\&bearbeiten, $start_verzeichnis);

sub bearbeiten {
    # Diese Subroutine wird für jede rekursiv gefundene Datei einmal
aufgerufen.
    return unless /\.html$/ || /\.shtml$/ ;
    my @Links = "";
    push (@Links, "$File::Find::name\n") ;
    my @SortLinks = sort (@Links);
    print @SortLinks;
    }
exit 0;


"Martin" <martin@gmx.de> schrieb im Newsbeitrag
news:bs1u94$4pv$04$1@news.t-online.com...
> I   want to walk through a directory path and get the sorted filepaths of
> my files.
> I tried this:
>
>   push (@Links, "$File::Find::name\n") ;
>      my @SortLinks = sort (@Links);
>     print @SortLinks;
>
> but the filenames do not get sorted.  I can print them with print
> @SortLinks, but ther is on sorting.
> Filenames are all something like ../a
>                                                  ../b
>                                                 etc....
>
>




------------------------------

Date: Sat, 20 Dec 2003 17:42:22 GMT
From: merlyn@stonehenge.com (Randal L. Schwartz)
To: "Martin" <martin@gmx.de>
Subject: Re: sort
Message-Id: <2ce29678f26340f2e136c1e0c9bee085@news.teranews.com>

>>>>> "Martin" == Martin  <martin@gmx.de> writes:

Martin> since I am unclear, here is the whole script, what i get is an unsorted
Martin> List.

You can't do your sorting and printing inside your "wanted" subroutine,
since that's being called for each file one at a time.  You need to
gather the results and then sort at the end.

You might find my File::Finder module (now on CPAN) a bit easier for this:

    use File::Finder;
    my @SortLinks = sort
      File::Finder
        ->type('f')
        ->not->name(qr/\.s?html$/)
        ->in($start_verzeichnis);

    print "$_\n" for @SortLinks;

print "Just another Perl hacker,"

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


------------------------------

Date: Sat, 20 Dec 2003 17:51:58 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: sort
Message-Id: <2%%Eb.4611$GO2.3220@nwrddc01.gnilink.net>

[Please do not top post! Trying to re-arrange into chronological order]
[Please trim your quotes to a reasonable length]
Martin wrote:
> "Martin" <martin@gmx.de> schrieb im Newsbeitrag
> news:bs1u94$4pv$04$1@news.t-online.com...
>> I   want to walk through a directory path and get the sorted
>> filepaths of my files.
[...]
>> but the filenames do not get sorted.  I can print them with print
>> @SortLinks, but ther is on sorting.
[...]
[...]

As I suspected (see inline below)

> find(\&bearbeiten, $start_verzeichnis);
>
> sub bearbeiten {
>     # Diese Subroutine wird für jede rekursiv gefundene Datei einmal
> aufgerufen.
>     return unless /\.html$/ || /\.shtml$/ ;
>     my @Links = "";

Here you initialize @Links as the list, that contains an empty string (why?)

>     push (@Links, "$File::Find::name\n") ;

Then you push _one_ filename onto that list

>     my @SortLinks = sort (@Links);

You sort that one-element list which effectivly doesn't do anything.

>     print @SortLinks;

And then you print the content of that one-element list.

This process is repeated for every file found, resetting @Links to the list
containing the empty string for each iteration. Of course the net effect is
that you are printing all files in the order as they are being traversed by
File::Find.

Solution:
@Links must become global, i.e. move the declaration out of the bearbeiten()
function.
And then move the sort() and print() calls out of the bearbeiten function,
too, to the end of the program. Or do you really want to sort once for every
file found? Because that is what you are doing now.

jue




------------------------------

Date: Sat, 20 Dec 2003 17:54:38 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: sort
Message-Id: <y10Fb.4618$GO2.3556@nwrddc01.gnilink.net>

Martin wrote:
> To be clearer:
>
> What i do is push a filepath to @Links.
> This is working well.
> So it boils down to a problem of type:
>
> @Links = (../abc, ../def, ../ghi);
> @SortLinks = sort (@Links);
>
> print @Links is same as print @SortLinks, but not sorted.

Actually no.
As bacame apparent from your other post containing the whole program the
problem is that @Links never contains more than one element and if you sort
an array with only one element, then you get the same array. And it is
sorted already, therefore you don't see any difference.

jue




------------------------------

Date: Sat, 20 Dec 2003 17:19:36 -0000
From: "Ragnar Hafstað" <gnari@simnet.is>
Subject: Re: sort
Message-Id: <bs20b3$6ie$1@news.simnet.is>


"Martin" <martin@gmx.de> wrote in message
news:bs1vif$lfs$05$1@news.t-online.com...
> To be clearer:
>
> What i do is push a filepath to @Links.
> This is working well.
> So it boils down to a problem of type:
>
> @Links = (../abc, ../def, ../ghi);
> @SortLinks = sort (@Links);
>
> print @Links is same as print @SortLinks, but not sorted.
>

ok lets try this
@Links = ("../abc","../def","../ghi");
print @Links,"\n";
@SortLinks = sort (@Links);
print @SortLinks,"\n";

output:
 ../abc../def../ghi
 ../abc../def../ghi

hey! you are right , they are the same. but wait! it IS sorted.
</sarcasm>

please show us an actual simple example of what you are talking about.
if it really boils down to the problem above, it should be only a few lines
of code.

gnari





------------------------------

Date: Sat, 20 Dec 2003 17:42:48 -0000
From: "Ragnar Hafstað" <gnari@simnet.is>
Subject: Re: sort
Message-Id: <bs21mj$6kd$1@news.simnet.is>

"Martin" <martin@gmx.de> wrote in message
news:bs2158$86p$01$1@news.t-online.com...
> since I am unclear, here is the whole script, what i get is an unsorted
> List.
>
 ...
>
> find(\&bearbeiten, $start_verzeichnis);
>
> sub bearbeiten {
>     # Diese Subroutine wird für jede rekursiv gefundene Datei einmal
> aufgerufen.
>     return unless /\.html$/ || /\.shtml$/ ;
>     my @Links = "";
>     push (@Links, "$File::Find::name\n") ;
>     my @SortLinks = sort (@Links);
>     print @SortLinks;
>     }
> exit 0;
>
>

ok. the problem is that you do not understand how find() works.
you are creating a one element array for each file, and sorting that.
(bearbeiten is called once for each file).

change the above to (unntested):

our @Links=();    # notice the () and not "". cannot be my, so use our
find(\&bearbeiten, $start_verzeichnis);
my @SortLinks = sort (@Links);
print @SortLinks;

sub bearbeiten {
    # Diese Subroutine wird für jede rekursiv gefundene Datei einmal
aufgerufen.
    return unless /\.html$/ || /\.shtml$/ ;
     push (@Links, "$File::Find::name\n") ;
}
exit 0;

gnari





------------------------------

Date: Sat, 20 Dec 2003 19:15:57 +0100
From: "Martin" <martin@gmx.de>
Subject: Re: sort
Message-Id: <bs23kv$e8v$06$1@news.t-online.com>

Hello Jürgen,

great, it works.
I made a few errors, that you found out very well.
thank you very much for your help.

Martin

"Jürgen Exner" <jurgenex@hotmail.com> schrieb im Newsbeitrag
news:2%%Eb.4611$GO2.3220@nwrddc01.gnilink.net...
> [Please do not top post! Trying to re-arrange into chronological order]
> [Please trim your quotes to a reasonable length]
> Martin wrote:
> > "Martin" <martin@gmx.de> schrieb im Newsbeitrag
> > news:bs1u94$4pv$04$1@news.t-online.com...
> >> I   want to walk through a directory path and get the sorted
> >> filepaths of my files.
> [...]
> >> but the filenames do not get sorted.  I can print them with print
> >> @SortLinks, but ther is on sorting.
> [...]
> [...]
>
> As I suspected (see inline below)
>
> > find(\&bearbeiten, $start_verzeichnis);
> >
> > sub bearbeiten {
> >     # Diese Subroutine wird für jede rekursiv gefundene Datei einmal
> > aufgerufen.
> >     return unless /\.html$/ || /\.shtml$/ ;
> >     my @Links = "";
>
> Here you initialize @Links as the list, that contains an empty string
(why?)
>
> >     push (@Links, "$File::Find::name\n") ;
>
> Then you push _one_ filename onto that list
>
> >     my @SortLinks = sort (@Links);
>
> You sort that one-element list which effectivly doesn't do anything.
>
> >     print @SortLinks;
>
> And then you print the content of that one-element list.
>
> This process is repeated for every file found, resetting @Links to the
list
> containing the empty string for each iteration. Of course the net effect
is
> that you are printing all files in the order as they are being traversed
by
> File::Find.
>
> Solution:
> @Links must become global, i.e. move the declaration out of the
bearbeiten()
> function.
> And then move the sort() and print() calls out of the bearbeiten function,
> too, to the end of the program. Or do you really want to sort once for
every
> file found? Because that is what you are doing now.
>
> jue
>
>




------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 5964
***************************************


home help back first fref pref prev next nref lref last post