Hi,
Let us assume
my $string2 = " Windows XP Professional Service Pack 2 (build
2600) Hewlett-Packard ";
Then
split( /\s+/, $string2 )
returns the list starting from an empty element, as it matches /\s+/ in the
beginning of $string2.
You probably like to use
split( ' ', $string2 )
which is the special case for 'split'; it does the matching AFTER skipping the
leading spaces in $string2.
The returned list will start form 'Windows'.
HTH.
Cheers,
Stanislaw Romanski
----- Original Message -----
From: Rothenmaier, Deane C.
To: [email protected]
Sent: Friday, August 19, 2011 10:01 PM
Subject: Leading Spaces and split()
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
------------------------------------------------------------------------------
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs