in reply to Specific instance of a repeated string

What is switches supposed to contain beyond the particular group of numbers it should split upon? Anyhow, this will be messy:

#!/usr/bin/perl -wl use Data::Dumper 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}]; # how many $filesplit{digit} can we find before this sucker? my $splits = 2 + grep($_ eq $filesplit{digit},@digits[0..$switches-> +{numindex}-1]); my @temp = split (/$filesplit{digit}/, $filename, $splits); $filesplit{suffix} = pop(@temp); $filesplit{prefix} = join $filesplit{digit}, @temp; return \%filesplit; } print Dumper(get_split("01-file01.html",{numindex=>1})); print Dumper(get_split("01-file01and01.html",{numindex=>1})); print Dumper(get_split("02-file01tom34bill01.html",{numindex=>3})); __DATA__ $VAR1 = { 'digit' => '01', 'suffix' => '.html', 'prefix' => '01-file' }; $VAR1 = { 'digit' => '01', 'suffix' => 'and01.html', 'prefix' => '01-file' }; $VAR1 = { 'digit' => '01', 'suffix' => '.html', 'prefix' => '02-file01tom34bill' };

That is some ugly code. Hope this helps.

antirice    
The first rule of Perl club is - use Perl
The
ith rule of Perl club is - follow rule i - 1 for i > 1

Replies are listed 'Best First'.
Re^2: Specific instance of a repeated string
by Ionizor (Pilgrim) on Aug 09, 2003 at 22:47 UTC

    Switches contains all the command line switch settings:

    SWITCHES

    All switches are optional.

    The numeric-index switch should only be used when a filename has multiple numbers in it, e.g. 01-file01.html. This switch defaults to -1 which is the last number in the filename. Specifying the index as 1 will force the script to increment the first set of numbers. Specifying the index as 2 will force the script to increment the second set of numbers (which is redundant since the last set of numbers is the default anyway). Again, you get enough rope to hang yourself so don't use an index higher than the number of numbers in the file name.

    The precision switch controls how many zeros are prepended to 'short' numbers, i.e. should the first file be file1.html, file01.html, file001.html, etc. For default values, the script first looks at the precision of min if it's present, then max. If neither value is specified, the script defaults to the precision in the input URI, meaning if you use the filename file23.html you'll get two digits of precision whether you want them or not.

    The reverse switch simply prints out the list of URIs in order from max to min rather than from min to max.

    The verbose switch turns on some basic warnings such as the detected precision and whether or not the min and max values were swapped.

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