ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Well, AFAIK, there's no regular expression method for this, although you might use regular expressions as part of a solution. I'd get an array with a list of the numbers (no dots), sort it in numerical order, and then count up the list... with a few well places ifs and a variable to store the last value you should be able to make something which creates these range strings.
Well, AFAIK, there's no regular expression method for this, although you might use regular expressions as part of a solution. I'd get an array with a list of the numbers (no dots), sort it in numerical order, and then count up the list... with a few well places ifs and a variable to store the last value you should be able to make something which creates these range strings.
The regex part is the piece I left out, as these are the third octets in IP addresses.
I did write a sloc code counter in Tcl/Tk a few years back... I think I will adapt a similar approach to this task as what I had done before. I just need to get familar with Perl... quickly.
I don’t understand the question. How are you choosing the values you want? Are those “ranges” or literal strings? It is hard to tell since you don’t supply any code.
If you want ranges, you can use array slices in anonymous arrays. For example, if I have:
There's probably a cleaner way to do this, but here's one solution
Code:
@a=qw/.1. .3. .5. .6. .7. .8. .11. .12. .14. /;
#filter out the '.' at the start and end
@clean = map {s/^\.//;s/\.$//;$_} @a;
#this makes it loop one extra time (so it does the last set correctly)
$#clean = $#clean+1;
#set initial values
$start = shift @clean;
$end = $start;
for $i (@clean){
if ($i==$last+1){
$end = $i;
}
else {
if ($start==$end){
$out = ".$start.";
} else {
$out = ".$start-$end.";
}
push @out, $out;
$start = $i;
$end = $i;
}
$last = $i;
}
print "$_\n" for @out;
output:
Code:
$ perl test.pl
.1.
.3.
.5-8.
.11-12.
.14.
I assume this is what you want? Not much of a regex question (aside from stripping the leading/trailing '.')
EDIT: I can do this a lot easier in python using groupby(), if perl has an equivalent function it'd make your life a lot easier.
Last edited by angrybanana; 04-11-2008 at 05:48 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.