Anonymous Monk,
Please forgive the poor error checking as this is intended only as an idea of how to accomplish this. It seems that you would want to make this a general (any string, any character to split on, any number of items) re-usable solution, so I offer the following:
#!/usr/bin/perl -w
use strict;
my $string = "foo.bar.foobar";
my @array = rev_split($string, '.', 2);
print "$_\n" foreach (@array);
sub rev_split {
return 0 unless (@_ > 1);
my ($string , $split , $quantity) = @_;
$string = reverse($string);
$split =~ /^(.)/;
$split = quotemeta $split;
my @things = $quantity ? split /$split/ , $string , $quantity : spl
+it /$split/ , $string;
$_ = reverse($_) foreach(@things);
return @things;
}
Cheers - L~R
Update: I misread and assumed the split should be done first, not the reverse and I have modified code to work as desired.
Update 2: I realized this isn't as a general solution as I thought since it will only work if the split criteria is a single character. I would be interested in someone coming up with an all purpose reverse split.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.