[29865] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1108 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Dec 12 16:09:46 2007

Date: Wed, 12 Dec 2007 13:09:11 -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           Wed, 12 Dec 2007     Volume: 11 Number: 1108

Today's topics:
    Re: (1)[0] ok but not 1[0] <ben@morrow.me.uk>
    Re: (1)[0] ok but not 1[0] <sensorflo@gmail.com>
    Re: (1)[0] ok but not 1[0] <nospam-abuse@ilyaz.org>
    Re: (1)[0] ok but not 1[0] <nospam-abuse@ilyaz.org>
    Re: (1)[0] ok but not 1[0] <bik.mido@tiscalinet.it>
        auto/Tk/getEncoding.al file is missing mariakvelasco@gmail.com
        Get values  from hash  "in order" <wheeledBobNOSPAM@yahoo.com>
    Re: Get values  from hash  "in order" <john@castleamber.com>
    Re: Get values  from hash  "in order" <jurgenex@hotmail.com>
    Re: Get values  from hash  "in order" <ben@morrow.me.uk>
    Re: Get values  from hash  "in order" <kkeller-usenet@wombat.san-francisco.ca.us>
    Re: Get values from hash "in order" <smallpond@juno.com>
        How can I name my scalar with a for loop? vorticitywolfe@gmail.com
    Re: How can I name my scalar with a for loop? <noreply@gunnar.cc>
    Re: lwp POST and proxies <john@castleamber.com>
    Re: split is not convinient <1usa@llenroc.ude.invalid>
    Re: split is not convinient (Randal L. Schwartz)
    Re: split is not convinient <joost@zeekat.nl>
    Re: split is not convinient <jurgenex@hotmail.com>
    Re: split is not convinient xhoster@gmail.com
    Re: split is not convinient <ben@morrow.me.uk>
    Re: Stupid Mistakes <ben@morrow.me.uk>
    Re: Why isn't void context a 'real' context pendley_is_ahomo@yahoo.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 12 Dec 2007 16:19:04 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: (1)[0] ok but not 1[0]
Message-Id: <o8e535-ie2.ln1@osiris.mauzo.dyndns.org>


Quoth Ben Morrow <ben@morrow.me.uk>:
> 
> No, this is incorrect. There are three different operators in Perl
> spelled []: array subscript, array slice, and list slice.

There is a fourth, of course: the anon array constructor. Duh.

Ben



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

Date: Wed, 12 Dec 2007 10:54:35 -0800 (PST)
From: Florian Kaufmann <sensorflo@gmail.com>
Subject: Re: (1)[0] ok but not 1[0]
Message-Id: <de4a313a-2bd6-465e-be9c-53656cc0e874@f3g2000hsg.googlegroups.com>

> I hope this helps you understand it: it *is* rather confusing :).

Yes, wonderful. Thats what I wrote down for myself to summarize, after
I read all your answers and made some tests:

1) $arraypseudoref[scalarcontext]    array subscript
2) @arraypseudoref[listcontext]      array slice
3) (listcontext)[listcontext]        list slice

arraypseudoref = Something that refers to an array. E.g.
-) an array identifier: my_array
-) a reference to an array (possibly anonymous): $ref_to_array and
{[1,2]} respectively
The braces are needed. [1,2] or ([1,2]) don't work.

I think I have now have described for me any array/list slices/
subscripts whatsoever that are possible in Perl.

What I know obviously have to read about more is what the correct
names/terms are, and how to better describe/specify them. I wonder how
"Programming Perl" calls what I have called arraypseudoref, and how it
is specified exactly.

Thank you

Flo

Just if anybody also wants to play with subscript/slice operators -
with the following script I tried to clarify things.

#! /bin/perl -w

sub foo { wantarray ? (print ("y"),(0,1)) : (print ("n"),2) }
sub bar { @a }

@a = (1..100);

foo();
print "\n";

print "-- 1 --------------\n";
${[1..100]}[foo()];
print "\n";
${[1..100]}[foo(),foo()];
print "\n";
@{[1..100]}[foo()];
print "\n";
@{[1..100]}[foo(),foo()];
print "\n";

print "-- 2 --------------\n";
(bar())[foo()];
print "\n";
(bar())[foo(),foo()];
print "\n";

print "-- 3 --------------\n";
$s = $a[foo()];
print "\n";
$s = $a[foo(),foo()];
print "\n";
$s = @a[foo()];
print "\n";
$s = @a[foo(),foo()];
print "\n";
$s = (1..100)[foo()];
print "\n";
$s = (1..100)[foo(),foo()];
print "\n";

print "-- 4 --------------\n";
@a2 = $a[foo()];
print "\n";
@a2 = $a[foo(),foo()];
print "\n";
@a2 = @a[foo()];
print "\n";
@a2 = @a[foo(),foo()];
print "\n";
@a2 = (1..100)[foo()];
print "\n";
@a2 = (1..100)[foo(),foo()];
print "\n";


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

Date: Wed, 12 Dec 2007 19:43:11 +0000 (UTC)
From:  Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: (1)[0] ok but not 1[0]
Message-Id: <fjpdkf$4cl$1@agate.berkeley.edu>

[A complimentary Cc of this posting was NOT [per weedlist] sent to
Michele Dondi 
<bik.mido@tiscalinet.it>], who wrote in article <qnhvl3di4b0t051u1q83tp0afbola2d58v@4ax.com>:
> >$x = (3)[0];

> >work. But then again, why shoudn't that work?
> >$x = 3[0];

> Because 3 is not a list.

Nonsense.  Of course, 3 is a list.

Hope this helps,
Ilya


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

Date: Wed, 12 Dec 2007 19:50:08 +0000 (UTC)
From:  Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: (1)[0] ok but not 1[0]
Message-Id: <fjpe1g$4i6$1@agate.berkeley.edu>

[A complimentary Cc of this posting was sent to
Florian Kaufmann 
<sensorflo@gmail.com>], who wrote in article <de624b45-bc92-4dc8-a099-7d24afad2771@s19g2000prg.googlegroups.com>:
> Perldoc says that "List values are denoted by separating individual
> values by commas (and enclosing the list in parentheses where
> precedence requires it):". To repeat the point, parentheses are not
> required to create/specify lists.

Correct.

> Thus the following two should be equivalent too in my view
> $x = 3[0];
> $x = (3)[0];

This is not so straightforward.  `[0]' is not a "pure operator"; it is
a "special syntax".  When you write $a[0], it is not equivalent to
($a)[0], right?  The latter IS an operator; the former is a syntaxic
sugar for aelt_scalar(\@a, 0).

I THINK that the parser tries to handle 3[0] the same as $3[0], and
the "syntax error" tries to tell you something like that `3' is not a
valid array name.

So there ARE several situations where parentheses mean more than "just
precedence".  IMO, it is an indication of the sad state of affair with
Perl documentation that there is no place in perl docs which lists all
of them...

Hope this helps,
Ilya


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

Date: Wed, 12 Dec 2007 21:14:15 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: (1)[0] ok but not 1[0]
Message-Id: <apf0m3pasfakum707n7lpa4v4u439hk85l@4ax.com>

On Wed, 12 Dec 2007 19:50:08 +0000 (UTC), Ilya Zakharevich
<nospam-abuse@ilyaz.org> wrote:

>So there ARE several situations where parentheses mean more than "just
>precedence".  IMO, it is an indication of the sad state of affair with
>Perl documentation that there is no place in perl docs which lists all
>of them...

Anyway, while the OP is confused by the fact e.g. 3[0] doesn't do what
he thinks it should, the current syntax for the wanted semantics seems
intuitive and dwimmy to me. I, for one, would find 3[0] to be
syntactically valid and semantically valid, to imply that 3 is an
autoboxed object (which in Perl 5 generally isn't unless using the
autobox module) and doing the role of an array. Perhaps this would
have some sense if the semantics were that [0..2](0), but then it
would be very inconsistent with $n[0] being the first element of @n.
If sigils were invariant as they are in Perl 6, then this
inconsistency would not exist, but there wouldn't be much need to give
3[0] a meaning, either.


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: Wed, 12 Dec 2007 11:40:06 -0800 (PST)
From: mariakvelasco@gmail.com
Subject: auto/Tk/getEncoding.al file is missing
Message-Id: <96395444-ad40-46df-a019-de29c0f7c694@i72g2000hsd.googlegroups.com>

Hello everyone,

We installed perl-Tk-804.026 on a Solaris 10 machine and everything
seem to have been installed properly.  We were then able to build our
executable on the same Solaris 10 machine using perl2exe with no
problem it seems; however, when we try to run the actual exe on the
same machine.  We got some errors like below:

PLEASE SEE THE PERL2EXE USER MANUAL UNDER "Can't locate somemodule.pm
in @INC"
FOR AN EXPLANATION OF THE FOLLOWING MESSAGE:
Can't locate auto/Tk/getEncoding.al in @INC (@INC contains:
PERL2EXE_STORAGE /vol/tools6/clearcase/sol_output /tmp/p2xtmp-17237)
at PERL2EXE_STORAGE/Tk/MainWindow.pm line 55

So we went to check if there was even a file in auto/Tk/getEncoding.al
and found that there was no such file in that directory.  Is this file
something that comes with perl-Tk-804.026 or is it a file that gets
created when installing perl-Tk.  If there is anyone out there that
has more information about this file, we would really appreciate the
help.

Thanks so much!




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

Date: Wed, 12 Dec 2007 17:37:11 GMT
From: still just me <wheeledBobNOSPAM@yahoo.com>
Subject: Get values  from hash  "in order"
Message-Id: <nk60m39128krohj5vgn4fgq0apgqvjmitj@4ax.com>

Newbie hash question:

I have a hash loaded with keys / values. I need to pull the values out
in the same order I have them physically listed in the source program
(or use some scheme to achieve that). 

I pulled them like this: 

	my $tempValue="";
	foreach $tempValue (values %outputFileHeadings)
	{
		$outputMessage = $outputMessage .  $tempValue 
	}

They seem to come out in a random order. Is there some way to pull
them in the same order they are physically listed in the program ?




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

Date: 12 Dec 2007 17:40:20 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: Get values  from hash  "in order"
Message-Id: <Xns9A0476BCE9BFCcastleamber@130.133.1.4>

still just me <wheeledBobNOSPAM@yahoo.com> wrote:

> Newbie hash question:
> 
> I have a hash loaded with keys / values. I need to pull the values out
> in the same order I have them physically listed in the source program
> (or use some scheme to achieve that). 
> 
> I pulled them like this: 
> 
>      my $tempValue="";
>      foreach $tempValue (values %outputFileHeadings)
>      {
>           $outputMessage = $outputMessage .  $tempValue 
>      }
> 
> They seem to come out in a random order. Is there some way to pull
> them in the same order they are physically listed in the program ?

perldoc -q order

-- 
John

Arachnids near Coyolillo - part 1
http://johnbokma.com/mexit/2006/05/04/arachnids-coyolillo-1.html


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

Date: Wed, 12 Dec 2007 18:01:35 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Get values  from hash  "in order"
Message-Id: <3mV7j.1724$sf.735@trndny04>

still just me wrote:
> Newbie hash question:
>
> I have a hash loaded with keys / values. I need to pull the values out
> in the same order I have them physically listed in the source program

Then you are using an inappropriate data structure. Hashes by their very 
nature do not have any odering, therefore they can't preserve something that 
doesn't even exist in their world.

> (or use some scheme to achieve that).

Use a data structure that supports the concept of sequences like an array.

> They seem to come out in a random order.

That is by design.

> Is there some way to pull
> them in the same order they are physically listed in the program ?

No.

jue 




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

Date: Wed, 12 Dec 2007 18:34:04 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Get values  from hash  "in order"
Message-Id: <s5m535-td4.ln1@osiris.mauzo.dyndns.org>


Quoth still just me <wheeledBobNOSPAM@yahoo.com>:
> Newbie hash question:
> 
> I have a hash loaded with keys / values. I need to pull the values out
> in the same order I have them physically listed in the source program

This is a FAQ: perldoc -q order.

Ben



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

Date: Wed, 12 Dec 2007 11:27:28 -0800
From: Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us>
Subject: Re: Get values  from hash  "in order"
Message-Id: <0ap535xjat.ln2@goaway.wombat.san-francisco.ca.us>

On 2007-12-12, still just me <wheeledBobNOSPAM@yahoo.com> wrote:
>
> I have a hash loaded with keys / values. I need to pull the values out
> in the same order I have them physically listed in the source program
> (or use some scheme to achieve that). 

Then you need an array, not a hash.

> 	my $tempValue="";
> 	foreach $tempValue (values %outputFileHeadings)
> 	{
> 		$outputMessage = $outputMessage .  $tempValue 
> 	}

smallpond already made one suggestion: list the keys in the order you
wish them to come out.  Here, it doesn't seem like you're even using the
keys at all, so you could do something like

my @outputFileHeadings=qw/foo bar blarg/;
my $outputMessage=join '',@outputFileHeadings;

If you do use the keys, you could try to convert to an array of hashrefs
if ordering is important throughout your program.  You might want to
look at perldoc perlreftut (if you haven't already) to learn how
references work, and whether they're appropriate for your data.

--keith



-- 
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information



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

Date: Wed, 12 Dec 2007 10:24:47 -0800 (PST)
From: smallpond <smallpond@juno.com>
Subject: Re: Get values from hash "in order"
Message-Id: <99ac4b7a-927d-4e49-ac56-d431fbe67e93@e4g2000hsg.googlegroups.com>

On Dec 12, 12:37 pm, still just me <wheeledBobNOS...@yahoo.com> wrote:
> Newbie hash question:
>
> I have a hash loaded with keys / values. I need to pull the values out
> in the same order I have them physically listed in the source program
> (or use some scheme to achieve that).
>
> I pulled them like this:
>
>         my $tempValue="";
>         foreach $tempValue (values %outputFileHeadings)
>         {
>                 $outputMessage = $outputMessage .  $tempValue
>         }
>
> They seem to come out in a random order. Is there some way to pull
> them in the same order they are physically listed in the program ?


foreach $key (key1, key2, key3, key4, ... ) {
   $tempValue = $outputFileHeadings{$key};
   $outputMessage = $outputMessage .  $tempValue;
}



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

Date: Wed, 12 Dec 2007 12:32:15 -0800 (PST)
From: vorticitywolfe@gmail.com
Subject: How can I name my scalar with a for loop?
Message-Id: <dd0687f3-c1ec-4fb1-b684-85b235035923@l32g2000hse.googlegroups.com>

How can I name/define my scalar with a for loop?

@stn=('A','B','C');

$count=0;
foreach($stn(@stn){

$Q.$count=$stn;

}

desired output:

Q1=A
Q2=B
Q3=C

Thanks for your help!

Jonathan


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

Date: Wed, 12 Dec 2007 21:59:08 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: How can I name my scalar with a for loop?
Message-Id: <5sb096F17ivd3U1@mid.individual.net>

vorticitywolfe@gmail.com wrote:
> 
> @stn=('A','B','C');
> 
> $count=0;
> foreach($stn(@stn){
> 
> $Q.$count=$stn;
> 
> }
> 
> desired output:
> 
> Q1=A
> Q2=B
> Q3=C

     use strict;
     use warnings;
     my @stn = ('A','B','C');
     my $count = 0;
     foreach my $stn ( @stn ) {
         print 'Q' . ++$count . "=$stn\n";
     }

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


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

Date: 12 Dec 2007 17:26:58 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: lwp POST and proxies
Message-Id: <Xns9A047478BDAF8castleamber@130.133.1.4>

Ben Morrow <ben@morrow.me.uk> wrote:

> Say what you were trying to open, and why it failed.
> 
>     open(my $FILE, '<', $data_file) 
>         || die("Could not open '$data_file': $!");

I prefer to add reading (in this case), i.e,

    	die "Could not open '$data_file' for reading: $!";

-- 
John

Arachnids near Coyolillo - part 1
http://johnbokma.com/mexit/2006/05/04/arachnids-coyolillo-1.html


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

Date: Wed, 12 Dec 2007 17:06:03 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: split is not convinient
Message-Id: <Xns9A047B16ECBB2asu1cornelledu@127.0.0.1>

Todd <xueweizhong@gmail.com> wrote in news:20d30e4a-8ded-4a3a-a99b-
7828f7dad487@e23g2000prf.googlegroups.com:

> case2:
> 
>     split /xxx/ # default to split /xxx/, $_
> 
> case3:
> 
>     split ' ', expr # ????
> 
> 
> I do think case3 is most frequently used, but why i've to type ' '
> every time, why there are no simpler syntax to use in this case?


How would you decide whether the following is case 1 or case 2?

split 'hello';

The designers of the API made a choice that if only one argument is 
specified, then it will be interpreted as the pattern.

Sinan

-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>



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

Date: Wed, 12 Dec 2007 09:04:23 -0800
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: split is not convinient
Message-Id: <861w9rone0.fsf@blue.stonehenge.com>

>>>>> "Todd" == Todd  <xueweizhong@gmail.com> writes:

Todd> case3:

Todd>     split ' ', expr # ????


Todd> I do think case3 is most frequently used, but why i've to type ' '
Todd> every time, why there are no simpler syntax to use in this case?

Because split is positional, and it's only 4 characters to type that, and it's
not *that* common.

print "Just another Perl hacker,"; # the original

-- 
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: 12 Dec 2007 18:57:04 GMT
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: split is not convinient
Message-Id: <47602f00$0$16792$e4fe514c@dreader27.news.xs4all.nl>

On Wed, 12 Dec 2007 08:07:57 -0800, Todd wrote:
>     split ' ', expr # ????
> 
> 
> I do think case3 is most frequently used, but why i've to type ' ' every
> time, why there are no simpler syntax to use in this case?

AFAIK the perl built-in functions that have default arguments always only 
allow you to remove the right-most argument(s).

That would make it a case of which of these in general are used the most 
(written in full for clarity):

split /some pattern/,$_;

or

split ' ',$some_string;

And I can definitely tell you I use the former much more often than the 
latter.

For instance:

while (<STDIN>) {
  chomp;
  my ($n,$v) = split /=/;
}

I probably use split /=/; already more often than I use split; split ' ',
$string and split/ /,$string; combined.

Joost.



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

Date: Wed, 12 Dec 2007 19:12:13 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: split is not convinient
Message-Id: <hoW7j.22401$Bg7.18329@trndny07>

Todd wrote:
> When I use split, there are serveral case frequently occured:
> case1:
>    split # default to split ' ', $_
>
> case2:
>    split /xxx/ # default to split /xxx/, $_
>
> case3:
>    split ' ', expr # ????
>
> I do think case3 is most frequently used, but why i've to type ' '
> every time, why there are no simpler syntax to use in this case?

One argument not being mentioned by others so far is that when a Perl 
function allows omitting of an argument and thus substituting a default 
argument it will be $_. Thisis the typical and expected Perl behaviour.
Interpreting a single argument as the text string rather than the search 
pattern would be unexpected and contradictory to the typical behaviour of 
all other Perl functions.

jue 




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

Date: 12 Dec 2007 19:23:49 GMT
From: xhoster@gmail.com
Subject: Re: split is not convinient
Message-Id: <20071212142351.665$db@newsreader.com>

Todd <xueweizhong@gmail.com> wrote:
> Hi,
>
> When I use split, there are serveral case frequently occured:
>
> case1:
>
>     split # default to split ' ', $_
>
> case2:
>
>     split /xxx/ # default to split /xxx/, $_
>
> case3:
>
>     split ' ', expr # ????
>
> I do think case3 is most frequently used,

Not by me.  When I use split on something other than $_, I almost
always use a pattern other than ' ', which I guess would be case4.

After a brief grep through some code, it looks like the order from most
used to least used would be case2 > case1 > case4 > case3

> but why i've to type ' '
> every time, why there are no simpler syntax to use in this case?


You could define a subroutine name splitd (d for default, as in ' ' being
the default pattern)

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.


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

Date: Wed, 12 Dec 2007 20:33:04 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: split is not convinient
Message-Id: <05t535-ljm1.ln1@osiris.mauzo.dyndns.org>


Quoth "Jürgen Exner" <jurgenex@hotmail.com>:
> 
> One argument not being mentioned by others so far is that when a Perl 
> function allows omitting of an argument and thus substituting a default 
> argument it will be $_. Thisis the typical and expected Perl behaviour.
> Interpreting a single argument as the text string rather than the search 
> pattern would be unexpected and contradictory to the typical behaviour of 
> all other Perl functions.

Uh huh... like shift, or readline, or the return value of split in
scalar context? There are many things I like about Perl; consistency is
not one of them.

:)

Ben



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

Date: Wed, 12 Dec 2007 16:15:31 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Stupid Mistakes
Message-Id: <32e535-ie2.ln1@osiris.mauzo.dyndns.org>


Quoth inthepickle <inthepickle@gmail.com>:
> Apparently I am making a stupid mistake, but I can't see it.  I have a
> text file with one 6 digit number per line which corresponds to a file
> name on a network directory.  I want to find each file, then copy it
> somewhere.  I am trying to do it in steps to make the find a little
> faster.  If it finds the file in the first step, it should not go
> through to the others, but it does.  Testing a file, I found it in the
> fist step.  The length of $f was 38.  Can't figure out why it is not
> picking up on the length of $f in the second step.  What am I doing
> wrong?  Please help!

You need

    use strict;
    use warnings;

here, and you need to fix all the errors 'use strict' gives you.

> use File::Find ;
> system ( "cls" ) ;

While this is OK, I'd rather do it in Perl.

    use Term::ANSIScreen qw/cls/;

    cls;

> $incoming = "D:/Cutlist.txt" ; #directory to look in for job sheets:

    my $incoming = ...

and so on elsewhere.

> $destination = "D:/SWFiles/"; #copy destination
> open INC, $incoming ; #open file

*Always* check the return value of open.
Use three-arg open.
Use lexical filehandles.
</scratched record>

    open my $INC, '<', $incoming
        or die "can't read '$incoming': $!";

> @cutlist = <INC> ; #assign file to array
> close INC ; #close file

 ...and then you don't need to explicitly close, since you're not
checking the return value anyway (there's no need here).

    my @cutlist = do {
        open my $INC, '<', $incoming or die ...;
        <$INC>;
    };

> foreach ( @cutlist ) { #for every number in file
> 	$swfile = $_ ;

You're just clobbering $_ for no reason.

    foreach my $swfile ( @cutlist ) {

> 	chop ( $swfile ) ;

Use chomp, not chop. Also, you can chomp a whole array at once, with

    chomp @cutlist;

before the loop.

> 	$subdir = substr ( $swfile,0,4 ) ; #get first 4 digits
> 	print "Searching Y:/SolidWorks Files/$subdir\n" ;
> 	@filetree = ( "Y:/SolidWorks Files/$subdir" ) ; #set to part sub dir

Why are you using an array when it only ever has one element? Why do you
specify the path twice?

    my $filetree = "Y:/SolidWorks Files/$subdir";
    print "Searching $filetree\n";

Also, I would set $\ = "\n" at the top and leave it out of all the print
statements.

> 	find ( \&findswfile,@filetree ) ;
> 	if ( length ( $f ) < 2 ) { # if subdir finds failed

What's $f? ...oh, I see, it's a global set by findswfile. This is a very
bad idea: I had to look quite hard to work out where that value was
coming from. You're in a slightly tricky situation here: File::Find's
API is not terribly well designed (it's a translation of the find.pl
library that used to come with perl 4, so it's kept some bad Perl-4ish
habits :) ), and it doesn't make returning a result very easy. At the
very least, using a more distinctive name and putting

    my $Found;  # set by findswfile

(using Initial_Caps for globals and ALL_CAPS for constants is a useful
habit to get into) just above the foreach loop would have saved me (and
you, when you come back to this in six months' time) some searching. You
will also need to explicitly set it to undef at the top of the loop, as
otherwise it will still be set to the last file you found. This is the
problem you actually asked about :). I'll show you a better solution at
the end.

Testing for length < 2 here is not very clear: what you care about is
that $f has been set at all, so just use

    if ($Found) {

> 		print "Searching Y:/SolidWorks Files/\n" ;
> 		@filetree = ( "Y:/SolidWorks Files/" ) ; #set to sw dir

Again you're duplicating the path. Every time you specify a literal like
this more than once, it's a bug waiting to happen when you need to
change the base path. You want something like

    my $BASEDRIVE = 'Y:';
    my $BASEPATH  = "$BASEDRIVE/SolidWorks Files';

at the top, and then use

    my $filetree = "$BASEPATH/$subdir";

above and $BASEPATH here. For more portability (and clarity) you could
use File::Spec, but that's likely not very useful in a
situation-specific program like this.

> 		find ( \&findswfile,@filetree ) ;
> 	} ; #end if for sw dir

You don't need that ';'.

The point of using sensible indentation, and blank lines between
sections of code, is to make comments like this unnecessary. Again, it's
just waiting for you to change something, so it can sit there being
wrong and confusing you :).

> 	if ( length ( $f ) < 2 ) {# if subdir & swdir finds failed
> 		print "Searching Y:/\n" ;
> 		@filetree = ( "Y:/" ) ; #set entire drive
> 		find ( \&findswfile,@filetree ) ;
> 	} ; #end if for entire drive
> }; #foreach
> print "\nEnd Program\n" ;
> sub findswfile {
> 	if ( ( /$swfile/ ) & ( /.SLDPRT/ | /.sldprt/ ) ) { #if part and
> (caliptal or lower) match

Don't use bitwise operators (& and |) when you mean logical operators
(&& and ||). At some point they won't do what you expect.

'.' is a special character in a regex, and needs escaping with \. When
interpolating into a regex you should usually use \Q..\E, as otherwise
special characters in $swfile will be interpreted by the regex engine.

If you really mean .SLDPRT or .sldprt *only*, this is fine, but if names
like 00000.sldPRT are OK (or don't occur) you can make this clearer:

    if ( / \Q $swfile \E \.sldprt /xi )

(I'm making lots of assumptions about what your filenames actually look
like, here, and I may be wrong. In that case, ignore me :).)

> 		$f = $File::Find::name ;	#directory and file name
> 		print "     Found: $f\n\n" ;
> 		system ( "copy \"$f\" \"$destination\" >nul" ) ; #copy file to
> destination

You can use qq{} to avoid escaping the quotes

    system( qq{copy "$f" "$destination" >nul} );

Also, I don't think DOS copy supports using / as a directory separator,
so you may need to convert it

    (my $DOSfile = $f) =~ s,/,\\,g;

Depending on how much you care about creation dates &c., you may find
File::Copy easier. You could also call CopyFile directly using
Win32API::File.

> 		} else {
> 			return ; #try again
> 	 } ; #end file match
> } ; #endsub

I would write this using File::Find::Rule, which makes it much easier.
Something like (untested)

    use File::Find::Rule;
    use Win32API::File      qw/CopyFile/;

    for my $swfile (@cutlist) {
        my $subdir = substr $swfile, 0, 4;

        for my $base ("$BASEPATH/$subdir", $BASEPATH, "$BASEDRIVE/") {

            print "Searching $base";

            File::Find::Rule
                ->name(qr/\Q $swfile \E \.sldprt/xi)
                ->exec(sub {
                    my ($src, $dest) = ($_[2], "$destination/$_");

                    print "    Found: $src";
                    CopyFile $src, $dest
                        or die "can't copy '$_[2]' to '$dest': $^E";
                })
                ->in($base)
                and last;
        }
    }

Notice how the inner loop tries each of your alternatives in turn, and
the 'and last' jumps out as soon as one of them found something.

Ben



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

Date: Wed, 12 Dec 2007 12:21:58 -0800 (PST)
From: pendley_is_ahomo@yahoo.com
Subject: Re: Why isn't void context a 'real' context
Message-Id: <0abdde68-4887-4087-9368-068c1006c66c@o42g2000hsc.googlegroups.com>

On Dec 11, 5:01 pm, mer...@stonehenge.com (Randal L. Schwartz) wrote:

>
> print "Just another Perl hacker," ; # the original

NO, you asshole. Larry Wall is the original Perl hacker. He invented
the fucking language!

You are "Just another convicted felon," ; # convicted of knowingly
accessing and using a computer and computer network for the purpose of
committing theft.

#!/usr/bin/perl
while (<DATA>) {
    chomp;
    print "$_ is a turd\n";
}

__DATA__
Charlton Wilbur
John Bokma
Paul Lalli
J=FCrgen Exner
Tad McClellan
A. Sinan Unur a.k.a. A.Sinine
Michele Dondi
Uri Guttman a.k.a. Urine Buttman
Sherman Pendley


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

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.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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 V11 Issue 1108
***************************************


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