LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-10-2008, 08:32 PM   #1
gliesian
LQ Newbie
 
Registered: Apr 2008
Posts: 18

Rep: Reputation: 0
Question One last Perl Regular Expression Question


Let's say the following is the contents of an Perl array:

.1.
.2.
.3.
.5.
.6.
.7.
.8.
.11.
.12.
.14.


I need it to rework the array to look like this:

.1-3.
.5-8.
.11-12.
.14.

Well, there is actually a whole lot more to what I need to do... but I'm starting with this.

Any help with reducing this algorithm to Perl code would be appreciated.

Last edited by gliesian; 04-10-2008 at 08:50 PM.
 
Old 04-10-2008, 09:09 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 04-10-2008, 09:21 PM   #3
gliesian
LQ Newbie
 
Registered: Apr 2008
Posts: 18

Original Poster
Rep: Reputation: 0
Smile Thanks for the help...

Quote:
Originally Posted by matthewg42 View Post
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.

Thanks for your help!
 
Old 04-11-2008, 05:21 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
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:
Code:
@a = ("1", "2", "3", "5", "6", "7", "8", "11", "12", "14");
I can have an array @b whose elements are references to the arrays containing the desired ranges like so:
Code:
@b = ([@a[0..2]], [@a[3..6]], [@a[7..8]], [@a[9]]);
 
Old 04-11-2008, 05:43 PM   #5
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl regular expression problem true_atlantis Programming 4 05-27-2009 06:35 AM
regular expression with perl MissEileen Programming 3 03-11-2008 01:26 PM
Perl Regular Expression abdul_zu Linux - Software 2 08-20-2007 03:57 AM
perl regular expression question (ARGHH!) amytys Programming 5 02-22-2005 03:59 AM
Perl Regular Expression rch Programming 14 07-11-2003 11:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration