[19710] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1905 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Oct 10 14:10:40 2001

Date: Wed, 10 Oct 2001 11:10:15 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <1002737415-v10-i1905@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Wed, 10 Oct 2001     Volume: 10 Number: 1905

Today's topics:
        PATTERN Matching: Substitution problem <Jean-Philippe.AVELANGE@cgey.com>
    Re: PATTERN Matching: Substitution problem nobull@mail.com
    Re: please check my script (Anno Siegel)
    Re: please check my script (Damian James)
    Re: pointers and perl <bart.lateur@skynet.be>
    Re: Quicker way to loop through directories? (Anno Siegel)
    Re: Quicker way to loop through directories? <s.warhurst@rl.ac.uk>
    Re: Quicker way to loop through directories? (Mark Jason Dominus)
    Re: Skimming an array for non-undef <djberge@qwest.com>
    Re: sort of array containing strings doesn't work (Abigail)
    Re: Sort unique Very large file <tintin@snowy.calculus>
    Re: Sort unique Very large file (Mark Jason Dominus)
        sorting question <wusyk@americasm01.nt.com>
    Re: sorting question <rsherman@ce.gatech.edu>
    Re: sorting question blah@blah.blah.invalid
    Re: sorting question <jurgenex@hotmail.com>
    Re: sorting question (Abigail)
    Re: split into filename and extension (need help before <Graham.T.Wood@oracle.com>
    Re: split into filename and extension (need help before <jurgenex@hotmail.com>
        What is faster?? <anordae@pisem.net>
    Re: What is faster?? <JPFauvelle@Colt-Telecom.fr>
    Re: What is faster?? <joe+usenet@sunstarsys.com>
    Re: What is faster?? (Mark Jason Dominus)
    Re: What is faster?? news@roaima.demon.co.uk
    Re: What is faster?? (Mark Jason Dominus)
    Re: What is faster?? <bart.lateur@skynet.be>
    Re: who said this? <Doug.King@abh.siemens.com>
        Why dosen't this work? <cschadl@nospam.satan.org.uk>
    Re: wildcard in argument of script blah@blah.blah.invalid
    Re: wildcard in argument of script <bart.lateur@skynet.be>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 10 Oct 2001 18:28:42 +0200
From: "Jean-Philippe AVELANGE" <Jean-Philippe.AVELANGE@cgey.com>
Subject: PATTERN Matching: Substitution problem
Message-Id: <3bc47785$0$13001$626a54ce@news.free.fr>

i've been looking for a solution to the ???? below, that would (without
having to change $PATTERN variable) replace values according to that
PATTERN, by the new $v1 and $v2 in my $_ variable to give the final result
displayed at the end. Something like: "Replace the $1 and $2 by $v1 and
$v2".

thanks in advance,
Jean-Philippe.

$PATTERN = "ABCD\d+-\d+-EFGH(\d+)-(\d+)";
$_ = "ABCD1-2EFGH3-4";
( $v1, $v2 ) = /$PATTERN/;
$v1++;
$v2++;
????
print;


ABCD1-2EFGH4-5





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

Date: 10 Oct 2001 18:42:12 +0100
From: nobull@mail.com
Subject: Re: PATTERN Matching: Substitution problem
Message-Id: <u9zo6zjssr.fsf@wcl-l.bham.ac.uk>

"Jean-Philippe AVELANGE" <Jean-Philippe.AVELANGE@cgey.com> writes:

> i've been looking for a solution to the ???? below, that would (without
> having to change $PATTERN variable) replace values according to that
> PATTERN, by the new $v1 and $v2 in my $_ variable to give the final result
> displayed at the end. Something like: "Replace the $1 and $2 by $v1 and
> $v2".

> $PATTERN = "ABCD\d+-\d+-EFGH(\d+)-(\d+)";
> $_ = "ABCD1-2EFGH3-4";
> ( $v1, $v2 ) = /$PATTERN/;
> $v1++;
> $v2++;
> ????
> print;

This can easily be done on 5.6 but not before since you need @+ and
@-.  

Firstly, of course, you must adjust the code so that the pattern
actually matches the string!  

Secondly having variables called $v1 and $v2 is a fairly sure sign you
should have been using an array.  

Thirdly whenever you have a // where your code relies on the
assumption that it will always match it is a good habit to make this
assumption explicit by appending 'or die'.

Fourthly you should get into the habit of automatically using
my().

Fifthly it is conventional in Perl to use lowercase variable
names except in special cases.

my $pattern = 'ABCD\d+-\d+EFGH(\d+)-(\d+)';
$_ = "ABCD1-2EFGH3-4";
my @v = /$pattern/ or die;
$_++ for @v;
for my $i (reverse 1 .. @v) {
  substr($_, $-[$i], $+[$i] - $-[$i]) = $v[$i-1];
}
print;

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


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

Date: 10 Oct 2001 13:24:45 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: please check my script
Message-Id: <9q1i6t$2fr$2@mamenchi.zrz.TU-Berlin.DE>

According to Damian James <damian@qimr.edu.au>:
> On 10 Oct 2001 08:55:41 GMT, Anno Siegel said:
> >According to Kimo R. <kimor79@hotmail.com>:
> >> $match++ if /^$user\b/o while <USER>;
> >> 
> >> Is giving me syntax error near "/^$user\b/o while".
> >...
> >The above example doesn't work because statement modifiers don't
> >nest:  If the inner statement is already modified, it is no longer
> >simple.  Someone didn't test their code...
> >
> 
> Gack! Indeed, and I already spotted another obvious mistake. My bad,
> but then I was providing interspersed snippets rather than real code. 
> Not prepending 'something like' often enough, bleh.
> 
> To the OP:
> [untested]
> 
> 	while (<USER>) { $match++ if /^$user\b/o };
> 
> is the correct rewrite.
> 
> Have a look at perlre for info about the /o modifier.
> 
> [OP] I guess you already spotted the other obvious mistake :-).

No, I didn't, and I don't.

You can have the statement-modifying while too:

    $match += /^$user\b/o while <USER>;

Anno


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

Date: 10 Oct 2001 13:30:08 GMT
From: damian@qimr.edu.au (Damian James)
Subject: Re: please check my script
Message-Id: <slrn9s8j5c.she.damian@puma.qimr.edu.au>

On 10 Oct 2001 13:00:25 GMT, Damian James said:
>...
>To the OP:
>[untested]
>
>	while (<USER>) { $match++ if /^$user\b/o };
>
>is the correct rewrite.
>

s/the/a/;

or:
    $match += /^$user\b/o while <USER>;

or even:

	/^$user\b/o && $match++ while <USER>

One of these is probably what I originally meant.

Cheers,
Damian "I don't know where I left my brain today" James
-- 
@:=grep!(m!$/|#!..$|),split//,<DATA>;@;=0..$#:;while($:=@;){$;=rand
$:--,@;[$;,$:]=@;[$:,$;]while$:;push@|,shift@;if$;[0]==@|;select$,,
$,,$,,1/80;print qq x\bxx((@;+@|)*$|++),@:[@|,@;],!@;&&$/}  __END__
Just another Perl Hacker,### http://home.pacific.net.au/~djames.hub


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

Date: Wed, 10 Oct 2001 13:12:25 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: pointers and perl
Message-Id: <dmh8stos01qbkm99to3ftkgmhbv2ok3qek@4ax.com>

bal wrote:

>use DBI;
>
># Connect to the database
>my $dbh = DBI->connect('DBI:mysql:my_database', 'my_username', 'my_pas
>+sword')
>    or die "Couldn't open database: $DBI::errstr; stopped";
>
>etc....
>....etc....
>
>Because of the presence of -> does this mean pointers, linked lists
>are feasible in perl?

I think they are. But you don't need them. Use arrays instead.

In this piece of code, the "->" doesn't actually indicate a
dereferencing. Instead, "connect" is a method, belonging to the class
DBI, and which is more or less the "constructor" for DBI. The object is
then stored in $dbh.

That asside: yes, a scalar can not only contain values, but references
too. References are like pointers, in that they "point" to other items.
That can be scalars, arrays, or hashes; but objects in Perl are
references too. In this case, the reference itself points to the
instance data, but in addition, the reference is "blessed" into a class,
so that  it knows what class it belongs to, thus what class it has to go
to retrieve its methods.

Now a cute little example, to whet your appetite:

	@a = (1, 2, 3);
	@b = (10, 20, 30);
	@c = (100, 200, 300);
	@x = (\@a, \@b, \@c); # an array of references;

	use Data::Dumper;
	print Dumper \@x;	# see what it contains

	print $x[1][2], "\n";	# prints "30", item with index 2 of @b

	push @c, 400;

	print Dumper \@x;	# see again what it contains
	#look! the modification in @c are visible in @x, too!

-- 
	Bart.


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

Date: 10 Oct 2001 13:13:37 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Quicker way to loop through directories?
Message-Id: <9q1hi1$2fr$1@mamenchi.zrz.TU-Berlin.DE>

According to S Warhurst <s.warhurst@rl.ac.uk>:
> I currently have a routine that searches through each of several thousand
> subdirectories and totals up the size of a specific type of file. It goes
> like this:
> 
> if(opendir(DIR, "f:/list-logs"))
> {
>   while($dir = readdir(DIR))
>   {
>     if(-d "f:/list-logs/$dir" and $dir ne "." and $dir ne "..")
>     {
>       if(opendir(SUB, "$listlogs/$dir"))
>       {
>         while($file = readdir(SUB))
>         {
>           if($file =~ /\.log.+/i)
>           {
>             $total += -s "$listlogs/$dir/$file";
>           }
>         }
>         closedir(SUB);
>       }
>     }
>   }
>   closedir(DIR);
> }
> 
> Under f:/list-logs there are about 3800 subdirectories and inside each I
> only want to total the size of files with the file extension ".logxxxx",
> hence the "/\.log.+/" match.

Unlikely.  Perl doesn't have to do much in that loop, the speed will
be limited by the file system, i.e. how fast it can serve the opendir's
and readdir's.  It may help slightly to slurp the top level directory,
so you read that one in a single go, instead of repeatedly returning
to it[1].  It won't be a speed breakthrough, if it makes any difference
at all.

Anno

[1] I just hope Cardinal S. won't see this.


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

Date: Wed, 10 Oct 2001 15:10:27 +0100
From: "S Warhurst" <s.warhurst@rl.ac.uk>
Subject: Re: Quicker way to loop through directories?
Message-Id: <9q1ksn$13uu@newton.cc.rl.ac.uk>

"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
news:9q1hi1$2fr$1@mamenchi.zrz.TU-
> Unlikely.  Perl doesn't have to do much in that loop, the speed will
> be limited by the file system, i.e. how fast it can serve the opendir's
> and readdir's.  It may help slightly to slurp the top level directory,
> so you read that one in a single go, instead of repeatedly returning
> to it[1].  It won't be a speed breakthrough, if it makes any difference
> at all.

Yes, I thought that might be the case. I had tried "slurping" the top level
directory but it didn't help.

However, just had a brainwave & found another way of doing it which only
takes about 5 minutes:

$dir = `dir g:\\list-logs\\\*\.log\* \/s \/w \/-c`;
@size = $dir =~ /Listed\:\s+\d+\sfile\(s\)\s+(\d+)\sbytes/i;

I didn't mention I was on NT before, but this way I just do a DIR listing
(which thankfully the output of doesn't have any linebreaks in it) into $dir
and then just extract the total that NT gives at the end of the dir listing.
Sorted :)

Regards
Spencer




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

Date: Wed, 10 Oct 2001 17:03:39 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: Quicker way to loop through directories?
Message-Id: <3bc47f6a.5656$a8@news.op.net>

In article <9q1f65$ht6@newton.cc.rl.ac.uk>,
S Warhurst <s.warhurst@rl.ac.uk> wrote:
>        while($file = readdir(SUB))
>        {
>          if($file =~ /\.log.+/i)
>          {
>            $total += -s "$listlogs/$dir/$file";
>          }
>
>Now, this routine takes ~15 minutes on my test machine. Can anyone think of
>an imginative way of speeding it up?

Might I suggest:

        for (grep /\.log.+/i, readdir(SUB)) {
          $total += -s "$listlogs/$dir/$_";
        }

I suspect that will be faster.  (My test runs say it got about 37%
faster; yours of course will vary.)

Replacing the outer while() loop with a for() similarly may also get a
speedup, if the top-level directory is large.

I'd also suggest changing /\.log.+/ to /\.log./, which does the same
thing.

If you can get rid of the /i, you should do so.  This regex could be
highly optimized, but /i disables most of the optimizations.

Hope this helps.
-- 
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print


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

Date: Wed, 10 Oct 2001 09:11:28 -0500
From: "Mr. Sunblade" <djberge@qwest.com>
Subject: Re: Skimming an array for non-undef
Message-Id: <8CYw7.62$d51.56331@news.uswest.net>


"John W. Krahn" <krahnj@acm.org> wrote in message
news:3BC389B2.F82AEF78@acm.org...
> "Mr. Sunblade" wrote:
<snip>
> > If you prefer an OO solution:
> >
> > my $sao = Set::Array->new(qw(1 2 3 4 undef undef undef));
>                             ^^
>
> > my $index = $sao->index('undef') -1;
> > print "Index was: $index\n";  # prints 3
> >
> > or, as a one-liner:
> >
> > print "Index was: ", Set::Array->new(qw(1 2 3 4 undef undef
>                                        ^^
>
> > undef))->index('undef') -1, "\n";
>
> If you use qw() to create the list the undef will be the literal text
> string 'undef' and not the _value_ undef.

Oops.  Typing ahead of my brain.  That should be:

print "Index was: ", Set::Array->new(1,2,3,4,undef,undef)->index(undef)-1,
"\n";

Or, if you just want to get rid of the undefined elements altogether Derek:

my $sao = Set::Array->new(@yourArray)->compact();

Regards,

Mr. Sunblade




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

Date: 10 Oct 2001 17:40:16 GMT
From: abigail@foad.org (Abigail)
Subject: Re: sort of array containing strings doesn't work
Message-Id: <slrn9s91vd.soe.abigail@alexandra.xs4all.nl>

Ralph Jocham (rjocham72@netscape.net) wrote on MMCMLXI September MCMXCIII
in <URL:news:ee8febb5.0110091426.1b3fa8f5@posting.google.com>:
@@ Hello,
@@ I am doing some kind of dependency analyisis for java packages.
@@ As a result I get a list of all imports from all classes. Duplicates
@@ are already removed.
@@ Then I sort the array with : sort @uniq;
@@ But the result is not sorted. Is there a size limite. The array contains
@@ about 2000 lines??

The only size limit is your system. You can't sort a huge array if
you don't have the RAM for it. But if Perl runs out of memory, it'll
tell you.

You probably have made a mistake.



Abigail
-- 
perl -we 'print q{print q{print q{print q{print q{print q{print q{print q{print 
               qq{Just Another Perl Hacker\n}}}}}}}}}'    |\
perl -w | perl -w | perl -w | perl -w | perl -w | perl -w | perl -w | perl -w


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

Date: Wed, 10 Oct 2001 23:44:30 +1000
From: "Tintin" <tintin@snowy.calculus>
Subject: Re: Sort unique Very large file
Message-Id: <8iYw7.19$l93.205934@news.interact.net.au>


"jzh" <jzh@earthlink.net> wrote in message
news:07Ow7.3806$7B1.150521@newsread2.prod.itd.earthlink.net...
> I have a very large file over 3million records "<FH> in example below"
with
> nonunique records.
> I am looking to sort without putting the contents of the file into an
array
> or hash.
>
> I would like to do a very low level operation line by line not to put any
> strain on memory limits.
>

[snip Perl code]

If you are on a Unix system (or even using Cygwin on Windoze), the sort
command (particularly GNU sort) will be much, much faster than trying to do
it in Perl.  If you are talking about 3 million records, then I'm sure
efficiency will be significate.




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

Date: Wed, 10 Oct 2001 16:31:04 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: Sort unique Very large file
Message-Id: <3bc477c7.5573$1ed@news.op.net>

In article <8iYw7.19$l93.205934@news.interact.net.au>,
Tintin <tintin@snowy.calculus> wrote:
>If you are on a Unix system (or even using Cygwin on Windoze), the sort
>command (particularly GNU sort) will be much, much faster than trying to do
>it in Perl. 

I second this.  He should be using

        grep '^S' input | sort -u > output

This has the added advantage that if the sort data *does* overflow the
computer's memory, the program will work anyway, because 'sort' can do
an external merge sort.  The Perl program would have dropped dead.

-- 
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print


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

Date: Wed, 10 Oct 2001 10:32:55 -0400
From: "Usyk, Walter [SKY:1P67:EXCH]" <wusyk@americasm01.nt.com>
Subject: sorting question
Message-Id: <3BC45C17.D028BFEE@americasm01.nt.com>

I have a question about sorting. Lets suppose that I have an unsorted
list with the following entries:

test.2 test.6 test.1 test.9 test.22 test.66 sort.1 sort.10 test.11
test.7 test.8 test.10 test.100 zest.10

If I sort it with perl I will get the following:

sort.1 sort.10 test.1 test.10 test.100 test.11 test.2 test.22 test.6
test.66 test.7 test.8 test.9 zest.10

What I really want is this order :

sorted.1 sorted.10 test.1 test.2 test.6 test.7 test.8 test.9 test.10
test.22 test.66 test.100 zest.10

Can anyone help with and let me know how this can be done?

Thanks for your help

--
Walter Usyk - Tools Developer
Software Development Environment Tools (1P67)
email: wusyk@nortelnetworks.com
ESN: 398-4603
Tel: (613) 768-4603




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

Date: Wed, 10 Oct 2001 11:26:35 +0500
From: Robert Sherman <rsherman@ce.gatech.edu>
Subject: Re: sorting question
Message-Id: <3BC3EA1B.736E6437@ce.gatech.edu>

"Usyk, Walter [SKY:1P67:EXCH]" wrote:
> 
> I have a question about sorting. Lets suppose that I have an unsorted
> list with the following entries:
> 
> test.2 test.6 test.1 test.9 test.22 test.66 sort.1 sort.10 test.11
> test.7 test.8 test.10 test.100 zest.10
> 
> If I sort it with perl I will get the following:
> 
> sort.1 sort.10 test.1 test.10 test.100 test.11 test.2 test.22 test.6
> test.66 test.7 test.8 test.9 zest.10
> 
> What I really want is this order :
> 
> sorted.1 sorted.10 test.1 test.2 test.6 test.7 test.8 test.9 test.10
> test.22 test.66 test.100 zest.10
> 
> Can anyone help with and let me know how this can be done?
> 

Sort::Naturally will do it...available at your friendly neighborhood
CPAN.

you could also format the extensions with sprintf so they sort properly
(sort.001 sort.010 test.001 test.002...). this would have the added
advantage of them sorting properly everywhere...but then something else
may depend on those extensions, in which case you could employ the
aforementioned module.

hth,

-- 
robert sherman
css, cee
georgia institute of technology
atlanta, ga, usa


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

Date: Wed, 10 Oct 2001 15:41:02 -0000
From: blah@blah.blah.invalid
Subject: Re: sorting question
Message-Id: <ts8r0edqj59822@corp.supernews.com>

Usyk, Walter [SKY:1P67:EXCH] <wusyk@americasm01.nt.com> wrote:
> I have a question about sorting. Lets suppose that I have an unsorted
> list with the following entries:
> 
> test.2 test.6 test.1 test.9 test.22 test.66 sort.1 sort.10 test.11
> test.7 test.8 test.10 test.100 zest.10
> 
> If I sort it with perl I will get the following:
> 
> sort.1 sort.10 test.1 test.10 test.100 test.11 test.2 test.22 test.6
> test.66 test.7 test.8 test.9 zest.10
> 
> What I really want is this order :
> 
> sorted.1 sorted.10 test.1 test.2 test.6 test.7 test.8 test.9 test.10
> test.22 test.66 test.100 zest.10
> 
> Can anyone help with and let me know how this can be done?


  use strict;

  my @list = qw ( test.2 test.6 test.1 test.9 test.22 test.66 sort.1
                  sort.10 test.11 test.7 test.8 test.10 test.100 zest.10 );

  print join ' ', sort {
        my @a=split(/\./, $a);
        my @b=split(/\./, $b);

        $a[0] cmp $b[0] || $a[1] <=> $b[1] } @list;

See 'perldoc -f sort'.  This splits the elements to be compared on '.'
and compares the first parts stringwise and the second parts numerically
(if the first parts match)

HTH



-- 
Eric Wong
eric+pgp@taedium.com                        http://www.taedium.com/pgp.txt


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

Date: Wed, 10 Oct 2001 08:41:36 -0700
From: "Jurgen Exner" <jurgenex@hotmail.com>
Subject: Re: sorting question
Message-Id: <3bc46c32@news.microsoft.com>

"Usyk, Walter [SKY:1P67:EXCH]" <wusyk@americasm01.nt.com> wrote in message
news:3BC45C17.D028BFEE@americasm01.nt.com...
> I have a question about sorting. Lets suppose that I have an unsorted
> list with the following entries:
>
> test.2 test.6 test.1 test.9 test.22 test.66 sort.1 sort.10 test.11
> test.7 test.8 test.10 test.100 zest.10
>
> If I sort it with perl I will get the following:
>
> sort.1 sort.10 test.1 test.10 test.100 test.11 test.2 test.22 test.6
> test.66 test.7 test.8 test.9 zest.10
>
> What I really want is this order :
>
> sorted.1 sorted.10 test.1 test.2 test.6 test.7 test.8 test.9 test.10
> test.22 test.66 test.100 zest.10
>
> Can anyone help with and let me know how this can be done?

Rather simple: write your own comparison function.
Something along the line (warning, conceptual sketch only, untested):

    sub mycmp {
        my ($afirst, $arest) = split $a, ".";
        my ($bfirst, $brest) = split $b, ".";
    if ($afirst cmp $bfirst) {
        return $afirst cmp $bfirst;
    } else {
        return $arest <=> $brest;
    }
    }

jue




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

Date: 10 Oct 2001 17:37:42 GMT
From: abigail@foad.org (Abigail)
Subject: Re: sorting question
Message-Id: <slrn9s91qj.soe.abigail@alexandra.xs4all.nl>

Walter [SKY:1P67:EXCH] (wusyk@americasm01.nt.com) wrote on MMCMLXII
September MCMXCIII in <URL:news:3BC45C17.D028BFEE@americasm01.nt.com>:
\\ I have a question about sorting. Lets suppose that I have an unsorted
\\ list with the following entries:
\\ 
\\ test.2 test.6 test.1 test.9 test.22 test.66 sort.1 sort.10 test.11
\\ test.7 test.8 test.10 test.100 zest.10
\\ 
\\ If I sort it with perl I will get the following:
\\ 
\\ sort.1 sort.10 test.1 test.10 test.100 test.11 test.2 test.22 test.6
\\ test.66 test.7 test.8 test.9 zest.10
\\ 
\\ What I really want is this order :
\\ 
\\ sorted.1 sorted.10 test.1 test.2 test.6 test.7 test.8 test.9 test.10
\\ test.22 test.66 test.100 zest.10
\\ 
\\ Can anyone help with and let me know how this can be done?


Assuming you want the first two results to be 'sort.1' and 'sort.10',
I'd suggest the following:

#!/opt/perl/bin/perl -w

use strict;

my @unsorted = qw /test.2 test.6 test.1 test.9 test.22 test.66
                   sort.1 sort.10 test.11 test.7 test.8 test.10
                   test.100 zest.10/;


my @sorted = map {substr $_ => 8}
             sort
             map {sprintf "%5s%03d%s" => (split /\./), $_}
            @unsorted;

print "@sorted\n";


__END__


which is using a form of a Guttman Rosler Transform.


Abigail
-- 
perl -we '$_ = q ?4a75737420616e6f74686572205065726c204861636b65720as?;??;
          for (??;(??)x??;??)
              {??;s;(..)s?;qq ?print chr 0x$1 and \161 ss?;excess;??}'


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

Date: Wed, 10 Oct 2001 15:44:48 +0100
From: Graham Wood <Graham.T.Wood@oracle.com>
Subject: Re: split into filename and extension (need help before I go insane)
Message-Id: <3BC45EDF.488E7018@oracle.com>

This is a multi-part message in MIME format.
--------------34C7BF65D9B65535E5BE1003
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

JHWB wrote:

> Ok, I have spent hours trying to figure out this simple task:
>
> how to split a filename into the name and the extension when the filename
> is:
> 1. any length
> 2. ends in either .jpg eller .gif
> 3. can include any character in the name
>
> As I have to insert a date between the name and the extension I (think I)
> will be using two variables $name and $ext.
>
> Please, this is a ridiculous question, but still I have found no answers
> anywhere. (Might have to do with not getting any sleep the past 48 hours).
>
> JHWB

($name,$ext)= $filename =~  /(.+)\.{1}([^.]+)?$/;

This should match
any characters followed by a dot then any characters other than a dot. It will
put the first lot of any characters into $name and the non-dot characters
after the last dot into $ext.  The ? stops the pattern from matching more than
one dot before the end of the filename.

Hope this helps

Graham Wood

--------------34C7BF65D9B65535E5BE1003
Content-Type: text/x-vcard; charset=UTF-8;
 name="Graham.T.Wood.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Graham Wood
Content-Disposition: attachment;
 filename="Graham.T.Wood.vcf"

begin:vcard 
n:;Graham
x-mozilla-html:FALSE
adr:;;;;;;
version:2.1
email;internet:Graham.T.Wood@oracle.com
fn:Graham Wood
end:vcard

--------------34C7BF65D9B65535E5BE1003--



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

Date: Wed, 10 Oct 2001 08:42:43 -0700
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: split into filename and extension (need help before I go insane)
Message-Id: <3bc46c73@news.microsoft.com>

"JHWB" <johan@basberg.comNOSPAM> wrote in message
news:9q16q3$4or$1@maud.ifi.uio.no...
> how to split a filename into the name and the extension when the filename
> is:
> 1. any length
> 2. ends in either .jpg eller .gif
> 3. can include any character in the name

Have you looked at
    File::Basename

jue




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

Date: Wed, 10 Oct 2001 18:59:17 +0400
From: Balance Keeper <anordae@pisem.net>
Subject: What is faster??
Message-Id: <3BC46245.E2B3630A@pisem.net>

Suppose we have $_="haha:lala:rere";
What is faster??
($haha) = split /:/, $_;  # or
($haha) = split(/:/, $_, 1);

----
  Temel Nosce




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

Date: Wed, 10 Oct 2001 17:59:38 +0200
From: Jean-Philippe Fauvelle <JPFauvelle@Colt-Telecom.fr>
Subject: Re: What is faster??
Message-Id: <ttr8st0nsrvrvihmkph27477hoeg8osf5v@4ax.com>

Le Wed, 10 Oct 2001 18:59:17 +0400, Balance Keeper <anordae@pisem.net> écrit:

The second one (split with 3 args) is faster because it allows Perl to 
stop searching just after the first ':'.

Jean-Philippe Fauvelle


>Suppose we have $_="haha:lala:rere";
>What is faster??
>($haha) = split /:/, $_;  # or
>($haha) = split(/:/, $_, 1);
>
>----
>  Temel Nosce
>



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

Date: 10 Oct 2001 12:10:07 -0400
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: What is faster??
Message-Id: <m3k7y3fpcw.fsf@mumonkan.sunstarsys.com>

Balance Keeper <anordae@pisem.net> writes:

> Suppose we have $_="haha:lala:rere";
> What is faster??
> ($haha) = split /:/, $_;  # or
> ($haha) = split(/:/, $_, 1);

  % perldoc Benchmark

A better question would be

  "Which is correct?"

This might help:

  % perldoc -f split | grep --context=2 LIMIT | tail -16


-- 
Joe Schaefer                 "I hate a country witout a derrick."
                                               --Mark Twain



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

Date: Wed, 10 Oct 2001 16:18:42 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: What is faster??
Message-Id: <3bc474e0.5525$2ad@news.op.net>

In article <3BC46245.E2B3630A@pisem.net>,
Balance Keeper  <anordae@pisem.net> wrote:
>Suppose we have $_="haha:lala:rere";
>What is faster??
>($haha) = split /:/, $_;  # or
>($haha) = split(/:/, $_, 1);

The second one is faster, because it doesn't have to do any splitting.
But $haha = $_ would be even faster and would do the same thing.

-- 
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print


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

Date: 10 Oct 2001 17:20:31 GMT
From: news@roaima.demon.co.uk
Subject: Re: What is faster??
Message-Id: <3bc4754f@news.netserv.net>

Balance Keeper <anordae@pisem.net> wrote:
> Suppose we have $_="haha:lala:rere";
> What is faster??
> ($haha) = split /:/, $_;  # or
> ($haha) = split(/:/, $_, 1);

These two statements do different things. Which result do you want
to achieve?

Chris


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

Date: Wed, 10 Oct 2001 16:26:33 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: What is faster??
Message-Id: <3bc476b8.5552$233@news.op.net>

In article <ttr8st0nsrvrvihmkph27477hoeg8osf5v@4ax.com>,
Jean-Philippe Fauvelle  <JPFauvelle@Colt-Telecom.fr> wrote:
>Le Wed, 10 Oct 2001 18:59:17 +0400, Balance Keeper <anordae@pisem.net> écrit:
>
>>($haha) = split /:/, $_;  # or
>>($haha) = split(/:/, $_, 1);
>
>The second one (split with 3 args) is faster because it allows Perl to 
>stop searching just after the first ':'.

This is deeply wrong in many ways.

First, you missed the important fact that the two pieces of code are
not doing the same thing.  The first one splits.  The second does not.
The second one is doing the same thing as $haha = $_.  It is not
splitting at all.

The original author should have asked about:
>>($haha) = split /:/, $_;  # or
>>($haha) = split(/:/, $_, 2);

But even in this case you would still be wrong.  When split() appears
on the right-hand side of a list assignment, Perl supplies the third
argument automatically.  The code that is generated for these two
statements is *completely identical*.  Even if you leave out the 2,
Perl puts it in for you.

This whole thread shows in sharp relief the dumbassery of these 'which
is faster' questions.  The original poster is wasting his time trying
to find out which is faster when he doesn't even know that the
alternative will work correctly.  Apparently he lives on the planet
where it's important for the program to do the wrong thing as quickly
as possible.  It's tempting to point out that

        ($haha) = exit(/:/);

will be fastest of all.


-- 
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print


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

Date: Wed, 10 Oct 2001 16:58:38 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: What is faster??
Message-Id: <jfv8stkvir2fkpa1e5mn9ogcbl5pjfs8g3@4ax.com>

Jean-Philippe Fauvelle wrote:

>The second one (split with 3 args) is faster because it allows Perl to 
>stop searching just after the first ':'.

Wrong.

If the LHS contains a fixed amount of scalars, then perl will stop after
one more. So these are equivalent:

	($haha) = split /:/, $_;

	($haha) = split(/:/, $_, 2);

p.s.

	($haha) = split(/:/, $_, 1);

won't do what you want.

-- 
	Bart.


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

Date: Wed, 10 Oct 2001 13:20:34 -0400
From: Doug King <Doug.King@abh.siemens.com>
Subject: Re: who said this?
Message-Id: <3BC48362.F5772C53@abh.siemens.com>

Dann Corbit wrote:
> 
> With a PC, I always felt limited by the software available.
> On Unix, I am limited only by my knowledge.
> 
> --Peter J. Schoenster
> --
> C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
>  "The C-FAQ Book" ISBN 0-201-84519-9
> C.A.P. FAQ: ftp://cap.connx.com/pub/Chess%20Analysis%20Project%20FAQ.htm

Practically everyone who has ever seriously tried to use both.   :-)


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

Date: Wed, 10 Oct 2001 12:38:49 -0500
From: "Chris Schadl" <cschadl@nospam.satan.org.uk>
Subject: Why dosen't this work?
Message-Id: <pan.2001.10.10.12.38.47.316.5178@nospam.satan.org.uk>

If I use the following code to recursivly traverse a directory:

#!/usr/bin/perl

use strict;

my $path = $ARGV[0];

print_dir($path);

exit;

sub print_dir($) {
    my $path = shift;

    for (<$path/*>) {
        if (-d $_) {
            print_dir($_);
        } else {
            print "$_\n";
        }
    }
}

 ...it works fine unless one of the directories happens to have a space in
it.  If that is the case, then I get output like this:

$ ./traverse.pl /mnt/ryoko/export/mp3
<snip>
/mnt/ryoko/export/mp3/Classical/Bach
-
Unaccompanied
Sonata
No.
3
in
C
</snip>

For some reason, spaces are getting transformed into newline chars.
What's the deal?  Is this a bug in the version of perl (5.6.0) I'm using?


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

Date: Wed, 10 Oct 2001 15:46:15 -0000
From: blah@blah.blah.invalid
Subject: Re: wildcard in argument of script
Message-Id: <ts8ra7b2ge4adb@corp.supernews.com>

Damian James <damian@qimr.edu.au> wrote:
> On Wed, 10 Oct 2001 08:54:26 GMT, Hessu said:
>>I have script which is doing fine but
>>now I'm finishing it and I'd like it
>>handle files by wildcards.
>>...
>> - perl myscript *.*
>> - perl myscript *.txt *.dat
>>...
> 
> The expansion would be done by your shell (even if that is DOS). 
> Have you tried it?
> 
> The output of:
> 
>     perl -le 'print for @ARGV' *
> 
> looks very much like an approximation of `ls` (or `dir`) to me.

If it doesn't, look into using the glob() function (perldoc -f glob).
Something like

  perl -le "print for glob( $ARGV[0] )" *

ought to point you in the general direction.

-- 
Eric Wong
eric+pgp@taedium.com                        http://www.taedium.com/pgp.txt


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

Date: Wed, 10 Oct 2001 16:56:44 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: wildcard in argument of script
Message-Id: <ruu8sto51gb8stkmq96l9f07tsjns8fjo9@4ax.com>

Hessu wrote:

>I have script which is doing fine but
>now I'm finishing it and I'd like it
>handle files by wildcards.
>but I 'd like make these work too:
>
> - perl myscript *.*
> - perl myscript *.txt *.dat
>
>is ther build-in way to do this perl,
>similar to glob?

Yes. The function is called... "glob". Really!

Search this newsgroup, for a very recent thread with subject line:
"binmode with "perl -p" doesn't work". The first message in this
subthread can be found in the archives under the URL:

<http://groups.google.com/groups?selm=a73bcad1.0110071923.280bb9b%40posting.google.com>

-- 
	Bart.


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

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


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