469,160 Members | 2,075 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,160 developers. It's quick & easy.

Change to previous directory with glob

I have a script where I am using glob to get a list of folders that end in "*import", but I am trying to change my directory to the previous one.

Here is what I have:

@directories = glob("*import");
@directories = sort { $b <=> $a } @files;

I can't figure out how to make glob go back one directory. I am looking for something similiar to the Unix "cd .." command.

I have tried glob("../*import") and dozens of variations, but nothing works.

Thanks
Aug 26 '07 #1
4 1903
miller
1,089 Expert 1GB
How I would do it:

Expand|Select|Wrap|Line Numbers
  1. use File::Spec::Functions qw(updir);
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my @files = do {
  7.     local *PARENTDIR;
  8.     opendir(PARENTDIR, updir) or die "Can't open dir: $!";
  9.     grep {/import$/} readdir(PARENTDIR);
  10. };
  11.  
  12. print "@files";
  13.  
Alternatively, just use chdir.

- Miller
Aug 26 '07 #2
I am an idiot.

I was using this code (that I had used earlier):

@files = glob("*import");
@files = sort { $b <=> $a } @files;

when I decided to change the word "files" to directory - and so I did this:

@directory = glob("*import");
@directory = sort { $b <=> $a } @files;

As you can see, I did not change the third "@files" on the sort line. Once I changed it to "@directory", it worked.

I guess that I should quit programming at 2:00 a.m. - after that, I am not thinking clearly....

Thanks for your help.
Aug 26 '07 #3
numberwhun
3,503 Expert Mod 2GB
Nah, I don't think you should quit the 2am suicide programming session(s), in fact I tend to get some really good work done during those weeeee morning hours. Instead, I would crank up the ol' tea pot and start steeping some tea (or whatever your beverage of choice is). Although the only issues I have truly had during those times are when I wake up with drool on the keyboard and an imprint of the keys on my cheek. Yeah, then you know you were up too long!

Glad you found your issue!

Regards,

Jeff
Aug 26 '07 #4
miller
1,089 Expert 1GB
As you can see, I did not change the third "@files" on the sort line. Once I changed it to "@directory", it worked.
I don't know for sure, but I suspect that you are probably not including "use strict;" in your scripts. Or at least not this script. If you had, then once you changed the original my definition from "my @files" to "my @directory", all other instances of @files would no longer work and you would have been alerted to the error.

This is just a suspicion. But if it's true, then I strongly encourage you to change your script to include "use strict;" at the top. It would have saved you this headache, and will definitely save you future headaches.

- Miller
Aug 26 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Gonnasi | last post: by
15 posts views Thread by Jameson | last post: by
11 posts views Thread by comp.lang.php | last post: by
2 posts views Thread by Kevin Walzer | last post: by
9 posts views Thread by Wolfgang Draxinger | last post: by
1 post views Thread by jo3c | last post: by
10 posts views Thread by royG | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.