Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

split function 3

Status
Not open for further replies.

leo2180

IS-IT--Management
Jun 7, 2000
52
US
I cant seem to get the split right.&nbsp;&nbsp;The file that it finds looks like this xx11_xxxxx.xxx, and there's more than one of those with the same format.&nbsp;&nbsp;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.&nbsp;&nbsp;Here's the code, now where am I going wrong?&nbsp;&nbsp;I know its gotta be within the match of the split.&nbsp;&nbsp;I tried everything but nothing seems to work..&nbsp;&nbsp;Someone please help me out...<br><br>opendir (DIR,&quot;\Program files&quot;);<br>@files = readdir(DIR);<br>foreach $file(@files) {<br> if ($file =~ /[^.*](\d+)_.*/i) {<br> $num = $1;<br> @nums = split (//, $num);<br> foreach $val (@nums) {<br> print &quot;$val\n&quot;; <br> }<br> }<br>closedir (DIR);<br> }<br>
 
#!/usr/local/bin/perl<br>$str = 'xx11_xxxxx.xxx';<br>if ($str =~ /..(\d+)_.*/i) <br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;$num = $1;<br>&nbsp;&nbsp;&nbsp;&nbsp;while ($num =~ /\d/g) { print $&; }<br>&nbsp;&nbsp;&nbsp;&nbsp;} <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
ok, <br><br>This is what i want to do.&nbsp;&nbsp;I want to look inside a folder find this file thats going to have a number in it.&nbsp;&nbsp;For example day10_msg.txt, day11_msg.txt, day12_msg.txt.&nbsp;&nbsp;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.&nbsp;&nbsp;After getting that, I want to print it to the screen also with the number next to it, indicating what days file it is.&nbsp;&nbsp;With the code I have above, is that the right approach?
 
OK,<br>what OS are you using???<br>If you are on a UNIX variant, there are some OS utils to play with.<br>If not, then we write some Perl. <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
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.<br><br><FONT FACE=monospace><br># pattern is - day##_msg.txt<br>opendir (DIR,&quot;\Program files&quot;);<br>@files = readdir(DIR);<br>close DIR; # @files is built, so release the DIR handle<br><br>foreach $file(@files) <br>&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp; if ($file =~ /day(\d+)_.*/i) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# catch the number<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $num = $1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # if new num is largest yet, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # replace maxNum with current num<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # do this for all files and you will have<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # largest number in the files.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ($num &gt; $maxNum) { $maxNum = $num; } <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br>&nbsp;&nbsp;&nbsp;&nbsp; }<br><br># rebuild the file name you want <br>$fileToRead = 'day'.$maxNum.'_msg.txt';<br><br># open that file and print it to where ever you want<br></font><br><br>hope this helps.... <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top