[23067] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5288 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jul 29 14:06:56 2003

Date: Tue, 29 Jul 2003 11:05:11 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Tue, 29 Jul 2003     Volume: 10 Number: 5288

Today's topics:
    Re: Calculate directorysize from a list? (Tad McClellan)
    Re: Calculate directorysize from a list? <g4rry_sh0rt@zw4llet.com>
        help with file dialog (Go Perl)
    Re: Help with parsing text file <kstairley@coppernospam.com>
    Re: How do you Call a Perl subroutine with a variable n <noreply@gunnar.cc>
    Re: How do you Call a Perl subroutine with a variable n <nobull@mail.com>
    Re: How do you Call a Perl subroutine with a variable n (Fred)
    Re: How do you Call a Perl subroutine with a variable n <uri@stemsystems.com>
    Re: How do you Call a Perl subroutine with a variable n <tassilo.parseval@rwth-aachen.de>
    Re: LDAP group membership query (Paul)
    Re: New to Perl - getting error <krahnj@acm.org>
    Re: New to Perl (Tad McClellan)
    Re: perl problem on windows (Fong Tsui)
    Re: remote readdir() (Tad McClellan)
    Re: remote readdir() <noreply@gunnar.cc>
    Re: remote readdir() (ARod)
    Re: Result from a regex substitute (Tad McClellan)
    Re: Result from a regex substitute <jds@trumpetweb.co.uk>
    Re: Result from a regex substitute <john.thetenant-s@moving-picture.com>
    Re: Sorting out sort (Tad McClellan)
    Re: Sorting out sort <john.thetenant-s@moving-picture.com>
    Re: Sorting out sort <joeminga@yahoo.com>
    Re: Sorting out sort (JR)
    Re: var within reg expression <john.thetenant-s@moving-picture.com>
    Re:  <bwalton@rochester.rr.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 29 Jul 2003 10:07:48 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Calculate directorysize from a list?
Message-Id: <slrnbid3e4.3tm.tadmc@magna.augustmail.com>

Garry Short <g4rry_sh0rt@zw4llet.com> wrote:

> while (<FILE>) {
>   shift;


What is the shift() meant to accomplish?

It looks superfluous (or even damaging) to me...


>   my ($size, $dir) = split;


That will fail for pathes that contain spaces.


>   my @f = split /\//, $dir;


That will fail on systems that use some other directory separator.


   use File::Basename;

will fail in neither case.



-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Tue, 29 Jul 2003 17:15:00 +0000
From: Garry Short <g4rry_sh0rt@zw4llet.com>
Subject: Re: Calculate directorysize from a list?
Message-Id: <bg66nv$3og$1$8300dec7@news.demon.co.uk>

Tad McClellan wrote:

> Garry Short <g4rry_sh0rt@zw4llet.com> wrote:
> 
>> while (<FILE>) {
>>   shift;
> What is the shift() meant to accomplish?
Umm ... good question ...

>>   my ($size, $dir) = split;
> That will fail for pathes that contain spaces.
I just picked the seperator out of the code he supplied

>>   my @f = split /\//, $dir;
> That will fail on systems that use some other directory separator.
Again, pulled out of the info the OP supplied ...

>    use File::Basename;
> will fail in neither case.
> 


Hmmmm .... that's the second time you've had to correct me today ... not
having a good day, am I? :-)

Cheers,

Garry




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

Date: 29 Jul 2003 09:29:00 -0700
From: puissant00@yahoo.com (Go Perl)
Subject: help with file dialog
Message-Id: <d3825316.0307290829.35a72bc3@posting.google.com>

I am using the following code to create file selection dialogs..But
apparently the Browse buttons do not seem to work.. i got this from
examples provided by typing widget. I changed a little to remove the
radio buttons and stuff, but i am not able to browse and select the
files. I will be glad if anyone can point the error and throw some
light on this.
Thanks very much.

use Tk;                                          
$mw = MainWindow->new(); 

    foreach my $i (qw(one two three four)) {
 my $f = $mw->Frame;
 my $lab = $f->Label(-text => "Select  $i file to Open: ",
       -anchor => 'e');
 my $ent = $f->Entry(-width => 20);
 my $but = $f->Button(-text => "Browse ...",
        -command => sub { fileDialog($mw, $ent, $i)});
 $lab->pack(-side => 'left');
 $ent->pack(-side => 'left',-expand => 'yes', -fill => 'x');
 $but->pack(-side => 'left');
 $f->pack(-fill => 'x', -padx => '1c', -pady => 3);
    }


sub filebox {
    my $demo = shift;

    
      (
       -name     => $demo,
       -text     => "Enter a file name in the entry box or click on
the \"Browse\" buttons to select a file name

using the file selection dialog.",
       -iconname => 'filebox',
      );
}

sub fileDialog {
    my $w = shift;
    my $ent = shift;
    my $operation = shift;
    my $types;
    my $file;
    @types =
      (["Text files", '*.txt'],
       ["All files", '*']
      );
    if ($operation eq 'open') {
 $file = $w->getOpenFile(-filetypes => \@types);
    }  
}


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

Date: Tue, 29 Jul 2003 13:36:49 -0400
From: "Kent" <kstairley@coppernospam.com>
Subject: Re: Help with parsing text file
Message-Id: <3f26b0b8_2@athena.netset.com>

Thanks for your help, I'm giving it a try, but running into snags.

Looking at the perldocs, thanks, I forgot about those.

1 thing I'd like to know is, can I pass $ARGV[1] instead of myfile.txt as
the file to open?

I'm also trying to match $line =~ $regex but can't seem to get something
like this to work:


my $line = undef;

if (defined($line = $fh->getline())) {

if ($line =~ $regex) { # or print if ($line =~ $regex);

print "$line \n";

}

}

Thanks again,

K

"albert vilella" <vilella@bio.ub.es> wrote in message
news:b5ecc0be.0307290347.7f713170@posting.google.com...
> "Kent" <kstairley@coppernospam.com> wrote in message
news:<3f26114c_3@athena.netset.com>...
> > All,
> >
> > I'm trying to parse a text file with logic that essentially goes like
this:
> > 1) open the file for reading with a handle
> > 2) match for a regex (regular expression)
> > 3) if found, get or print (to stdout or a file) the next few lines until
a
> > blank line is found or a different regex is found
> >
> > I can open the file for reading, no prob. - open (myhandle,
<text_file>);
> > # or something like that
> > I can match the regex, no prob.  -  while ($myhandle =~ $regex)     # or
> > something like this, sorry I don't have the code
> > in front of me and it's late. (But I can do this with the IO::Handle
module)
> > But, how do I get the next few lines after I've matched the first regex?
> > I've looked at the getline(s) function
> > in the IO::Handle module and I couldn't get something like this to work:
> > (from the wrox press Perl prog. book)
> > $line = $fh->getline();
> > while ($fh->getline) {
> >     print "$_ \n";
> > }
> >
> > Any help would be appreciated.
> > TIA, Kent
> > (kstairley@copper.net)
>
> Maybe using IO::File instead of IO::Handle,
>
> ------------------------------------
> use strict;
> use IO::File;
>
> my $fh = IO::File->new("<myfile.txt")
> or die "Couldnt open file for reading: $!\n";
>
> my $line = undef;
> if (defined($line = $fh->getline())) {
>   #do something
> }
>
> while (defined($line = $fh->getline())) {
>   #do something more
> }
>
> 1;
> ------------------------------------
>
> You can find more info in "perldoc IO::File" and "perldoc IO::Handle"
>
> "IO::Handle" is the base class for all other IO handle classes. It is
>        not intended that objects of "IO::Handle" would be created
directly,
>        but instead "IO::Handle" is inherited from by several other classes
in
>        the IO hierarchy.
>
> Hope it helps,
>
> avilella




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

Date: Tue, 29 Jul 2003 17:14:38 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: How do you Call a Perl subroutine with a variable name?
Message-Id: <bg634t$lbs66$1@ID-184292.news.uni-berlin.de>

Fred wrote:
> I have tried the two solutions recommended in the current 8
> messages, but I cannot get either to work.

My solution was tested:

   ($outvar1, $outvar2) = &{\&{$array[3]}}($invar1,$invar2,$invar3);

It should be noted that I'm not using a symbolic reference (in that 
case Perl would have complained, since I run it under "strict refs"), 
but I'm creating a real code reference 'on the fly', and dereference 
it immediately.

It can be more clearly written like this:

   my $coderef = \&{$array[3]};
   ($outvar1, $outvar2) = $coderef->($invar1,$invar2,$invar3);

If you have several subroutines that you want to call that way, you 
should really consider a dispatch table as Tad illustrated. But if you 
only have one, the above is a convenient solution IMO.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



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

Date: 29 Jul 2003 17:42:18 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: How do you Call a Perl subroutine with a variable name?
Message-Id: <u9el09i8t1.fsf@wcl-l.bham.ac.uk>

Gunnar Hjalmarsson <noreply@gunnar.cc> writes:

> Fred wrote:
> > I have tried the two solutions recommended in the current 8
> > messages, but I cannot get either to work.
> 
> My solution was tested:
> 
>    ($outvar1, $outvar2) = &{\&{$array[3]}}($invar1,$invar2,$invar3);
> 
> It should be noted that I'm not using a symbolic reference (in that
> case Perl would have complained, since I run it under "strict
> refs"),

Bzzt!

As is says in perldoc strict..

      ""strict refs""
             This generates a runtime error if you use symbolic
             references (see the perlref manpage).
               [snip]
             There is one exception to this rule:

                 $bar = \&{'foo'};
                 &$bar;

You _are_ using a symbolic reference but you are doing so in a way
that is explicitly exempted from "strict refs".  

The reason that symrefs should be avoid is not because they cause
strict refs to throw errors.  In fact the causality is the reverse.
The reason strict refs exists in the fist place is because symrefs
should generally be avoided.  It follows that just because you've
found a way to use a symref without strict refs complaining it doesn't
make using symrefs an any less bad idea.

perldoc goes on to say..

             is allowed so that "goto &$AUTOLOAD" would not break
             under stricture.

It doesn't mention that is exemption also allows exists(&{'foo'}) and
defined(&{'foo'}) (IIRC).

> It can be more clearly written like this:
> 
>    my $coderef = \&{$array[3]};
>    ($outvar1, $outvar2) = $coderef->($invar1,$invar2,$invar3);

No I think it's much cleaner to say:

    ($outvar1, $outvar2) = do{ no strict 'refs'; $array[3]->($invar1,$invar2,$invar3) };

When you are using symbolic refs you should just say so and not use
loopholes in 'strict refs' try to pretend that you are not.

Even Uri has fallen into this trap in the past:
  Message-ID: <u91ypd62ze.fsf@wcl-l.bham.ac.uk>
  http://groups.google.com/groups?selm=u91ypd62ze.fsf%40wcl-l.bham.ac.uk
        
> If you have several subroutines that you want to call that way, you
> should really consider a dispatch table as Tad illustrated. But if you
> only have one, the above is a convenient solution IMO.

But the OP _is_ using a dispatch table.  @array is a dispatch table.
It's just a dispatch table using symrefs.

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


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

Date: 29 Jul 2003 10:28:54 -0700
From: fred.illig@lmco.com (Fred)
Subject: Re: How do you Call a Perl subroutine with a variable name?
Message-Id: <8714513c.0307290928.47a3012a@posting.google.com>

Thanks for the specific examples that were very easy for me (a
relative new Perl programmer) to understand.  I got it working !!!!

Fred



tadmc@augustmail.com (Tad McClellan) wrote in message news:<slrnbictn1.3jp.tadmc@magna.augustmail.com>...
> Fred <fred.illig@lmco.com> wrote:
> 
> > I want to call this subroutine
> > (x) via an array element - @array = (a,b,c,x,y,z);
> > ($outvar1, $outvar2) = &$array[3]($invar1,$invar2,$invar3);
> 
> 
> Use a "dispatch table".
> 
> 
> > How do I make a call to a subroutine where the name of the subroutine
> > to be called is being derived from an element in an array?  
> 
> 
> By using "symbolic references".
> 
> But *don't do that*!
> 
> 
> > We have a
> > variable as the subroutine name that we want to use.
> 
> 
> Use real references instead of symbolic references.
> 
> 
> > A real live example
> > would be great.
> 
> 
> --------------------------------------------
> #!/usr/bin/perl
> use strict;
> use warnings;
> 
> my @subs = ( \&a, \&b, \&x );  # array of code references
> 
> foreach my $i ( 0..2 ) {
>    $subs[$i]->( 'one', 'two', 'three');
> }
> 
> sub a { print "a(@_) got called\n" }
> sub b { print "b(@_) got called\n" }
> sub x { print "x(@_) got called\n" }
> --------------------------------------------
> 
> 
> But using a hash instead of an array is often nicer:
> 
> --------------------------------------------
> #!/usr/bin/perl
> use strict;
> use warnings;
> 
> my %subs = ( a => \&a, b => \&b, x => \&x );  # hash of code references
> 
> foreach my $i ( 'a', 'b', 'x' ) {
>    $subs{$i}->( 'one', 'two', 'three');
> }
> 
> sub a { print "a(@_) got called\n" }
> sub b { print "b(@_) got called\n" }
> sub x { print "x(@_) got called\n" }
> --------------------------------------------


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

Date: Tue, 29 Jul 2003 17:44:29 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: How do you Call a Perl subroutine with a variable name?
Message-Id: <x7znixp6rm.fsf@mail.sysarch.com>

>>>>> "BM" == Brian McCauley <nobull@mail.com> writes:

  BM> Even Uri has fallen into this trap in the past:
  BM>   Message-ID: <u91ypd62ze.fsf@wcl-l.bham.ac.uk>
  BM>   http://groups.google.com/groups?selm=u91ypd62ze.fsf%40wcl-l.bham.ac.uk

heh!

symbolic lookups of methods are not the same as with plain subs. in fact
that is called late binding and is used by polymophism. you may load
different modules having the same method names and you need to know if a
method can be called (the can method in UNIVERSAL does this). so it is
not a trap but a different paradigm than what the OP wants (which is
symbolic sub lookup).
        
  >> If you have several subroutines that you want to call that way, you
  >> should really consider a dispatch table as Tad illustrated. But if you
  >> only have one, the above is a convenient solution IMO.

  BM> But the OP _is_ using a dispatch table.  @array is a dispatch table.
  BM> It's just a dispatch table using symrefs.

then amend that to a dispatch table using hard refs. the advantages
include the ability to use anon subs and less likelyhood of nasty things
happening. in general when people say dispatch tables here they mean
with code refs and not symbolic.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: 29 Jul 2003 17:47:31 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: How do you Call a Perl subroutine with a variable name?
Message-Id: <bg6bvj$8aa$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Brian McCauley:
> Gunnar Hjalmarsson <noreply@gunnar.cc> writes:

>> My solution was tested:
>> 
>>    ($outvar1, $outvar2) = &{\&{$array[3]}}($invar1,$invar2,$invar3);
>> 
>> It should be noted that I'm not using a symbolic reference (in that
>> case Perl would have complained, since I run it under "strict
>> refs"),
> 
> Bzzt!
> 
> As is says in perldoc strict..
> 
>       ""strict refs""
>              This generates a runtime error if you use symbolic
>              references (see the perlref manpage).
>                [snip]
>              There is one exception to this rule:
> 
>                  $bar = \&{'foo'};
>                  &$bar;
> 
> You _are_ using a symbolic reference but you are doing so in a way
> that is explicitly exempted from "strict refs".  
> 
> The reason that symrefs should be avoid is not because they cause
> strict refs to throw errors.  In fact the causality is the reverse.
> The reason strict refs exists in the fist place is because symrefs
> should generally be avoided.  It follows that just because you've
> found a way to use a symref without strict refs complaining it doesn't
> make using symrefs an any less bad idea.

However, these kind of symbolic references play in another league than
those that reference ordinary Perl variables IMO. For one, Perl
functions are by definition global. There is no lexically scoped
function (and subsequently the advice of using lexically scoped types
cannot be applied here). Furthermore, regardless of strictures enabled
or not, you get a fatal error when accessing a non-existing function via
symref. This is not the case for variables. So a lot of the maintenance
nightmare that might arise from symrefs isn't quite so severe with
symbolic function references.

Having said that, a dispatch table is nonetheless the better solution,
particularly for a beginner.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Date: 29 Jul 2003 11:03:36 -0700
From: ppopour@coastalnow.net (Paul)
Subject: Re: LDAP group membership query
Message-Id: <8ba68739.0307291003.55f26176@posting.google.com>

Anyone?

ppopour@coastalnow.net (Paul) wrote in message news:<8ba68739.0307250617.16ec7a1f@posting.google.com>...
> I am trying to query an NDS tree using the Win32::OLE and
> Win32::OLE::Enum modules.  I was able to traverse the tree and gather
> all the group names, however, I can't seem to get the members of the
> groups.  I've tried several methods and I must be missing something. 
> Below is a snippet of the last try.  Any help would be greatly
> appreciated.
> 
> my $obj = Win32::OLE->GetObject("LDAP://$srv/$oh")||die
> Win32::OLE->LastError();
> foreach my $object (in $obj)
>  {
>  my $objectclass = $object->Get(objectClass);
>  foreach my $objclass (in $objectclass)
>   {
>   if ($objclass eq "organizationalUnit")
>    {
>    &showgrp("$object->{Name},$oh");
>    }
>   if ($objclass eq "groupOfNames")
>    {
>    print "$object->{Name}\t$object->{Class}\t$object->{ADsPath}\n";
>    my $grpenum = Win32::OLE::Enum->new($object)|| die
> Win32::OLE->LastError();
>    foreach ($grpenum->All)
>     {
>     if(lc($_->{'class'}) eq "member")
>      {
>      print "$_->{Name}\n";
>      }
>     }
>    }
>   }
>  }


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

Date: Tue, 29 Jul 2003 16:32:31 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: New to Perl - getting error
Message-Id: <3F26A1DF.22C5EABF@acm.org>

Bob wrote:
> 
> "John W. Krahn" <krahnj@acm.org> wrote in message
> news:3F25F4BB.AC444B5A@acm.org...
> > Bob wrote:
> > >
> > > John W. Krahn" <krahnj@acm.org> wrote in message
> > > news:3F25A113.37BFFF08@acm.org...
> > > > nos wrote:
> > > > >
> > > > > I am looking for a way to look into a file and determine the next
> > > > > number in a sequence that is not present within a range of numbers.
> > > > > Example:
> > > > > File    10000
> > > > >         10001
> > > > >         10003
> > > > >         10004
> > > > >
> > > > > I want it to find 10002 as the missing number. Any suggestions.
> > > > perl -lne'print for $last + 1 .. $_ - 1; $last = $_' yourfile
> > >
> > > c:\tmp>  perl -lne'print for $last + 1 .. $_ - 1; $last = $_'  data
> > >
> > > where data is file with 4  lines..
> > > 10001
> > > 10003
> > > 10004
> > > 10005
> > >
> > > on w2k sp2 system - I get following error msg
> > > Can't find string terminator "'" anywhere before EOF at -e line 1.
> >
> > Sorry, I don't use Windows, but my understanding from reading the Perl
> > documentation is that you have to use " instead of ' when running on
> > Windows.
> >
> > perl -lne"print for $last + 1 .. $_ - 1; $last = $_"  data
> 
> thanks.  that works..  ( in a way)..  starts at 1

Yes, after thinking about it I thought it might.  This will work without starting at 1.

perl -lne"BEGIN{$last = <>} print for $last + 1 .. $_ - 1; $last = $_" data



John
-- 
use Perl;
program
fulfillment


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

Date: Tue, 29 Jul 2003 10:26:21 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: New to Perl
Message-Id: <slrnbid4gt.414.tadmc@magna.augustmail.com>

Garry Short <g4rry_sh0rt@zw4llet.com> wrote:

[ snip code with mistakes ]

> That's what happens when you post code without really thinking about
> it


No, that's what happens when you post code without testing it first. :-)

If you merely test your code before offering it to thousands of people
around the world, then such a problem can be avoided without the
added effort of thinking.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: 29 Jul 2003 10:01:36 -0700
From: f.tsui@f5.com (Fong Tsui)
Subject: Re: perl problem on windows
Message-Id: <e7ffa9a7.0307290901.3619360c@posting.google.com>

"dw" <me@verizon.invalid> wrote in message news:<opnVa.21285$FP5.19256@nwrddc02.gnilink.net>...
> "mgarrish" <mgarrish@rogers.com> wrote in message
> news:dNlVa.110419$zwL.58270@news04.bloor.is.net.cable.rogers.com...
> >
> > "dw" <me@verizon.invalid> wrote in message
> > news:L4kVa.19523$FP5.5834@nwrddc02.gnilink.net...
> > >
> > > have you tried:
> > >   system $com1, "-load", $file1
> > >
> >
> > Depends on what you're trying to run. That will work for a bat or exe file
> > but not another perl script (just as examples). My advice would be to
>  avoid
> > the whole issue if you can (i.e., use e:\progra~1\ unless there is a good
> > reason why progra~1 will not correspond to the Program Files directory).
> >
> 
> It seems to work for me.  Could it be a setup issue?  This is what I get:
> 
> D:\path with spaces>type main.pl
> #!d:\perl\bin\perl5.8.0.exe
> 
> print "in $0\n";
> $other_pgm = "d:\\path with spaces\\other script.pl";
> @args = qw(arg1 arg2 arg3);
> system $other_pgm, @args;
> print "done with $0\n";
> 
> exit 0;
> 
> 
> D:\path with spaces>type "other script.pl"
> #!d:\perl\bin\perl5.8.0.exe
> 
> print "in $0\n";
> $x = 0;
> printf "%3d : %s\n", $x++, $_ foreach ($0, @ARGV);
> print "done with $0\n";
> exit 0;
> 
> D:\path with spaces>main.pl
> in D:\path with spaces\main.pl
> in d:\path with spaces\other script.pl
>   0 : d:\path with spaces\other script.pl
>   1 : arg1
>   2 : arg2
>   3 : arg3
> done with d:\path with spaces\other script.pl
> done with D:\path with spaces\main.pl
> 
> D:\path with spaces>

Thanks for all inputs. 
I've tried what suggested. If there is space within an arg, it doesn't work.


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

Date: Tue, 29 Jul 2003 09:59:21 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: remote readdir()
Message-Id: <slrnbid2u9.3tm.tadmc@magna.augustmail.com>

ARod <Arthurkog@yahoo.com> wrote:

> Is there a way to use readdir to get the directory structure of a
> remote server?


That depends on what protocol this anonymous "server" can support.

What protocols can that server use?


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Tue, 29 Jul 2003 17:36:06 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: remote readdir()
Message-Id: <bg64d5$l85li$1@ID-184292.news.uni-berlin.de>

ARod wrote:
> Is there a way to use readdir to get the directory structure of a
> remote server?  If not, is there another function that can do this?

Are you possibly talking about the server of a web hosting account? In 
that case, you may want to use File::Find in a CGI script.

Otherwise you've got several answers by others. ;-)

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



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

Date: 29 Jul 2003 10:30:09 -0700
From: Arthurkog@yahoo.com (ARod)
Subject: Re: remote readdir()
Message-Id: <4a7949b4.0307290930.647896f4@posting.google.com>

Garry Short <g4rry_sh0rt@zw4llet.com> wrote in message news:<bg60k7$mo4$2$8300dec7@news.demon.co.uk>...
> Abigail wrote:
> 
> > ARod (Arthurkog@yahoo.com) wrote on MMMDCXIX September MCMXCIII in
> > <URL:news:4a7949b4.0307290506.66fc711f@posting.google.com>:
> > **  Is there a way to use readdir to get the directory structure of a
> > **  remote server?  If not, is there another function that can do this?
> > 
> > 
> > Yes. You first have to mount the directory you want to read though.
> > NFS and Samba are two protocols that are used often.
> > 
> > On Unix, you would do something like:
> > 
> >     $ mount -t nfs remote.server.tla:/path/to/dir /mnt
> > 
> > 
> > Of course, if you want to do this often, or transparantly, you could
> > use the automounter.
> > 
> > 
> > Abigail
> 
> And if you're using some brand of Windoze,
> 
> my $remote = '\\remote_server\remote_share\file_structure_within_share';
> readdir $remote or die "Can't READDIR $remote: $!\n";
> 
> 
> Or, if you use double quotes to define $remote so that you can include a
> variable, double-up the backslashes :
> 
> foreach $folder (@list) {
>   $remote = "\\\\server\\share\\$folder";
>   ...
> }
> 
> HTH,
> 
> Garry


Garry, I'm using Windows NT but your suggestion is not working. 
Here's a snippet of the code:

$remote = "\\\\server\\some_share";

opendir(DIRECTORY,$remote);
@dirs=readdir(DIRECTORY) or die "can't read $remote";
closedir(DIRECTORY);

The readdir fails...What am I doing wrong?


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

Date: Tue, 29 Jul 2003 10:01:41 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Result from a regex substitute
Message-Id: <slrnbid32l.3tm.tadmc@magna.augustmail.com>

Chris <no_thanks@bms.umist.ac.uk> wrote:

> my string1 = my string2 = "mark had a little dog";
    ^^           ^^


Perl variables require a funny character (sigil) before their names.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Tue, 29 Jul 2003 17:08:25 +0100
From: "Julia deSilva" <jds@trumpetweb.co.uk>
Subject: Re: Result from a regex substitute
Message-Id: <AYwVa.2073$8m2.370@news-binary.blueyonder.co.uk>

Thanks everyone,

> my $string1 = "mark had a little dog";

Sorry, I did get my nursery rhyme wrong, and for YOUR information, I live 2
miles from Kilmersdon, the village where Jack and Jill fell down the hill,
but that's another story.....





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

Date: Tue, 29 Jul 2003 17:07:52 +0100
From: John Strauss <john.thetenant-s@moving-picture.com>
Subject: Re: Result from a regex substitute
Message-Id: <20030729170752.325f1139.john.thetenant-s@moving-picture.com>

On Tue, 29 Jul 2003 12:33:56 +0100
"Julia deSilva" <jds@trumpetweb.co.uk> wrote:
>
> Hi there all,
> 
> I sure this is really obvious but what's the syntax for this please:
> 
> my $string1 = "mark had a little dog";
> $string1  =~ s/dog/lamb/g; # $string1 is "mary had a little lamb"
> 
> 
> But ................
> my $string1 = "mark had a little dog";
> my $string2 = $string1 =~ s/dog/lamb/g;
> This returns
> $string2 = 1 (presumably a boolean for substitute done)
> whereas I want it to return $string2 = "mary had a little lamb";
> 
> Thanks in advance
> 
> 
> 

(my $string2 = $string1) =~ s/dog/lamb/g;

unless you really want to change "mark" to "mary":
my %lut=qw/mark mary dog lamb/;
(my $string2 = $string1) =~ s/(mark|dog)/$lut{$1}/g;



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drop the .thetenant to get me via mail


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

Date: Tue, 29 Jul 2003 10:36:49 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Sorting out sort
Message-Id: <slrnbid54h.414.tadmc@magna.augustmail.com>

Peter Stokes <peterstokes@operamail.com> wrote:

> I can get the data out, but I can't get it to sort.
  ^^^^^^^^^^^^^^^^^^^^^^

Not really. Your problem is with getting the data, not with 
sorting the data...


> #select the field with the county names	
> 	@out = $out[2];


Stomps over all of the elements in @out, and puts a single element 
in their place.


> #clean out duplicates 
> 	@out = grep ( (($out{$_}++ < 1) || 0), @out); 


@out can never have more than a single element in it.

A single element cannot contain duplicates.

This code serves no purpose.


> 	if (@out == 1) {
> 		print sort @out, "\n";


sort()ing a 1-element list is a rather trivial thing to do, so why do it?


> I
> get the same result wherever I put the 'sort' command.


sort() operates on a list of values.

Your code does not construct a list of values. 

You do not have a problem sorting, you have a probleming building up
a list that you can then sort.


   # untested
   my @out;
   while ( <FILE> ) {
      push @out, (split /\|/)[2];   # a "list slice"
   }
   print sort @out;


or to eliminate duplicates:

   # untested
   my %out;
   while ( <FILE> ) {
      $out{ (split /\|/)[2] } = 1;   # a "list slice" used as a hash key
   }
   print sort keys @out;



-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Tue, 29 Jul 2003 16:51:28 +0100
From: John Strauss <john.thetenant-s@moving-picture.com>
Subject: Re: Sorting out sort
Message-Id: <20030729165128.79a27598.john.thetenant-s@moving-picture.com>

On 29 Jul 2003 07:55:58 -0700
peterstokes@operamail.com (Peter Stokes) wrote:
>
> I'm trying to extract a column from a flatfile database and print it
> alphabetically. I can get the data out, but I can't get it to sort.
> 
> The database is pipe-separated in a plain text file and looks like
> this:
> 
> Ref_no|Title|County|Another_field|And_another|And_another
> 1234|A Name|Devon|Data here|More data here|And more data
> 1234|A Name|Somerset|Data here|More data here|And more data
> 1234|A Name|Nottinghamshire|Data here|More data here|And more data
> 1234|A Name|Essex|Data here|More data here|And more data
> ... and so on
> 
> My routine is as follows:
> 
> #open the file 
> open (INFILE, 'database.txt') or die "could not open listing file - 
> $!";
> while (<INFILE>) {
> #split the array at the separator
> 	@out = split /\|/;
> #select the field with the county names	
> 	@out = $out[2];
> #clean out duplicates 
> 	@out = grep ( (($out{$_}++ < 1) || 0), @out); 
> #delete blank lines left by duplicates	
> 	if (@out == 1) {
> 		print sort @out, "\n";
> 	}
> 	else {
> 		print "";
> 	}
> }
> #close the file to tidy up
> close INFILE;
> #eof
> 
> When I print out the results, I get, typically:
> Devon
> Somerset
> Nottinghamshire
> Essex
> 
> ... which is the order they appear in the database, and they haven't
> sorted alphabetically. I've tried it from every angle I can think, but
> I must be missing one because I can't make this result alphabetical. I
> get the same result wherever I put the 'sort' command.
> 
> Thanks in anticipation

working in england, i've had a few beers at lunch, 
so take this with a grain of salt...
unless you sort in your open()'d command as in: 
open(INFILE, "sort -k2 database.txt |") or blah; #very unix-y

then you must first read the database.txt file in, 
and sort it based on the County name:
my @db=(); 
open(INFILE, "<database.txt") or die "cannot open file, '$!'";
while (<INFILE>) {
    chomp;  
    my @out = split(/\|/,$_);
    push @db, \@out;
}
# $e->[2] is the county name
foreach my $e (sort {$a->[2] cmp $b->[2]} @db) {
    print "$e->[2]: @$e\n";
}
use a hash instead of @db if you want to drop duplicate
counties.

regarding your example, i can see where @out is set, 
but i don't see how %out (from your grep) is populated.  
you've either not shown enough code, or you are not 
using the "use warnings" and "use strict" directives.

for the definitive perldoc answer to your question run:
perldoc -q "How do I sort a"



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drop the .thetenant to get me via mail


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

Date: Tue, 29 Jul 2003 11:58:32 -0400
From: "Domenico Discepola" <joeminga@yahoo.com>
Subject: Re: Sorting out sort
Message-Id: <aTwVa.48594$PD3.4512430@nnrp1.uunet.ca>


"Peter Stokes" <peterstokes@operamail.com> wrote in message
news:10876b94.0307290655.50e56394@posting.google.com...
> I'm trying to extract a column from a flatfile database and print it
> alphabetically. I can get the data out, but I can't get it to sort.
>

I've written similar scripts and the CPAN module Sort::Fields has helped a
lot - especially with multi column sorts.




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

Date: 29 Jul 2003 09:45:35 -0700
From: jrolandumuc@yahoo.com (JR)
Subject: Re: Sorting out sort
Message-Id: <b386d54b.0307290845.32be7e2e@posting.google.com>

peterstokes@operamail.com (Peter Stokes) wrote in message news:<10876b94.0307290655.50e56394@posting.google.com>...
> I'm trying to extract a column from a flatfile database and print it
> alphabetically. I can get the data out, but I can't get it to sort.
> 
> The database is pipe-separated in a plain text file and looks like
> this:
> 
> Ref_no|Title|County|Another_field|And_another|And_another
> 1234|A Name|Devon|Data here|More data here|And more data
> 1234|A Name|Somerset|Data here|More data here|And more data
> 1234|A Name|Nottinghamshire|Data here|More data here|And more data
> 1234|A Name|Essex|Data here|More data here|And more data
> ... and so on
> 
> My routine is as follows:
> 
> #open the file 
> open (INFILE, 'database.txt') or die "could not open listing file - 
> $!";
> while (<INFILE>) {
> #split the array at the separator
> 	@out = split /\|/;
> #select the field with the county names	
> 	@out = $out[2];
> #clean out duplicates 
> 	@out = grep ( (($out{$_}++ < 1) || 0), @out); 
> #delete blank lines left by duplicates	
> 	if (@out == 1) {
> 		print sort @out, "\n";
> 	}
> 	else {
> 		print "";
> 	}
> }
> #close the file to tidy up
> close INFILE;
> #eof
> 
> When I print out the results, I get, typically:
> Devon
> Somerset
> Nottinghamshire
> Essex
> 
> ... which is the order they appear in the database, and they haven't
> sorted alphabetically. I've tried it from every angle I can think, but
> I must be missing one because I can't make this result alphabetical. I
> get the same result wherever I put the 'sort' command.
> 
> Thanks in anticipation

### You're sorting as you get the data, rather than getting all 
### the data and then sorting.  The below changes should help. 

### [tested once]

#!/perl
use strict;
use warnings;
use diagnostics;

my (@out, %out);

## Get data
while (<DATA>) {
   my ($refNo, $title, $county, $otherFields) = split /\|/, $_, 4;
   push @out, $county;   
}

$out{$_} = 1 for @out;            # Remove duplicates
print "$_\n" for sort keys %out;  # Sort and print counties

__DATA__
Ref_no|Title|County|Another_field|And_another|And_another
1234|A Name|Devon|Data here|More data here|And more data
1234|A Name|Somerset|Data here|More data here|And more data
1234|A Name|Devon|Data here|More data here|And more data
1234|A Name|Nottinghamshire|Data here|More data here|And more data
1234|A Name|Essex|Data here|More data here|And more data


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

Date: Tue, 29 Jul 2003 17:09:21 +0100
From: John Strauss <john.thetenant-s@moving-picture.com>
Subject: Re: var within reg expression
Message-Id: <20030729170921.4c76faa6.john.thetenant-s@moving-picture.com>

On Tue, 29 Jul 2003 14:24:15 +0100
news@roaima.freeserve.co.uk wrote:
>
> Gunnar Hjalmarsson <noreply@gunnar.cc> wrote:
> > This is getting confusing. John made a typo, and Chris illustrated his
> > point using the original incorrect expression.
> 
> Sorry! You're quite correct
> 
> Chris
> -- 
> @s=split(//,"Je,\nhn ersloak rcet thuarP");$k=$l=@s;for(;$k;$k--){$i=($i+1)%$l
> until$s[$i];$c=$s[$i];print$c;undef$s[$i];$i=($i+(ord$c))%$l}


sorry for the confusion.  (un)happy fingers.



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drop the .thetenant to get me via mail


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

Date: Sat, 19 Jul 2003 01:59:56 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re: 
Message-Id: <3F18A600.3040306@rochester.rr.com>

Ron wrote:

> Tried this code get a server 500 error.
> 
> Anyone know what's wrong with it?
> 
> if $DayName eq "Select a Day" or $RouteName eq "Select A Route") {

(---^


>     dienice("Please use the back button on your browser to fill out the Day
> & Route fields.");
> }
 ...
> Ron

 ...
-- 
Bob Walton



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

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 5288
***************************************


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