[lug] Perl regex question

LLC kevin at kevinkempterllc.com
Wed Sep 20 14:04:25 MDT 2006


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?

- Baffled in Boulder


_______________________________________________
Web Page: http://lug.boulder.co.us
Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
Join us on IRC: lug.boulder.co.us port=6667 channel=#colug






More information about the LUG mailing list