I'm trying to do something that seems fairly simply but I cannot figure out for the life of me how to get what I need.

I have a variable called $scene, and I want to get an array of filenames that belong to the scene variable so that I can loop through them and "do stuff". Some files have an LE7 prefix, others LT5 and LT4. Additionally, there are several files that belong to a scene, each with a unique date. I'm very new to perl (as in just started today) and I can't seem to grasp the whole strings/escaping/variable deal.

Here's the first way I tried:

#!/usr/bin/perl -w

use autodie;
use strict;
use warnings;
use File::Glob ':glob';

my $inpath = "indata/gz";

open(LIST, "desktop/migrate_list.txt");

while (my $scene = <LIST>){
    chomp $scene;
    my @gzfiles=glob("$inpath/L??$scene*.gz");

    foreach my $gzfile (@gzfiles) {
        # do stuff....

close LIST;

I could go into detail about the different things I've tried and results I've gotten but basically my methods have been very crude and results a bunch of senseless crap (most importantly--the glob array always turns up empty).

Obviously, I'm missing how to incorporate wildcards and variables into a string/command call. I've tried a lot of things but I'm thinking/hoping someone knows a solution off the top of their head because I have no idea.

To be clear on what I'm looking for, here's an example. Say $scene = "011099". Then the array could include the following files:

LE70110992001365-SC28491244.tar.gz,     LT40110991990001-SC9793793477.tar.gz,
LT40110992010099-SC983698346923.tar.gz, LT50110992002365-SC6938693863.tar.gz,
LE70110991992001-SC936983693.tar.gz, ...

Any help is appreciated. Thanks!

  • 1
    thanks for responding! when I print @gzfiles, I get 0's for each scene in the loop but I know for a fact they exist in that path. Now that I think about it, perhaps perl doesn't respond well to using symlinks in the path? I'll use the explicit path and see if that makes any difference. brb... – user20408 May 8 '14 at 22:36
  • 1
    @TLP I got rid of the glob load and changed the symlink paths to explicit paths and got no change. globbing "$inpath/*gz" gives me all of the files in that directory, which leads me to think it has to do with the variable usage... – user20408 May 8 '14 at 22:40
  • 1
    If you have space in your $scene variable, globbing will not work. – TLP May 8 '14 at 22:41
  • 1
    Use this: use Data::Dumper; $Data::Dumper::Useqq = 1; chomp(my @scenes = <LIST>); print Dumper \@scenes; and see if you have the lines you think you have in the file. – TLP May 8 '14 at 22:44
  • 2
    @TLP THANK YOU!!!! the list was reading the scenes with a trailing whitespace for some reason, I guess. After stripping that off, it works. Thanks so much! – user20408 May 8 '14 at 22:48

Converting debugging in comments to answer:

Use this:

use Data::Dumper; 
$Data::Dumper::Useqq = 1; 
chomp(my @scenes = <LIST>); 
print Dumper \@scenes; 

And see if you have the lines you think you have in the file. It looks like your problem is that you have space in your file somehow, which would break the glob.

  • 1
    apparently i can't upvote because i don't have enough rep but thank you! – user20408 May 8 '14 at 22:56
  • No worries. You're welcome. – TLP May 8 '14 at 22:58
  • 3
    up voted for you :) +1 for step by step insight into the debugging process (ok, ok, it's a just a little script - but it works). Well played! – G. Cito May 15 '14 at 2:02

Your Answer

