Hence i am not getting the things.
What does this mean? I already explained it to you: If you ask good questions, like you've done a few times so far, we can give good answers. If you ask questions that can't be answered because you haven't provided enough information, not only will you not get good answers, you will lose support from more and more monks as you go on. I gave you three links to read, please do so, and always follow that advice!
Your code looks ok to me (Update: however, you should take stevieb's advice, as well as hippo's advice to Use strict and warnings) and when I set $output_dir to an input file name, it seems to work. However, only you know what all of your specifications are, and what all of your input looks like, so I can't tell you if your code will always work, at the moment only you can confirm that your code fully works. Testing is an important skill, and what I can do is show you how you might go about this. For example, you can test whether your code, properly modularized into a subroutine, works for various test inputs. Here, I'm using in-memory files (open) and Test::More. Note how I've left your logic entirely unchanged.
#!/usr/bin/env perl use warnings; use strict; sub scan_handle_for_rev { my $filehandle = shift; my $first_line = 1; my $max_id; while (<$filehandle>) { if (/rev(\d+)/) { if ($first_line) { $first_line = 0; $max_id = $1; } else { $max_id = $1 if ($1 > $max_id); } } } return $max_id; } use Test::More; { open my $fh, '<', \<<'END_TEST_INPUT' or die $!; | |-- action.txt | `-- rev2 | `-- rev1.html `-- add.html END_TEST_INPUT is scan_handle_for_rev($fh), 2; close $fh; } { open my $fh, '<', \<<'END_TEST_INPUT' or die $!; |--action | |-- action.txt | `-- rev1 | | `-- rev1.html | `-- rev2 | `-- rev1.html `-- add.html END_TEST_INPUT is scan_handle_for_rev($fh), 2; close $fh; } { open my $fh, '<', \<<'END_TEST_INPUT' or die $!; |--action | |-- action.txt | `-- rev1 | | `-- rev1.html | `-- rev13 | | `-- rev1.html | `-- rev2 | `-- rev1.html `-- add.html END_TEST_INPUT is scan_handle_for_rev($fh), 13; close $fh; } done_testing;
In reply to Re^3: How to take only maximum directory name by using its digits using perl?
by haukex
in thread How to take only maximum directory name by using its digits using perl?
by finddata
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |