On 8/19/2011 1:01 PM, Rothenmaier, Deane C. wrote: > Greetings, O Wise Ones… > > I’m trying to understand the behavior I’m getting from this code: ... > The red text illustrates the error (if such it be?). My SWAG is that the > “nothing” between Perl’s ‘^’ anchor and the first space in $string2 is what’s > causing the problem. Can someone confirm this? Or provide me with the correct > answer if I’m wrong?
perlfunc under split: As a special case, specifying a PATTERN of space (' ') will split on white space just as "split" with no arguments does. Thus, "split(' ')" can be used to emulate awk's default behavior, whereas "split(/ /)" will give you as many null initial fields as there are leading spaces. A "split" on "/\s+/" is like a "split(' ')" except that any leading whitespace produces a null first field. A "split" with no arguments really does a "split(' ', $_)" internally. # My version: use strict; use warnings; my $string1 = 'Windows XP Professional Service Pack 2 (build 2600) Hewlett-Packard'; my @ary1 = split ' ', $string1; my ($win_type1, $win_ver1, $svc_pack1, $win_build1); $win_type1 = $ary1[1]; $win_ver1 = $ary1[2]; if ($win_type1 =~ /XP/) { $svc_pack1 = $ary1[5]; ($win_build1 = $ary1[7]) =~ s/\)//; } print "Current Method...\n"; print "Type: '$win_type1', Version: "$win_ver1',\n"; print "Service Pack: '$svc_pack1', Build: '$win_build1'\n\n"; if ($string1 =~ /XP/) { my ($win_type2, $win_ver2, $svc_pack2, $win_build2) = (split /\s+/, $string1)[1, 2, 5, 7]; $win_build2 =~ s/\)//; print "Proposed Method...\n"; print "Type: '$win_type2', Version: '$win_ver2',\n"; print "Service Pack: '$svc_pack2', Build: '$win_build2'\n"; } @ary1 = split ' ', $string1; $win_type1 = $ary1[1]; $win_ver1 = $ary1[2]; if ($win_type1 =~ /XP/) { $svc_pack1 = $ary1[5]; ($win_build1 = $ary1[7]) =~ s{\)}{}; } print "\nFILE string:\n"; print "Current Method...\n"; print "Type: '$win_type1', Version: '$win_ver1',\n"; print "Service Pack: '$svc_pack1', Build: '$win_build1'\n\n"; my $string2 = " Windows XP Professional Service Pack 2 (build 2600) Hewlett-Packard "; if ($string2 =~ /XP/) { $string2 =~ s/^\s*//; # I would either use ' ' split or add this line my ($win_type2, $win_ver2, $svc_pack2, $win_build2) = (split /\s+/, $string2)[1, 2, 5, 7]; $win_build2 =~ s/\)//; print "Proposed Method...\n"; print "Type: '$win_type2', Version: '$win_ver2',\n"; print "Service Pack: '$svc_pack2', Build: '$win_build2'\n"; } __END__ Current Method... Type: 'XP', Version: 'Professional', Service Pack: '2', Build: '2600' Proposed Method... Type: 'XP', Version: 'Professional', Service Pack: '2', Build: '2600' FILE string: Current Method... Type: 'XP', Version: 'Professional', Service Pack: '2', Build: '2600' Proposed Method... Type: 'XP', Version: 'Professional', Service Pack: '2', Build: '2600' _______________________________________________ ActivePerl mailing list ActivePerl@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs