×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

split function
3

split function

split function

(OP)
I cant seem to get the split right.  The file that it finds looks like this xx11_xxxxx.xxx, and there's more than one of those with the same format.  Now I want to get the digit (11)from that file of that format, and the other as well, and assign to it a variable.  Here's the code, now where am I going wrong?  I know its gotta be within the match of the split.  I tried everything but nothing seems to work..  Someone please help me out...

opendir (DIR,"\Program files");
@files = readdir(DIR);
foreach $file(@files) {
if ($file =~ /[^.*](\d+)_.*/i) {
$num = $1;
@nums = split (//, $num);
foreach $val (@nums) {
print "$val\n";
}
}
closedir (DIR);
}

RE: split function

2
#!/usr/local/bin/perl
$str = 'xx11_xxxxx.xxx';
if ($str =~ /..(\d+)_.*/i)
    {
    $num = $1;
    while ($num =~ /\d/g) { print $&; }
    }




keep the rudder amid ship and beware the odd typo

RE: split function

(OP)
ok,

This is what i want to do.  I want to look inside a folder find this file thats going to have a number in it.  For example day10_msg.txt, day11_msg.txt, day12_msg.txt.  Now I want to get the latest file from that folder with that format, the file with the highest number at the time will be the latest file.  After getting that, I want to print it to the screen also with the number next to it, indicating what days file it is.  With the code I have above, is that the right approach?

RE: split function

OK,
what OS are you using???
If you are on a UNIX variant, there are some OS utils to play with.
If not, then we write some Perl.




keep the rudder amid ship and beware the odd typo

RE: split function

(OP)
Im using WIn2000

RE: split function

the way my brain works, I get around to it this way.....until I think about it some more, at which point I might do it another way.


# pattern is - day##_msg.txt
opendir (DIR,"\Program files");
@files = readdir(DIR);
close DIR; # @files is built, so release the DIR handle

foreach $file(@files)
     {
     if ($file =~ /day(\d+)_.*/i)
         {
        # catch the number
         $num = $1;
         # if new num is largest yet,
         # replace maxNum with current num
         # do this for all files and you will have
         # largest number in the files.
         if ($num > $maxNum) { $maxNum = $num; }
         }
     }

# rebuild the file name you want
$fileToRead = 'day'.$maxNum.'_msg.txt';

# open that file and print it to where ever you want


hope this helps....




keep the rudder amid ship and beware the odd typo

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close