473,394 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Opening up directories with a variable number appended to end

Hi everyone,
I am relatively new to perl. I am trying to write a script that can access directories that have variable numbers appended to the end. For example, I have 4 directories that all have an incremental number (1->4) and then an incremental number at the end (x->x+4) that is variable. Besides that the names are identical. For example:

this_is_identical_1_more_identical_15
this_is_identical_2_more_identical_16
this_is_identical_3_more_identical_17
this_is_identical_4_more_identical_18

The 1->4 part I put in the for loop. However, how do I access the directory with an unknown number at the end? I am running this on a unix server, so it might be easiest to use the 'tab' button to autocomplete if that is in any way possible. Any ideas on how to enter these directories?
Thanks!
Aug 1 '07 #1
8 1447
numberwhun
3,509 Expert Mod 2GB
What have you tried so far?

Regards,

Jeff
Aug 1 '07 #2
I tried doing

Expand|Select|Wrap|Line Numbers
  1. for ($sequence = 1; $sequence <= 4; $sequence++) {
  2.     chdir "SS_".$sequence."_MAX_*";
  3.     ..
  4.     ..
  5. }
  6.  
  7.  
  8. for ($sequence = 1; $sequence <= 4; $sequence++) {
  9.     system "cd SS_".$sequence."_MAX_*";
  10.     ..
  11.     ..
  12. }
  13.  
neither of which have worked. i am currently researching other not as straightforward solutions.
Aug 1 '07 #3
KevinADC
4,059 Expert 2GB
maybe:

Expand|Select|Wrap|Line Numbers
  1. for (1..4) {
  2.    $n = $_;
  3.    $r = $n+4;
  4.    my $dir = "SS_${n}_MAX_1$r";
  5.    print "$dir\n";
  6.    chdir ($dir);
  7. }       
  8.  
Aug 1 '07 #4
if i understand this correctly, this only works if the last numbers are 15 through 18. these numbers can be anything, but they will be sequential
Aug 1 '07 #5
numberwhun
3,509 Expert Mod 2GB
if i understand this correctly, this only works if the last numbers are 15 through 18. these numbers can be anything, but they will be sequential
I believe that Kevin actually coded it per your original statement:

For example, I have 4 directories that all have an incremental number (1->4) and then an incremental number at the end (x->x+4) that is variable

I think that so we can better assist you, that you should better describe the naming. Will the first number always be 1 through 4? Will it ever be other than? Will the last number be determined by a formula similar to the one you quoted? More information if you could please.

Regards,

Jeff
Aug 1 '07 #6
Sorry if it's not clear. The first number is always 1 through 4. That I can handle with the for loop.
The problem is the last number. It will always be four numbers in a row, but what those numbers are can vary. For example, in one case it can be 15, 16, 17, 18. But in another, it could be 8, 9, 10, 11. Is there any way to chop off a numerical value at the end and store it in a variable and then simply count up from that number?
Aug 1 '07 #7
miller
1,089 Expert 1GB
Use regular expressions:

Expand|Select|Wrap|Line Numbers
  1. my $basedir = "where ever your dirs are located";
  2.  
  3. open(BASE, $basedir) or die "Can't open $basedir: $!";
  4. my @dirs = grep {!/^\.+$/} readdir(BASE);
  5. closedir(BASE);
  6.  
  7. foreach my $dir (sort @dirs) {
  8.     my $path = "$basedir/$dir";
  9.  
  10.     next if ! -d $path; # Not a directory;
  11.     next if ! $dir =~ /^this_is_identical_\d_more_identical_\d+$/;
  12.  
  13.     # Processing here
  14. }
  15.  
- Miller
Aug 2 '07 #8
KevinADC
4,059 Expert 2GB
If you have no idea what those numbers on the end are be beforehand, or can't reasonably guess what they will be, you will have to use a regexp to grep through the directory and pull in the files. Maybe someone else will have a better suggestion.
Aug 2 '07 #9

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

Similar topics

3
by: Edward | last post by:
ASP.NET / VB.NET SQL Server 7.0 Our client has insisted that we change our established practice of building SQL in-line and move it all to SPROCs. Not a problem for 80% of the app. However,...
7
by: Fred H | last post by:
Hi I've tried to find out if and how I can open directories in standard C++, the same way I can open files. To my great suprise, I haven't managed to find out how this is done! I'm using MS...
1
by: Alfons | last post by:
Hello, I have build a program that can do file transferring between a Windows XP computer and a DOS computer via a serial port. The Windows program I have build in C++ with Visual Studio 6.0....
27
by: Mike P | last post by:
I will be passing my function a two dimensional array of varying length. Within that array is one data point, and the number of times it should loop through. So, for example, I might pass this...
10
by: Felix Kater | last post by:
Hi, is it possible to define a function in a way that when calling it I can insert as many arguments as I want? (I would neither like to define a function with 50 default arguments nor prepare a...
1
by: Sushil Prasad | last post by:
hi, I have a web application with a help link. On the click of the help link user see a html page. I do response.redirect to url in my code. The vb.net application that user was using gets...
5
by: k.vanderstarren | last post by:
Hi All, I'm having a problem that is driving me absolutely batty and I'm hoping that one of you JavaScript gurus can help me out before I go stark raving mad. I am using the function shown...
6
by: encoad | last post by:
Hi everyone, I've run into another road block that google.com isn't able to solve since I don't even know where to start when searching. In my webapp, a certain number of table rows containing...
3
by: simonharrison | last post by:
Hello everyone. Hopefully someone can point me in the right direction here. I'm wanting to write a script to open microsoft word and adobe pdf documents . Here is a little background: At the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.