[24145] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 6339 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Mar 30 11:05:42 2004

Date: Tue, 30 Mar 2004 08:05:09 -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           Tue, 30 Mar 2004     Volume: 10 Number: 6339

Today's topics:
    Re: Cannot capture message from external program. <tadmc@augustmail.com>
        RE: Converting From C to Perl <nospamplease@no.com>
    Re: Included directory problem <uri@stemsystems.com>
    Re: Included directory problem <chatiman@free.fr>
    Re: Included directory problem <uri@stemsystems.com>
    Re: Included directory problem <chatiman@free.fr>
        move() sometimes failing with cross-device error? (B Wooster)
    Re: Net::SSH  Need example how to use..can't find one.. (cayenne)
        newbie : please explain this behaviour <madhurahuja_delhi@vsnl.net>
    Re: newbie : please explain this behaviour <tore@aursand.no>
    Re: newbie : please explain this behaviour (Anno Siegel)
        Passing a struct to a subroutine (Graham)
    Re: Syntax error <kuujinbo@hotmail.com>
    Re: Syntax error <nobull@mail.com>
    Re: Syntax error <bernard.el-haginDODGE_THIS@lido-tech.net>
        Trap errors inside MSDOS batch file <Thalaiappan.Sundaramoorthy@sbsc.siemens.co.uk>
        Using Net::LDAP to query for accounts -- returns no dat (Steve The Geek)
        Variable substitution in variable name (mshetty)
    Re: Variable substitution in variable name <1usa@llenroc.ude>
    Re: Variable substitution in variable name <noreply@gunnar.cc>
    Re: Variable substitution in variable name <ittyspam@yahoo.com>
    Re: Variable substitution in variable name <raisin@delete-this-trash.mts.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 30 Mar 2004 07:07:01 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Cannot capture message from external program.
Message-Id: <slrnc6is7l.e1i.tadmc@magna.augustmail.com>

Simon Taylor <simon@unisolve.com.au> wrote:
> Hon Seng Phuah wrote:


>> How do I capture the output error message,


> You could try the following, (the '2>&1' is a shell construction that 
> ensures that anything printed to the standard error stream is
> redirected to standard out):


> Hope this helps,


Hope the OP already knew that, as it is covered in the documentation
for the function he is using.

I wouldn't want to think that he would ask thousands of people
around the world without looking a bit himself first...


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


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

Date: Tue, 30 Mar 2004 16:50:18 +0100
From: "SketchySteve" <nospamplease@no.com>
Subject: RE: Converting From C to Perl
Message-Id: <6zgac.108$OJ.46@newsfe2-gui.server.ntli.net>

Hi there,

I'm currently working on neural network project. The program i'm using
(SNNS) will automaticly create a network as a c function which can be
called.

I know there are tools for converting Perl into C but I was wondering if
there were any which worked the other way round.

Thanks for your help

Steve




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

Date: Tue, 30 Mar 2004 14:32:41 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Included directory problem
Message-Id: <x765cmquba.fsf@mail.sysarch.com>

>>>>> "c" == chatiman  <chatiman@free.fr> writes:

  >> >
  >> > FAQ. did you think you were the first person (or even the thousandth) to
  >> > ask this?
  >> 
  >> thousandth -> millionth
  >> 
  c> and how many answers ?
  c> if the answer is
  c> use lib "prefix"
  c> then this does not work with perl 5.005
  c> is there another answer ? except PERL5LIB=prefix and perl -Iprefix
  c> which don't work too ?

huh? how does it not work with 5.005? and why don't those other
solutions work? what does 'does not work' mean? what happens? does you
computer spit out fire and ash? does it dance a lousy jig?

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: Tue, 30 Mar 2004 17:03:20 +0200
From: "chatiman" <chatiman@free.fr>
Subject: Re: Included directory problem
Message-Id: <40698c2c$0$14039$626a14ce@news.free.fr>


"Uri Guttman" <uri@stemsystems.com> a écrit dans le message de news:
x765cmquba.fsf@mail.sysarch.com...
> >>>>> "c" == chatiman  <chatiman@free.fr> writes:

> huh? how does it not work with 5.005? and why don't those other
> solutions work? what does 'does not work' mean? what happens? does you
> computer spit out fire and ash? does it dance a lousy jig?
>
well no :)
It just cannot find the module and throws an error like :
Can't locate Time::HiRes in @INC(./perl ...)

The module is in fact located in :
 ./perl/lib/5.00503/i386-linux/

It will work if I do :
use lib "./perl/lib/5.00503/i386-linux/"

Instead of installing this package (Time::HiRes) each time,
Can I use the compiled version of these modules
on any higher perl Version ?





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

Date: Tue, 30 Mar 2004 15:08:40 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Included directory problem
Message-Id: <x7ptaupe2w.fsf@mail.sysarch.com>

>>>>> "c" == chatiman  <chatiman@free.fr> writes:

  c> "Uri Guttman" <uri@stemsystems.com> a écrit dans le message de news:
  c> x765cmquba.fsf@mail.sysarch.com...
  >> >>>>> "c" == chatiman  <chatiman@free.fr> writes:

  >> huh? how does it not work with 5.005? and why don't those other
  >> solutions work? what does 'does not work' mean? what happens? does you
  >> computer spit out fire and ash? does it dance a lousy jig?
  >> 
  c> well no :)
  c> It just cannot find the module and throws an error like :
  c> Can't locate Time::HiRes in @INC(./perl ...)

  c> The module is in fact located in :
  c> ./perl/lib/5.00503/i386-linux/

  c> It will work if I do :
  c> use lib "./perl/lib/5.00503/i386-linux/"

so it does work. you just had it wrong before. so don't say it doesn't
work. that is misleading.

and what is wrong with the use lib line you got to work?

  c> Instead of installing this package (Time::HiRes) each time,
  c> Can I use the compiled version of these modules
  c> on any higher perl Version ?

pure perl modules can be installed in a version independent place. xs
based modules need to be in a version dependent place but most perl5
versions should/could/would work with xs modules. but xs was changed
with 5.8 IIRC so modules for 5.8 have to be recompiled.

and what is so hard about installing the module? CPAN.pm or even manual
installation is trivial even when specifying a local dir.

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: Tue, 30 Mar 2004 17:21:42 +0200
From: "chatiman" <chatiman@free.fr>
Subject: Re: Included directory problem
Message-Id: <4069907b$0$14075$626a14ce@news.free.fr>


"Uri Guttman" <uri@stemsystems.com> a écrit dans le message de news:
x7ptaupe2w.fsf@mail.sysarch.com...
> and what is so hard about installing the module? CPAN.pm or even manual
> installation is trivial even when specifying a local dir.

I wanted to do a kind of self install script.
This program for exemple is to be installed on various
hosted web servers with various perl versions
by users probably not familiar with perl.

I was thinking the "lib" module was a bit "clever" than that
and when you do
perl Makefile.PL prefix=my_prefix
You could later do :
use lib "my_prefix";






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

Date: 30 Mar 2004 07:10:44 -0800
From: bwooster47@hotmail.com (B Wooster)
Subject: move() sometimes failing with cross-device error?
Message-Id: <75cc51dd.0403300710.33b6ef4b@posting.google.com>

use File::Copy;     # for move
 ...
  move("$temp_decode_file", "$real_name")

This is copying accross devices, and this code works most of the time, but
now once in a while I have started getting errors:
   Invalid cross-device link

But that can't be, since my logs that following succeeded
 move -> /tmp/1 -> /dosdrive/1
 move -> /tmp/2 -> /dosdrive/2
 move -> /tmp/3 -> /dosdrive/3 - FAILED!

Always fails at the same file, any ideas on what else I should look for?


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

Date: 30 Mar 2004 07:38:32 -0800
From: chilecayenne@yahoo.com (cayenne)
Subject: Re: Net::SSH  Need example how to use..can't find one...
Message-Id: <2deb3d1.0403300738.264af0d5@posting.google.com>

"J. Gleixner" <glex_nospam@qwest.invalid> wrote in message news:<VMY9c.469$gv3.26724@news.uswest.net>...
>
> 
> Make sure you have the following line in your code:
> 
> use Net::SSH qw(ssh_cmd);
> 
> Without that, your script doesn't know where to find "ssh_cmd".
> 
> > I tried using the ssh() function which looked similar to the ssh_cmd
> > function..but, crashed about the same way.
> > 
><snip>
> #!/usr/local/bin/perl
> use strict;
> use Net::SSH qw(ssh_cmd);
> 
> my $directory = '???'; #don't know what this is set to..
> my $file_name = 'fred_test_01_Mar_2004';
> my $user = 'user';
> my $host = 'sid';
> 
> ssh_cmd( {
>      user => $user,
>      host => $host,
>      command => "rm 
> /data/VOL1/oradata/backups/sid/$directory/$file_name.tar.gz",
>     } ) or die "Error: $!";
> 
> Same as using the ssh method:
> 
> ssh("$user\@host", "rm 
> /data/VOL1/oradata/backups/sid/$directory/$file_name.tar.gz") or die 
> "Error: $!";
> 
> only it's a bit more readable.
> 
<snip>

First...Thank You!!

This got me going. I'm going to re-write it to  use the => notation
too, it does make things look cleaner for sure.

I do have one last question, with regards to the single part that you
gave me that made it work. With regards to adding the use  qw(ssh_cmd)
part after the use Net::SSH declaration.

Why do you have to do this?  My first perl script I did, that
connected to an Oracle database, didn't seem to require this when
using the DBI module. On that one, all I had to declare was:

use DBI;

I didn't have to use the qw() function (this is usually used to load
an array as 'quoted words' is it not?)

In the future, when I try to use other modules, how can I know, from
looking at the .pm, when I need to use the qw() in the declaration for
individual functions, and when I do not have to?

The example I worked from for the DBI modules was:
http://www.saturn5.com/~jwb/dbi-examples.html

Anyway, thank you very much...I got it to working, but, would
appreciate if you could answer me back on this last question. While
its always nice to get a script running...I just want to make sure I
know why what I did made it work.

CC


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

Date: Tue, 30 Mar 2004 18:43:30 +0530
From: "madhur" <madhurahuja_delhi@vsnl.net>
Subject: newbie : please explain this behaviour
Message-Id: <c4brl7$2g4tvc$1@ID-81175.news.uni-berlin.de>

Hello
1) The code below doesn't executes properly if the use strict; line is
activated.
It gives errors:
Variable "@printarray" is not imported at alias1.pl line 18.
Variable "@printarray" is not imported at alias1.pl line 26.
Global symbol "@printarray" requires explicit package name at alias1.pl line
18.
Global symbol "@printarray" requires explicit package name at alias1.pl line
26.
Execution of alias1.pl aborted due to compilation errors.

What is the reason of these errors when all the variables are declared.

2)If I remove the use strict line; the code runs but I think value of @foo
should be changed to "madhur ahuja is great" but It doesnt.
It occurs only if I declare our foo instead of my foo.

3)I thought difference between my and our was of global and local only. But
there seems to be more difference too.


#!/usr/local/bin/perl
#use strict;
use warnings;

 my @foo = ("here's", "a", "list");    #if I change this declaration to our
$foo it works correctly
  &testsub (*foo);
 print "\n ss: @foo";

  sub testsub
  {
         local (*printarray) = @_;
          foreach my $element (@printarray)
          {
                 print ("df : $element\n");
         }
     @printarray=("madhur ahuja is great");
 }


Madhur




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

Date: Tue, 30 Mar 2004 15:54:33 +0200
From: Tore Aursand <tore@aursand.no>
Subject: Re: newbie : please explain this behaviour
Message-Id: <pan.2004.03.30.13.54.19.741641@aursand.no>

On Tue, 30 Mar 2004 18:43:30 +0530, madhur wrote:
> &testsub (*foo);

Why?  What do you expect this would do?

>   sub testsub
>   {
>          local (*printarray) = @_;
>           foreach my $element (@printarray)
>           {
>                  print ("df : $element\n");
>          }
>      @printarray=("madhur ahuja is great");
>  }

What's the logic behind this sub-routine?  Do you really want it to change
the contents of the @foo array (which is outside of it)?  If so, you could
try to pass the array reference to that sub-routine;

  my @foo = qw( here's a list );
  testsub( \@foo ); # No need to use the '&'

  print @foo;

  sub testsub {
      my $arrayref = shift;
      foreach ( @$arrayref ) {
          print "df : $_\n";
      }
      $arrayref = ['madhur ahuja is great'];
  }


-- 
Tore Aursand <tore@aursand.no>
"Life is pleasant. Death is peaceful. It's the transition that's
 troublesome." -- Isaac Asimov


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

Date: 30 Mar 2004 14:42:10 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: newbie : please explain this behaviour
Message-Id: <c4c102$pe6$1@mamenchi.zrz.TU-Berlin.DE>

madhur <madhurahuja_delhi@vsnl.net> wrote in comp.lang.perl.misc:
> Hello
> 1) The code below doesn't executes properly if the use strict; line is
> activated.
> It gives errors:
> Variable "@printarray" is not imported at alias1.pl line 18.
> Variable "@printarray" is not imported at alias1.pl line 26.
> Global symbol "@printarray" requires explicit package name at alias1.pl line
> 18.
> Global symbol "@printarray" requires explicit package name at alias1.pl line
> 26.
> Execution of alias1.pl aborted due to compilation errors.
> 
> What is the reason of these errors when all the variables are declared.

You are trying to take a typeglob "*foo" from $foo, but typeglobs *always*
pertain to package variables.  So, even if "my $foo" is declared,
"*foo" has nothing to do with it.

> 2)If I remove the use strict line; the code runs but I think value of @foo
> should be changed to "madhur ahuja is great" but It doesnt.
> It occurs only if I declare our foo instead of my foo.

See above.  The lexical $foo is never affected.

> 3)I thought difference between my and our was of global and local only. But
> there seems to be more difference too.

No, they're quite different structures.  A package variable has slots for
a scalar, an array, a hash, a filehandle, and a number of other things
as well.  The abstraction of this bundle of values is the typeglob.  There
is only one typeglob for the package variables $foo, @foo, %foo, etc,
which they all share.

Lexical variables store only one value of their declared type.  They
don't *have* a typeglob.

Using typeglobs for parameter passing is a concept from Perl 4, it
is no longer necessary.  Use references instead, which work exactly
the same for both types of variables.

Anno

[code for reference follows]

> #!/usr/local/bin/perl
> #use strict;
> use warnings;
> 
>  my @foo = ("here's", "a", "list");    #if I change this declaration to our
> $foo it works correctly
>   &testsub (*foo);
>  print "\n ss: @foo";
> 
>   sub testsub
>   {
>          local (*printarray) = @_;
>           foreach my $element (@printarray)
>           {
>                  print ("df : $element\n");
>          }
>      @printarray=("madhur ahuja is great");
>  }


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

Date: 30 Mar 2004 08:04:54 -0800
From: graham@dim.com (Graham)
Subject: Passing a struct to a subroutine
Message-Id: <8a420ed9.0403300804.790dba0e@posting.google.com>

For some reason I Can't seem to figure this out.

I have a struct:

use Class::Struct;

struct( stuff => {
        name => '$',
        rank => '$',
        serial => '$' }
 );


I have an assignment:

$soldier=stuff->new( name=>"Joe");

Now I want to pass this to a subroutine:

PrintSoldierName($soldier);

sub PrintSoldiername {

#
# this doesn't work
#

my $dude=stuff->new;
$dude= @_;
print $dude->name


}



How do I access the passed structure so that I can print the name of
the soldier in PrintSoldierName? I'd list out all the things I have
tried but it would be a very long post. ;-)

Thanks,
Graham


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

Date: Tue, 30 Mar 2004 20:42:22 +0900
From: ko <kuujinbo@hotmail.com>
Subject: Re: Syntax error
Message-Id: <c4bmf1$2ft64t$1@ID-227975.news.uni-berlin.de>

Martin Livingston wrote:
> Hi,
> 
> I have been looking at this for about 6 hours and I don't see the syntax
> errors (as follows)
> 
> - syntax error at /cgi-bin/polaris/external.lib line 40, near "; }"
> 
> - syntax error at /cgi-bin/polaris/external.lib line 58, near "}"
> 
> I am a newbie so its probably something very simple. Here is the script
> which is an external subroutine that reads the files of a directory and
> creates a new "contact sheet" every time someone uploads additional pictures
> to the file. The full path has been x'ed out in the example.

Not sure exactly why you got those errors, did a copy/paste and got 
different ones. Did you copy/paste in your post, or type it?

So I didn't go through the whole script, but a cursory inspection 
revealed a couple of problems. Hopefully the corrections will help get 
you started. After corrected it passed syntax checks (-c switch), 
assuming 'use strict' and 'use warnings'. Maybe that's the reason you 
got the different error messages - you should *always* start your 
scripts with:

use strict;
use warnings;

[snip code]

> print INDEX qq(<a href="$part1.$type">
===========================^^^^^========
Perhaps this should be $parts[1], or some other element of the @parts 
array? You didn't declare a $part1 variable.

[snip more code]

> If  ($parts[1] eq 'th')     {
==^^^===
Case matters, try 'if'.

[snip code]

The way you have the code formatted is *very* hard to read. Issue a 
'perldoc perlstyle' from your shell for some guidelines to make your 
scripts easier to read/maintain.

HTH - keith


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

Date: 30 Mar 2004 12:47:14 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Syntax error
Message-Id: <u965cm8sl9.fsf@wcl-l.bham.ac.uk>

"Martin Livingston" <mjpliv@hfx.eastlink.ca> writes:

> I have been looking at this for about 6 hours

> I am a newbie so its probably something very simple.

First a but of general advice...

You should always declare all variables as lexically scoped in the
smallest applicable lexical scope unless you have a positive is a
reason to do otherwise. BTW: this is not perculliar to Perl, it
applies in all programming languges - allowing that a languge not
having lexical variables is a positive reason :-).

For Perl this means that most of the time the declaration should be
combined with the first assignment. BTW: this to is not perculliar to
Perl, it also applies in other programming languges where assignment
and declaration can be combined.

By following this convention you will be able to get maximum beniefit
out of putting "use strict" at the top of all your scripts.

Try to get into this habit now, do not wait for your failure to do so
to cause you the unecessary distress of wasting your own time and that
of other people.  The longer you leave it the harder you will find it
to adjust.  Worse still, if you leave it too long you may never adjust
and may mutate into a bitter and twisted troll.

Also try indenting your code to make it easier to follow.

> and I don't see the syntax errors (as follows)
> 
> - syntax error at /cgi-bin/polaris/external.lib line 40, near "; }"
> 
> - syntax error at /cgi-bin/polaris/external.lib line 58, near "}"
> 
> Here is the script which is an external subroutine that reads the
> files of a directory and creates a new "contact sheet" every time
> someone uploads additional pictures to the file. The full path has
> been x'ed out in the example.

[snip]

Those characters do not seem to appear on the respective lines of the
stript you include.  Are you sure you included the whole of
external.lib and the same version that is on your web server?

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


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

Date: Tue, 30 Mar 2004 14:01:30 +0200
From: "Bernard El-Hagin" <bernard.el-haginDODGE_THIS@lido-tech.net>
Subject: Re: Syntax error
Message-Id: <Xns94BC8DEA4DB95elhber1lidotechnet@62.89.127.66>

"Martin Livingston" <mjpliv@hfx.eastlink.ca> wrote:

[...]

> If  ($parts[1] eq 'th')     {
  ^^


That should be "if" not "If".


-- 
Cheers,
Bernard


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

Date: Tue, 30 Mar 2004 16:49:12 +0100
From: "News" <Thalaiappan.Sundaramoorthy@sbsc.siemens.co.uk>
Subject: Trap errors inside MSDOS batch file
Message-Id: <c4c49l$nj4$1@news.mch.sbs.de>

Hi all,

I would like to know, how to trap errors while running perl script from
MSDOS batch file.

Actually I am planning to run multiple perl scripts (Script1.pl ,
Script2.pl... ) in  a batch file. Below is the logical requirement.

perl Script1.pl "argument 1"
if it is success                ##need some command for this
    perl Script2.pl "arg 1"
else
  exit.


Now I need some method to findout whether first Script execution is success
or not. Here success means, there is no error message generated while
running the script.

Can any one help me..??

Thanks in Advance,
Thalai








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

Date: 30 Mar 2004 07:16:00 -0800
From: slkleine@hotmail.com (Steve The Geek)
Subject: Using Net::LDAP to query for accounts -- returns no data
Message-Id: <863f122c.0403300716.69dd7953@posting.google.com>

Trying to use Net::LDAP to query Microsoft's LDAP implementation for
expired accounts; so far, I'm testing by having it find all accounts
that have the attribute useraccountcontrol. By default, all user
accounts have this attribute.

I get zero results, as commented in the code; doing a query with the
LDP utility from Microsoft's Win2K resource kit with the same
parameters returns every user account, as expected.

Note the username, password, and domain have been obfuscated. :-)

Thanks in advance!

Steve the (over-reaching his noob status) Geek

############################################################################
## SearchViaLdapForExpiredAccounts.pl
## Creation date: 20040329
## Last edit: 20040329
## Author: Steve Kleine
############################################################################
##  Searches Win2K LDAP for accounts that are both disabled and have
not
## been used for thirty (30) days.
############################################################################
##  NOTE: this is a proof-of-concept with an end goal of automation of
## account deletions, including email, home directory, and user
account
## information in AD.
############################################################################

require 5.002;
use strict;
use warnings;

use Net::LDAP;

my $entry;

my $ldap = Net::LDAP->new( 'corp.talbots.com' ) or die "$@";

my $mesg = $ldap->bind('cn=Testaccount',
                      password =>'Secret'
                     );
print "$mesg\n";  #Just to see what it produces

  $mesg = $ldap->search( # perform a search
                        base   => 'OU=North
America,DC=another,DC=domain,DC=com',
                        filter => '(useraccountcontrol=*)'
                        );
 print $mesg->count,"\n"; # $mesg->count should be 1000+; it actually
returns 0
 $mesg->code && die $mesg->error;

 foreach  $entry ($mesg->code)
 {
  print $mesg->count; # $mesg->count should be 1000+; it actually
returns 0
 }

 $mesg = $ldap->unbind;   # take down session


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

Date: 30 Mar 2004 05:58:00 -0800
From: mshetty@mail.com (mshetty)
Subject: Variable substitution in variable name
Message-Id: <bfbb8fd4.0403300558.6f63967c@posting.google.com>

Hi,

Have the following code where I want to reduce the lines of code:

#Variables to store total file count for each type
$total_cpp_file=0;
$total_h_file=0;
$total_c_file=0;
$total_inp_file=0;
$total_inl_file=0;
$total_hpp_file=0;
$total_cc_file=0;
$total_hh_file=0;
$total_yxx_file=0;
$total_y_file=0;
$total_l_file=0;

find (\&wanted, "S:\\src");
find (\&wanted, "S:\\inc");
sub wanted 
{
		store_file ($File::Find::name);
}

sub store_file ($)
{
    my $name = shift;
    if ($name =~ /\.(cpp)$/i) 
	{
	$total_cpp_file++;
        push @files_cpp, ($name);
    }
    elsif ($name =~ /\.(h)$/i) 
	{
	$total_h_file++;   
	push @files_h, ($name);
    }
    elsif ($name =~ /\.(c)$/i) 
	{
	$total_c_file++; 	
	push @files_c, ($name);
    }
	elsif ($name =~ /\.(inp)$/i) 
	{
	$total_inp_file++; 	
	push @files_INP, ($name);
    }
	elsif ($name =~ /\.(inl)$/i) 
	{
	$total_inl_file++; 	
	push @files_INL, ($name);
    }
	elsif ($name =~ /\.(hpp)$/i) 
	{
	$total_hpp_file++; 	
	push @files_HPP, ($name);
    }
	elsif ($name =~ /\.(cc)$/i) 
	{
	$total_cc_file++; 		
	push @files_CC, ($name);
    }
	elsif ($name =~ /\.(hh)$/i) 
	{
	$total_hh_file++; 		
	push @files_HH, ($name);
    }
	elsif ($name =~ /\.(yxx)$/i) 
	{
	$total_yxx_file++; 		
	push @files_YXX, ($name);
    }
	elsif ($name =~ /\.(y)$/i) 
	{
	$total_y_file++; 		
	push @files_Y, ($name);
    }
	elsif ($name =~ /\.(l)$/i) 
	{
	$total_y_file++; 		
	push @files_L, ($name);
    }elsif ($name =~ /\.(dsp)$/i) 
	{
	push @files_dsp, ($name);
    }
    
} #store_file end

IS there a shorter way to write this?

I mean can I store (cpp, h, c, inp, inl, hpp, cc, hh, yxx, y, l) in an
array(file_type and say push @files_$file_type[1]?

Thanks and Regards,
M Shetty


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

Date: 30 Mar 2004 14:18:00 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude>
Subject: Re: Variable substitution in variable name
Message-Id: <Xns94BC5E9B42C42asu1cornelledu@132.236.56.8>

mshetty@mail.com (mshetty) wrote in 
news:bfbb8fd4.0403300558.6f63967c@posting.google.com:

> Hi,
> 
> Have the following code where I want to reduce the lines of code:
> 
> #Variables to store total file count for each type

OK ...

snipped a bunch of lines of noise

> IS there a shorter way to write this?

Use a hash.

my %totals = (
    	cpp => 0,
    	hpp => 0
    	ini => 0,
      # ... etc etc
);

print 'Total for cpp: ', $totals{ini}, "\n";

-- 
A. Sinan Unur
1usa@llenroc.ude (reverse each component for email address)


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

Date: Tue, 30 Mar 2004 16:24:12 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Variable substitution in variable name
Message-Id: <c4c013$2gu914$1@ID-184292.news.uni-berlin.de>

mshetty wrote:
> Have the following code where I want to reduce the lines of
> code:
> 
> #Variables to store total file count for each type
> $total_cpp_file=0;
> $total_h_file=0;

<snip>

Learn about scoping, and enable strictures.

>     if ($name =~ /\.(cpp)$/i) 
> 	{
> 	$total_cpp_file++;
>         push @files_cpp, ($name);
>     }

<snip>

> IS there a shorter way to write this?

Absolutely.

The counters should better be hash elements.

     my %total;

A hash for storing the file names would also be suitable:

     my %files;

Instead of all the if-elsif-else statements, you could do something like:

     if ($name =~ /\.(\w+)$/) {
         $total{$1}++;
         push @{ $files{$1} }, $name;
     }

That would make %files a hash of arrays.

HTH

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



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

Date: Tue, 30 Mar 2004 09:36:19 -0500
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: Variable substitution in variable name
Message-Id: <20040330092914.E19862@dishwasher.cs.rpi.edu>

On Tue, 30 Mar 2004, mshetty wrote:

>
> Hi,
>
> Have the following code where I want to reduce the lines of code:
>
> #Variables to store total file count for each type
> $total_cpp_file=0;
> $total_h_file=0;
> $total_c_file=0;
> $total_inp_file=0;
> $total_inl_file=0;
> $total_hpp_file=0;
> $total_cc_file=0;
> $total_hh_file=0;
> $total_yxx_file=0;
> $total_y_file=0;
> $total_l_file=0;
>
> find (\&wanted, "S:\\src");
> find (\&wanted, "S:\\inc");
> sub wanted
> {
> 		store_file ($File::Find::name);
> }
>
> sub store_file ($)
> {
>     my $name = shift;
>     if ($name =~ /\.(cpp)$/i)
> 	{
> 	$total_cpp_file++;
>         push @files_cpp, ($name);
>     }

<etc>

> IS there a shorter way to write this?
>
> I mean can I store (cpp, h, c, inp, inl, hpp, cc, hh, yxx, y, l) in an
> array(file_type and say push @files_$file_type[1]?
>
> Thanks and Regards,
> M Shetty
>


Use a hash of arrays.

my %files;

sub store_file {
	my $name = shift;
	if ($name =~ /\.(.*)$/i)){
		push @{$files{$1}}, $name;
	} else {
		warn "Unknown file type: $name\n";
	}
}

sub print_files {
	foreach (keys %files){
		local $, = ", ";
		print "There are " . @{$files{$_}} . " $_ files:\n";
		print @{$files{$_}};
	}
}

HTH,
Paul Lalli


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

Date: Tue, 30 Mar 2004 10:04:51 -0600
From: Web Surfer <raisin@delete-this-trash.mts.net>
Subject: Re: Variable substitution in variable name
Message-Id: <MPG.1ad3569b1795400c9897f4@news.mts.net>

[This followup was posted to comp.lang.perl.misc]

In article <bfbb8fd4.0403300558.6f63967c@posting.google.com>, 
mshetty@mail.com says...
> Hi,
> 
> Have the following code where I want to reduce the lines of code:
> 
> #Variables to store total file count for each type
> $total_cpp_file=0;
> $total_h_file=0;
> $total_c_file=0;
> $total_inp_file=0;
> $total_inl_file=0;
> $total_hpp_file=0;
> $total_cc_file=0;
> $total_hh_file=0;
> $total_yxx_file=0;
> $total_y_file=0;
> $total_l_file=0;
> 
> find (\&wanted, "S:\\src");
> find (\&wanted, "S:\\inc");
> sub wanted 
> {
> 		store_file ($File::Find::name);
> }
> 
> sub store_file ($)
> {
>     my $name = shift;
>     if ($name =~ /\.(cpp)$/i) 
> 	{
> 	$total_cpp_file++;
>         push @files_cpp, ($name);
>     }
>     elsif ($name =~ /\.(h)$/i) 
> 	{
> 	$total_h_file++;   
> 	push @files_h, ($name);
>     }
>     elsif ($name =~ /\.(c)$/i) 
> 	{
> 	$total_c_file++; 	
> 	push @files_c, ($name);
>     }
> 	elsif ($name =~ /\.(inp)$/i) 
> 	{
> 	$total_inp_file++; 	
> 	push @files_INP, ($name);
>     }
> 	elsif ($name =~ /\.(inl)$/i) 
> 	{
> 	$total_inl_file++; 	


You could use a hash.

my %totals = ( "cpp" => 0 , "h" => 0 , "c" => 0 );
# of course you will need to code the entries for ALL possible filename 
# extensions

To increment an element :

    $totals{"cpp"} += 1;


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

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 V10 Issue 6339
***************************************


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