Perl: Extract distinct patterns from a string

  • A+
Category:Languages

I would like to extract patterns according to a[a-z] from a long string and output only distinct values. For example for the following string in $x

perl -e "$x = 'abx1acy2acz3ab'"; 

The results should be:

ab
ac

Problem: There is no rule to split the record/string into an array which makes it easy to extract the pattern.

 


If you ever want unique data, consider using a hash.

#!/usr/bin/perl  use strict; use warnings; use feature 'say';  $_ = 'abx1acy2acz3ab'; my %seen;  for (/(a[a-z])/g) {   say $_ unless $seen{$_}++; } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: