in reply to Specific instance of a repeated string

I was hoping what I was trying to accomplish would be clear without having to post all the code but apparently it's not. Here is the full code for my script, which is a rewrite of the script in this node to create a sequential list of files.

#!/usr/bin/perl require 5.8.0; use strict; use warnings; # Load in the modules we'll be using use URI; use Pod::Usage; use Getopt::Long; use File::Basename; my $switches = process_switches(); # User has goofed so be concise pod2usage( -verbose => 0) unless (@ARGV >= 1 and @ARGV <= 3); # User has asked for help so be helpful if ($switches->{help}) { pod2usage( -verbose => 1, -message => "More help: cherry.pl --man\n", -output => \*STDERR); } # User has asked for more help so be more helpful if ($switches->{man}) { pod2usage( -verbose => 2 -output => \*STDERR); } # This is our input URI my $uri = shift; # Split the URI into a host / path and filename my ($filepath, $filename) = get_file_path_name($uri); my $filesplit = get_split($filename, $switches); my ($min, $max) = set_limits(@ARGV, $filesplit->{digit}); # Use default precision if none was specified unless ($switches->{precision}) { $switches->{precision} = length($filesplit->{digit}); warn "Autodetected precision as $switches->{precision} digits.\n" if + ($switches->{verbose}); } if ($switches->{verbose}) { if ($switches->{reverse}) { warn "Generating sequence from $max to $min.\n" if ($switches->{ve +rbose}); } else { warn "Generating sequence from $min to $max.\n" if ($switches->{ve +rbose}); } } # Print out the sequential list... for (my $i = $min; $i <= $max; $i++) { # Print the path and filename prefix print $filepath.$filesplit->{prefix}; unless ($switches->{reverse}) { # ...in ascending order print (sprintf "%0$switches->{precision}d", $i); } else { # ...or in descending order print (sprintf "%0$switches->{precision}d", $max - $i + 1); } # Print the filename suffix print $filesplit->{suffix}."\n"; } sub process_switches { my %switches; # Call Getopt::Long to handle any switches that might be present GetOptions ( 'help|h|?' => \$switches{help}, 'numeric-index|n:i' => \$switches{numindex}, 'precision|p:i' => \$switches{precision}, 'reverse|r' => \$switches{reverse}, 'verbose|v' => \$switches{verbose}, ); $switches{numindex} ||= -1; return \%switches; } sub get_file_path_name { # Get the URI that's been passed to the function my $input_uri = shift; # Create a new URI object based on it my $uri = URI->new($input_uri); # Extract the filename from the URI my ($filename) = fileparse($uri->path); # Slice the filename off a copy of the URI to get the full server / +path my $filepath = $uri; $filepath =~ s/$filename//; return ($filepath, $filename); } sub set_limits { my $warn_flag = 1 if (@_ == 3); my $max = shift if (@_); my $min = shift if (@_); $min ||= 1; # Swap max and min if they're wrong if ($min > $max) { ($min, $max) = ($max, $min); warn "Waring: Given min is larger than max. Swapping values.\n" if + ($switches->{verbose}); } $min += 0; $max += 0; return ($min, $max); } sub get_split { my $filename = shift; my $switches = shift; my %filesplit; my @digits = $filename =~ /(\d+)/g or die "Error: Could not extract +a number from filename '$filename'.\n"; $filesplit{digit} = $digits[$switches->{numindex}]; ### This won't work for files like 01-file01.html ($filesplit{prefix}, $filesplit{suffix}) = split (/$filesplit{digit} +/, $filename, 2); ### This won't work for files like 01-file01.html return \%filesplit; }

--
Grant me the wisdom to shut my mouth when I don't know what I'm talking about.

Replies are listed 'Best First'.
Re: Re: Specific instance of a repeated string
by shenme (Priest) on Aug 10, 2003 at 08:50 UTC
    Wow, it's 03:45, so I'm going to have to tickle the back of my throat and spew what I've got so far.   Besides the comments and debug stuff there's really only 8-10 lines of real code in the loop that does the good stuff.   Anyway, pardon the mess, it's an inspiration still steaming from the source...

    The idea I had was getting an RE to create an array of string pieces, alternating number and non-number chunks.   If you could then figure out the array index of the requested number chunk you'd operate on that.   And then collapse the pieces back together into the updated filename string when needed.

    This is a lot just to show an idea, but you might've not seen an RE do something like this before ...
    my @a = $filename =~ m/ (\D+)? (\d+)? /xg;

      Some excellent inspiration. Thank you.

      --
      Grant me the wisdom to shut my mouth when I don't know what I'm talking about.

Re: Re: Specific instance of a repeated string
by hangmanto (Monk) on Aug 10, 2003 at 11:55 UTC
    Instead of using a regex followed by a split, try using just a regex.
    if ( $filename =~ /(.*)(\d+)\.(.*)$/ ) { $prefix = $1; $digit = $2; $suffix = $3; } else { print "Error etc\n"; }
    You may need to tweak the regex. I am unable to test it from my current location.

      Unfortunately this won't do what I need it to do. This will only work for files that look like foo01.bar. Some of my files look like: foo10bar.baz. This regex also assumes that it's the last number in the filename that I want to operate on which isn't always the case.

      Thanks for the suggestion though, it is appreciated.

      --
      Grant me the wisdom to shut my mouth when I don't know what I'm talking about.