[lug] Perl regex question

Bill Thoen bthoen at gisnet.com
Wed Sep 20 14:25:16 MDT 2006


Thanks Kevin and Zan... My real problem is a bit more complicated than 
this simplified example, but your clue to use an array was just what I 
needed. With that bit of help, I've got it working!

LLC wrote:

>Something like this will work:
>
>while (<>) {
>   chomp;
>   my @row = split /,/;
>   for (my $i=1; $i<=$#row; $i++) {
>     print "$row[0]$row[$i] ";
>   }
>   print "\n";
>}
>
>
>Note: You might need to change the <=$#row to a simple <$#row if you get a
>dupe for the last entry, however I believe the above is correct.
>
>
>
>
>----- Original Message -----
>Subject: [lug] Perl regex question
>From: "Bill Thoen" <bthoen at gisnet.com>
>Date: Wed, September 20, 2006 13:16
>
>
>
>If I have strings like the following:
>A,2, 3, 5, 7
>F, 8, 9, 1
>
>and I want to turn them into stirngs like these:
>A2 A3 A5 A7
>F8 F9 F1
>
>using Perl regex, how is this done?  I'm trying something like the
>following:
>#!/usr/bin/perl
>while (<>) {
> chop;
> s/^[A-Z][, ]+(([0-9])[, ]+[^, ])+/$1$2 /g;
> print "$_\n";
>}
>
>But of course, it doesn't work. How does one process a list of items
>like this using Perl's regular expressions?
>  
>




More information about the LUG mailing list