in reply to Strings overwrite themselves in print command

Logic is much simpler if you process logical records rather than lines and extract only the data that you need.
use strict; use warnings; $/ = q(); while (my $record = <DATA>) { my @lines = split /\n/, $record; my $header = shift @lines; my ($grpNm) = $header =~ /\:\s*(\w+)/; foreach (@lines) { my ($memID) = unpack '@12A6'; print "$grpNm, $memID\n"; } } __DATA__ cn: applegroup member: CN=mem001,OU=Users,DC=myorg,DC=com member: CN=mem002,OU=Users,DC=myorg,DC=com member: CN=mem003,OU=Users,DC=myorg,DC=com member: CN=mem004,OU=Users,DC=myorg,DC=com cn: orangegroup member: CN=mem301,OU=Users,DC=myorg,DC=com member: CN=mem302,OU=Users,DC=myorg,DC=com member: CN=mem303,OU=Users,DC=myorg,DC=com

OUTPUT,

applegroup, mem001 applegroup, mem002 applegroup, mem003 applegroup, mem004 orangegroup, mem301 orangegroup, mem302 orangegroup, mem303
Bill

Replies are listed 'Best First'.
Re^2: Strings overwrite themselves in print command
by tdsny71 (Initiate) on Jun 28, 2016 at 05:54 UTC
    Bill,

    I like the simplicity in this approach. I haven't learned enough perl to come up with this myself so thanks for stretching my understanding. However, I am seeing an error when I apply this code:

    Not enough arguments for unpack at script.pl line 64, near "'@12A6';"

    Not certain what this means.

      It means your Perl is ancient and doesn't support the default second argument. Use
      my ($memID) = unpack '@12A6', $_;

      instead.

      Update: The change happened in 5.10 (2007), see Miscellaneous.

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,