Click here to get back home

Problem expanding filenames in loop

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Problem expanding filenames in loop Hemant 06-23-2008
Posted by Hemant on June 23, 2008, 2:43 pm
Please log in for more thread options
Folks,

When I try to expand file name in a loop, perl fails to expand
everyother file.

Example:

#!/usr/bin/perl
#

@FileListArr = (
"Full-2008-06-16-23:29:40",
"Incr-2008-06-17-22:02:18",
"Incr-2008-06-18-22:02:23",
"Incr-2008-06-19-22:02:20",
"Incr-2008-06-20-22:02:38",
"Incr-2008-06-21-22:05:30",
"Incr-2008-06-22-22:05:26",
);

foreach $Volume (@FileListArr)
{
$File=glob("/backups/backupfiles/*/$Volume");
if (!defined $File)
{
print "File not found for $Volume\n";
}
else
{
print "$File\n";
}
}


The output of the script:

/backups/backupfiles/lidp11/Full-2008-06-16-23:29:40
File not found for Incr-2008-06-17-22:02:18
/backups/backupfiles/lidp11/Incr-2008-06-18-22:02:23
File not found for Incr-2008-06-19-22:02:20
/backups/backupfiles/lidp11/Incr-2008-06-20-22:02:38
File not found for Incr-2008-06-21-22:05:30
/backups/backupfiles/lidp11/Incr-2008-06-22-22:05:26


I also tried using "<>" keyword, but got same result. I tried similar
test to expand user's HOME directory (tilde expansion) and for same
result.

I tested it under perl 5.8.4 on AIX and perl 5.10.0 on linux.

Posted by John W. Krahn on June 23, 2008, 3:01 pm
Please log in for more thread options
Hemant wrote:
> Folks,
>=20
> When I try to expand file name in a loop, perl fails to expand
> everyother file.
>=20
> Example:
>=20
> #!/usr/bin/perl
> #
>=20
> @FileListArr =3D (
> "Full-2008-06-16-23:29:40",
> "Incr-2008-06-17-22:02:18",
> "Incr-2008-06-18-22:02:23",
> "Incr-2008-06-19-22:02:20",
> "Incr-2008-06-20-22:02:38",
> "Incr-2008-06-21-22:05:30",
> "Incr-2008-06-22-22:05:26",
> );
>=20
> foreach $Volume (@FileListArr)
> {
> $File=3Dglob("/backups/backupfiles/*/$Volume");
> if (!defined $File)
> {
> print "File not found for $Volume\n";
> }
> else
> {
> print "$File\n";
> }
> }
>=20
>=20
> The output of the script:
>=20
> /backups/backupfiles/lidp11/Full-2008-06-16-23:29:40
> File not found for Incr-2008-06-17-22:02:18
> /backups/backupfiles/lidp11/Incr-2008-06-18-22:02:23
> File not found for Incr-2008-06-19-22:02:20
> /backups/backupfiles/lidp11/Incr-2008-06-20-22:02:38
> File not found for Incr-2008-06-21-22:05:30
> /backups/backupfiles/lidp11/Incr-2008-06-22-22:05:26
>=20
>=20
> I also tried using "<>" keyword, but got same result. I tried similar
> test to expand user's HOME directory (tilde expansion) and for same
> result.
>=20
> I tested it under perl 5.8.4 on AIX and perl 5.10.0 on linux.

From the perlop.pod man page:

perldoc perlop
[snip]
So if you=92re expecting a single value from a glob, it is much bett=
er
to say

($file) =3D <blurch*>;

than

$file =3D <blurch*>;

because the latter will alternate between returning a filename and
returning false.


So it's probably better to use something like this:

foreach my $File ( map glob("/backups/backupfiles/*/$_"), @FileListArr )
{
print "$File\n";
}



John
--=20
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Posted by xhoster on June 23, 2008, 3:09 pm
Please log in for more thread options
> Folks,
>
> When I try to expand file name in a loop, perl fails to expand
> everyother file.
>
> Example:
>
...
>
> foreach $Volume (@FileListArr)
> {
> $File=glob("/backups/backupfiles/*/$Volume");

call it in a list context:

($File)=glob("/backups/backupfiles/*/$Volume");

In a scalar context, glob iterates through the expansions, returning
undef when done. The fact that $Volume has changed in the mean time
doesn't matter. Glob doesn't look for such changes until after it is done
iterating on the expansion is is currently working over. In your case,
if the expansion always expands to exactly one file, then you will
get this every other effect.

1) Notice the argument. Return the file.
2) Fail to notice the argument change. Return undef, to indicate no more
files for the previous argument.
3) Since its starting from empty, Notice the argument again. Return that
file.

etc.

From perldoc perlop:

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.

In my experience, it is almost always a mistake to use glob in a scalar
context.

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.

Posted by Ben Morrow on June 23, 2008, 3:12 pm
Please log in for more thread options

>
> When I try to expand file name in a loop, perl fails to expand
> everyother file.
>
> Example:
>
> #!/usr/bin/perl
> #

use warnings;
use strict;

> @FileListArr = (

my @FileListArr = (

> "Full-2008-06-16-23:29:40",
> "Incr-2008-06-17-22:02:18",
> "Incr-2008-06-18-22:02:23",
> "Incr-2008-06-19-22:02:20",
> "Incr-2008-06-20-22:02:38",
> "Incr-2008-06-21-22:05:30",
> "Incr-2008-06-22-22:05:26",
> );

It is often more convenient to write a list like this as

my @FileListArr = qw(
Full-2008-06-16-23:29:40
Incr-2008-06-17-22:02:18
);

> foreach $Volume (@FileListArr)

foreach my $Volume (@FileListArr)

> {
> $File=glob("/backups/backupfiles/*/$Volume");

glob in scalar context doesn't do what you think. Once you've invoked it
once, it ignores its argument until it's returned all the possible
results and then undef. If you just want the first result, you need to
call it in list context like this:

my ($File) = glob(...);

where the parens around ($File) make this a list assignment (into a list
with only one value) rather than a scalar assignment.

Ben

--
Outside of a dog, a book is a man's best friend.
Inside of a dog, it's too dark to read.
ben@morrow.me.uk Groucho Marx

Posted by Hemant on June 23, 2008, 3:48 pm
Please log in for more thread options
>
>
>
> > When I try to expand file name in a loop, perl fails to expand
> > everyother file.
>
> > Example:
>
> > #!/usr/bin/perl
> > #
>
> use warnings;
> use strict;
>
> > @FileListArr = (
>
> my @FileListArr = (
>
> > "Full-2008-06-16-23:29:40",
> > "Incr-2008-06-17-22:02:18",
> > "Incr-2008-06-18-22:02:23",
> > "Incr-2008-06-19-22:02:20",
> > "Incr-2008-06-20-22:02:38",
> > "Incr-2008-06-21-22:05:30",
> > "Incr-2008-06-22-22:05:26",
> > );
>
> It is often more convenient to write a list like this as
>
> my @FileListArr = qw(
> Full-2008-06-16-23:29:40
> Incr-2008-06-17-22:02:18
> );
>
> > foreach $Volume (@FileListArr)
>
> foreach my $Volume (@FileListArr)
>
> > {
> > $File=glob("/backups/backupfiles/*/$Volume");
>
> glob in scalar context doesn't do what you think. Once you've invoked it
> once, it ignores its argument until it's returned all the possible
> results and then undef. If you just want the first result, you need to
> call it in list context like this:
>
> my ($File) = glob(...);
>
> where the parens around ($File) make this a list assignment (into a list
> with only one value) rather than a scalar assignment.
>
> Ben
>
> --
> Outside of a dog, a book is a man's best friend.
> Inside of a dog, it's too dark to read.
> b...@morrow.me.uk Groucho Marx

Thank you all for the quick replies.

Similar ThreadsPosted
Problem with glob and filenames containing '[' and ']' September 27, 2006, 7:33 am
Help on expanding variable from sh May 2, 2007, 3:36 pm
Expanding tree paths July 19, 2005, 10:48 am
Problem with loop control LAST exiting prematurely March 18, 2008, 6:22 am
How do you match 25 filenames? May 11, 2005, 7:51 am
using -e with array of filenames May 24, 2005, 3:19 pm
How to use a textfile for filenames?? February 12, 2006, 11:16 pm
utf8 filenames April 9, 2006, 8:39 pm
Can't change filenames..why? April 3, 2007, 8:39 pm
how to read Chinese filenames? June 5, 2006, 5:49 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap