Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: How to take only maximum directory name by using its digits using perl?

by finddata (Sexton)
on Mar 29, 2017 at 08:44 UTC ( #1186341=note: print w/replies, xml ) Need Help??


in reply to Re: How to take only maximum directory name by using its digits using perl?
in thread How to take only maximum directory name by using its digits using perl?

The thing which you mentioned is right one.I should skip the underscore from the rev.And should include with + inside grouping. Please look the below code.Hence i am not getting the things.Did i made any mistakes with my code.Let me know to check it?
my $location = $output_dir; print $location; open LOGFILE, $location; my $first_line = 1; print $first_line; my $max_id; while (<LOGFILE>) { if (/rev(\d+)/) { if ($first_line) { $first_line = 0; print $first_line; $max_id = $1; print $max_id; } else { $max_id = $1 if ($1 > $max_id); print $max_id; } } } close LOGFILE;
  • Comment on Re^2: How to take only maximum directory name by using its digits using perl?
  • Download Code

Replies are listed 'Best First'.
Re^3: How to take only maximum directory name by using its digits using perl?
by hippo (Chancellor) on Mar 29, 2017 at 09:29 UTC
    Did i made any mistakes with my code.

    This isn't an SSCCE. You have neither declared nor defined $output_dir and therefore $location is undefined so LOGFILE cannot be opened and your while (<LOGFILE>) condition will never be true.

    Let me know to check it?

    Item 1 of the Basic debugging checklist is to use the stricture pragmas. This would have highlighted the problem to you.

Re^3: How to take only maximum directory name by using its digits using perl?
by haukex (Chancellor) on Mar 29, 2017 at 09:59 UTC
    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;
Re^3: How to take only maximum directory name by using its digits using perl?
by poj (Abbot) on Mar 29, 2017 at 16:12 UTC

    Ok, you've corrected 2 problems with the regex. Now try this SSCCE and see if you can find another error. Remember you said 'No need to consider about the files inside those folders.'

    #!/usr/bin/perl use strict; my $max; while (<DATA>) { if (/rev(\d+)/) { if ($1 > $max){ $max = $1; } } } print "max rev = $max\n"; __DATA__ `-- added |-- add.txt `-- added1 |--action | |-- action.txt | `-- rev1 | | `-- rev9999.html | `-- rev2 | `-- rev1.html `-- add.html
    poj

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1186341]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2019-12-09 02:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?