Home > Archive > PERL Beginners > June 2005 > problem with assigning a value
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
problem with assigning a value
|
|
| Vladimir Lemberg 2005-06-08, 3:57 am |
| Hi,
I have a problem with assigning value to string. I'll be really grateful if someone will help me.
use strict;
use warnings;
use Cwd;
use Term::ANSIColor qw(:constants);
$Term::ANSIColor::AUTORESET = 1;
####code fragment####
foreach (@dirs){
chdir $_;
my $curdir = cwd();
my $vsn = glob ("*.vsn");
my $ver = glob ("*.ver");
print BLUE "\n[$_]\n";
opendir(DIR, "$curdir") or die RED "Unable to open $curdir: $!";
while (defined (my $file = readdir DIR)) {
next if -d "$curdir/$file" or $file eq "zone.cfg" or $file eq $vsn or $file eq $ver;
#compressing
system($program, $ARGV[0], $ARGV[1], $file) if $ARGV[0] eq "-c";
#decompressing
system($program, $ARGV[0], $file) if $ARGV[0] eq "-d";
}
closedir(DIR);
chdir $cwd;
}
The problem is that on each even iteration of loop, strings my $vsn and $ver cannot be initialized.
Thanks in advance,
Vladimir
| |
| mgoland@optonline.net 2005-06-08, 3:57 am |
|
----- Original Message -----
From: Vladimir Lemberg <lemba@sbcglobal.net>
Date: Tuesday, June 7, 2005 5:13 pm
Subject: problem with assigning a value
> Hi,
Hello
>
> I have a problem with assigning value to string. I'll be really
> grateful if someone will help me.
>
> use strict;
> use warnings;
> use Cwd;
> use Term::ANSIColor qw(:constants);
> $Term::ANSIColor::AUTORESET = 1;
>
> ####code fragment####
>
> foreach (@dirs){
> chdir $_;
> my $curdir = cwd();
> my $vsn = glob ("*.vsn");
> my $ver = glob ("*.ver");
>
> print BLUE "\n[$_]\n";
>
> opendir(DIR, "$curdir") or die RED "Unable to open $curdir: $!";
> while (defined (my $file = readdir DIR)) {
>
> next if -d "$curdir/$file" or $file eq "zone.cfg" or $file eq
> $vsn or $file eq $ver;
>
> #compressing
> system($program, $ARGV[0], $ARGV[1], $file) if $ARGV[0] eq "-c";
> #decompressing
> system($program, $ARGV[0], $file) if $ARGV[0] eq "-d";
> }
>
> closedir(DIR);
> chdir $cwd;
> }
>
> The problem is that on each even iteration of loop, strings my
> $vsn and $ver cannot be initialized.
That is because you have already defined these variables outside of your loop. you can simpley say $vsn = "Fooooo" in your loop. By the way my
$vsn = glob ("*.vsn"); may not due what you expact of it.
HTH,
mark G.
>
> Thanks in advance,
> Vladimir
>
>
>
>
>
>
>
>
| |
| Vladimir Lemberg 2005-06-08, 3:57 am |
| Hi Mark,
Yes, I defined them outside of the while loop but it should not be a problem
because I assigned them after changing directory.
Here some debugging prints:
I have for example 3 directories: a, b, c. Each one has .vsn file: a.vsn,
b.vsn, c.vsn,
on first iteration
cwd = /home/vladimir/scripts/test/a
$vsn = a.vsn woks ok
on second iteration
cwd = /home/vladimir/scripts/test/b
$vsn = uninitialized not working
on second iteration
cwd = /home/vladimir/scripts/test/c
$vsn = c.ver works ok
If I'll define them inside while loop, it wont work either.
Vladimir
----- Original Message -----
From: <mgoland@optonline.net>
To: "Vladimir Lemberg" <lemba@sbcglobal.net>
Cc: "Perl Beginners" <beginners@perl.org>
Sent: Tuesday, June 07, 2005 2:23 PM
Subject: Re: problem with assigning a value
>
>
> ----- Original Message -----
> From: Vladimir Lemberg <lemba@sbcglobal.net>
> Date: Tuesday, June 7, 2005 5:13 pm
> Subject: problem with assigning a value
>
> Hello
> That is because you have already defined these variables outside of your
> loop. you can simpley say $vsn = "Fooooo" in your loop. By the way my
> $vsn = glob ("*.vsn"); may not due what you expact of it.
> HTH,
> mark G.
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>
| |
| Wagner, David --- Senior Programmer Analyst --- WG 2005-06-08, 3:57 am |
| Vladimir Lemberg wrote:
> Hi Mark,
>=20
> Yes, I defined them outside of the while loop but it should not be a
> problem because I assigned them after changing directory.
>=20
> Here some debugging prints:
>=20
> I have for example 3 directories: a, b, c. Each one has .vsn file:
> a.vsn, b.vsn, c.vsn,
>=20
> on first iteration
> cwd =3D /home/vladimir/scripts/test/a
> $vsn =3D a.vsn woks ok
>=20
>=20
> on second iteration
> cwd =3D /home/vladimir/scripts/test/b
> $vsn =3D uninitialized not working
>=20
>=20
> on second iteration
> cwd =3D /home/vladimir/scripts/test/c
> $vsn =3D c.ver works ok
>=20
>=20
> If I'll define them inside while loop, it wont work either.
>=20
> Vladimir
I tried a couple of things and I don't believe that glob is working as you=
would expect it to work. If you do not want the vsn or ver, then I would =
do a simple regex like ( $file =3D~ /.+\.(ver|vsn)/ ) If the file name port=
ion needs to be ralted the directory you are working out, then it becomes a=
little harder, but I use glob very infrequently and when I have I have onl=
y used the <glob> against a directory that I want to work specific files ot=
herwise a regex doing what I need to check is the way.
Wags ;)[color=darkred]
>=20
> ----- Original Message -----
> From: <mgoland@optonline.net>
> To: "Vladimir Lemberg" <lemba@sbcglobal.net>
> Cc: "Perl Beginners" <beginners@perl.org>
> Sent: Tuesday, June 07, 2005 2:23 PM
> Subject: Re: problem with assigning a value
>=20
>=20
****************************************
***************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
****************************************
***************
| |
| Vladimir Lemberg 2005-06-08, 3:57 am |
| Thanks David!
Works perfect with regex. -)
----- Original Message -----
From: "Wagner, David --- Senior Programmer Analyst --- WGO"
<David.Wagner@freight.fedex.com>
To: "Vladimir Lemberg" <lemba@sbcglobal.net>; <mgoland@optonline.net>
Cc: "Perl Beginners" <beginners@perl.org>
Sent: Tuesday, June 07, 2005 4:57 PM
Subject: RE: problem with assigning a value
Vladimir Lemberg wrote:
> Hi Mark,
>
> Yes, I defined them outside of the while loop but it should not be a
> problem because I assigned them after changing directory.
>
> Here some debugging prints:
>
> I have for example 3 directories: a, b, c. Each one has .vsn file:
> a.vsn, b.vsn, c.vsn,
>
> on first iteration
> cwd = /home/vladimir/scripts/test/a
> $vsn = a.vsn woks ok
>
>
> on second iteration
> cwd = /home/vladimir/scripts/test/b
> $vsn = uninitialized not working
>
>
> on second iteration
> cwd = /home/vladimir/scripts/test/c
> $vsn = c.ver works ok
>
>
> If I'll define them inside while loop, it wont work either.
>
> Vladimir
I tried a couple of things and I don't believe that glob is working as you
would expect it to work. If you do not want the vsn or ver, then I would do
a simple regex like ( $file =~ /.+\.(ver|vsn)/ ) If the file name portion
needs to be ralted the directory you are working out, then it becomes a
little harder, but I use glob very infrequently and when I have I have only
used the <glob> against a directory that I want to work specific files
otherwise a regex doing what I need to check is the way.
Wags ;)[color=darkred]
>
> ----- Original Message -----
> From: <mgoland@optonline.net>
> To: "Vladimir Lemberg" <lemba@sbcglobal.net>
> Cc: "Perl Beginners" <beginners@perl.org>
> Sent: Tuesday, June 07, 2005 2:23 PM
> Subject: Re: problem with assigning a value
>
>
****************************************
***************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
****************************************
***************
| |
| FreeFall 2005-06-08, 3:57 am |
| perldoc -f glob maybe will help:
On Tue, 7 Jun 2005 14:13:29 -0700
"Vladimir Lemberg" <lemba@sbcglobal.net> wrote:
> glob ("*.vsn")
--
Whatever you do will be insignificant,but
the important is you do it!
It doesn't matter who you are, it's what
you do that takes you far!
| |
| John W. Krahn 2005-06-08, 3:57 am |
| Vladimir Lemberg wrote:
> Hi,
Hello,
> I have a problem with assigning value to string. I'll be really grateful if someone will help me.
>
> use strict;
> use warnings;
> use Cwd;
> use Term::ANSIColor qw(:constants);
> $Term::ANSIColor::AUTORESET = 1;
>
> ####code fragment####
>
> foreach (@dirs){
> chdir $_;
> my $curdir = cwd();
> my $vsn = glob ("*.vsn");
> my $ver = glob ("*.ver");
>
> print BLUE "\n[$_]\n";
>
> opendir(DIR, "$curdir") or die RED "Unable to open $curdir: $!";
> while (defined (my $file = readdir DIR)) {
>
> next if -d "$curdir/$file" or $file eq "zone.cfg" or $file eq $vsn or $file eq $ver;
>
> #compressing
> system($program, $ARGV[0], $ARGV[1], $file) if $ARGV[0] eq "-c";
> #decompressing
> system($program, $ARGV[0], $file) if $ARGV[0] eq "-d";
> }
>
> closedir(DIR);
> chdir $cwd;
> }
>
> The problem is that on each even iteration of loop, strings my $vsn and $ver cannot be initialized.
perldoc perlop
[snip]
A (file)glob evaluates its (embedded) argument only when it is starting
a new list. All values must be read before it will start over. In
list context, this isn't important because you automatically get them
all anyway. However, in scalar context the operator returns the next
value each time it's called, or "undef" when the list has run out. As
with filehandle reads, an automatic "defined" is generated when the
glob occurs in the test part of a "while", because legal glob returns
(e.g. a file called 0) would otherwise terminate the loop. Again,
"undef" is returned only once. So if you're expecting a single value
from a glob, it is much better to say
($file) = <blurch*>;
than
$file = <blurch*>;
because the latter will alternate between returning a filename and
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^^
returning false.
^^^^^^^^^^^^^^^
John
--
use Perl;
program
fulfillment
|
|
|
|
|