Re: Leading Spaces and split()
Ken Slater <[email protected]> Fri, 19 Aug 2011 21:48:53 -0400
| Newsgroups | gmane.comp.lang.perl.active-perl |
|---|---|
| Message-ID | <CA+20WcmHCj+10foERhHuY8PdaNtW0LW+0MB97WG_9MGxiP22kg@mail.gmail.com> |
On Fri, Aug 19, 2011 at 4:01 PM, Rothenmaier, Deane C. < [email protected]> wrote: > Greetings, O Wise Ones…**** > > ** ** > > I’m trying to understand the behavior I’m getting from this code:**** > > ** ** > > #!Perl**** > > > ################################################################################ > **** > > # PROGRAM: array_test1.pl**** > > > ################################################################################ > **** > > ** ** > > use strict;**** > > use warnings;**** > > ** ** > > my $string1 = "Windows XP Professional Service Pack 2 (build 2600) > Hewlett-Packard";**** > > my $string2 = " Windows XP Professional Service Pack 2 (build > 2600) Hewlett-Packard ";**** > > ** ** > > my @ary1;**** > > my @ary2;**** > > ** ** > > @ary1 = split( " ", $string1 );**** > > ** ** > > my ($win_type1, $win_ver1, $svc_pack1, $win_build1);**** > > ** ** > > $win_type1 = $ary1[1];**** > > $win_ver1 = $ary1[2];**** > > ** ** > > for ($win_type1) {**** > > /XP/ && do {**** > > $svc_pack1 = $ary1[5];**** > > ($win_build1 = $ary1[7]) =~ s{\)}{};**** > > last;**** > > };**** > > }**** > > ** ** > > print "Current Method...\n";**** > > print "Type: \"$win_type1\", Version: \"$win_ver1\",\n";**** > > print "Service Pack: \"$svc_pack1\", Build: \"$win_build1\"\n\n";**** > > ** ** > > my ($win_type2, $win_ver2, $svc_pack2, $win_build2);**** > > ** ** > > for ($string1) {**** > > /XP/ && do {**** > > ($win_type2, $win_ver2, $svc_pack2, $win_build2) = (split( /\s+/, > $string1 ))[1, 2, 5, 7];**** > > last;**** > > };**** > > }**** > > ** ** > > $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];**** > > ** ** > > for ($win_type1) {**** > > /XP/ && do {**** > > $svc_pack1 = $ary1[5];**** > > ($win_build1 = $ary1[7]) =~ s{\)}{};**** > > last;**** > > };**** > > }**** > > ** ** > > 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";**** > > ** ** > > for ($string2) {**** > > /XP/ && do {**** > > ($win_type2, $win_ver2, $svc_pack2, $win_build2) = (split( /\s+/, > $string2 ))[1, 2, 5, 7];**** > > last;**** > > };**** > > }**** > > ** ** > > $win_build2 =~ s{\)}{};**** > > ** ** > > print "Proposed Method...\n";**** > > print "Type: \"$win_type2\", Version: \"$win_ver2\",\n";**** > > print "Service Pack: \"$svc_pack2\", Build: \"$win_build2\"\n";**** > > ** ** > > ** ** > > > ################################################################################ > **** > > # PROGRAM OUTPUT: **** > > > ################################################################################ > **** > > ** ** > > 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: "Windows", Version: "XP",**** > > Service Pack: "Pack", Build: "(build"**** > > ** ** > > 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?**** > > ** ** > > Thanks!**** > > ** ** > > Deane Rothenmaier**** > > Programmer/Analyst – IT-StdCfg**** > > Walgreens Corp.**** > > 2 Overlook Point #N51022D**** > > MS 6515**** > > Lincolnshire, IL 60069**** > > 224-542-5150**** > > ** ** > > I will make sure that my doomsday device is up to code and properly > grounded. – Peter Anspach’s list of 100 things to do when one becomes an > Evil Overlord**** > > > Look at the documentation on the split function (perldoc -f split). Particularly the section that states: 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 initial null fields (empty string) as there are leading spaces. A "split" on "/\s+/" is like a "split(' ')" except that any leading whitespace produces a null first field. HTH, Ken _______________________________________________ ActivePerl mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs