Leading Spaces and split()

Leading Spaces and split()

am 19.08.2011 22:01:18 von Deane.Rothenmaier

--===============1119999654==
Content-Type: multipart/alternative;
boundary="_000_115007D93766724DAE9F518846E121DE9399CFDMSS8MP 2MBwagreso_"
Content-Language: en-US

--_000_115007D93766724DAE9F518846E121DE9399CFDMSS8MP2MBwagre so_
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="us-ascii"

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 =3D "Windows XP Professional Service Pack 2 (build 2600) Hewlet=
t-Packard";
my $string2 =3D " Windows XP Professional Service Pack 2 (build =
2600) Hewlett-Packard ";

my @ary1;
my @ary2;

@ary1 =3D split( " ", $string1 );

my ($win_type1, $win_ver1, $svc_pack1, $win_build1);

$win_type1 =3D $ary1[1];
$win_ver1 =3D $ary1[2];

for ($win_type1) {
/XP/ && do {
$svc_pack1 =3D $ary1[5];
($win_build1 =3D $ary1[7]) =3D~ 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) =3D (split( /\s+/, $=
string1 ))[1, 2, 5, 7];
last;
};
}

$win_build2 =3D~ s{\)}{};

print "Proposed Method...\n";
print "Type: \"$win_type2\", Version: \"$win_ver2\",\n";
print "Service Pack: \"$svc_pack2\", Build: \"$win_build2\"\n";



@ary1 =3D split( " ", $string1 );

$win_type1 =3D $ary1[1];
$win_ver1 =3D $ary1[2];

for ($win_type1) {
/XP/ && do {
$svc_pack1 =3D $ary1[5];
($win_build1 =3D $ary1[7]) =3D~ 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) =3D (split( /\s+/, $=
string2 ))[1, 2, 5, 7];
last;
};
}

$win_build2 =3D~ 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 "n=
othing" 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 corr=
ect 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 grounde=
d. - Peter Anspach's list of 100 things to do when one becomes an Evil Over=
lord


--_000_115007D93766724DAE9F518846E121DE9399CFDMSS8MP2MBwagre so_
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset="us-ascii"

osoft-com:office:office" xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" xmlns=3D"http:=
//www.w3.org/TR/REC-html40">

>





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 =3D "Windows XP Professional Servic=
e Pack 2 (build 2600) Hewlett-Packard";


my $string2 =3D "     =
       Windows XP Professional Service Pack 2=
(build 2600) Hewlett-Packard ";


 


my @ary1;


my @ary2;


 


@ary1 =3D split( " ", $string1 ); p>


 


my ($win_type1, $win_ver1, $svc_pack1, $win_build1);=


 


$win_type1 =3D $ary1[1];


$win_ver1 =3D $ary1[2];


 


for ($win_type1) {


   /XP/ && do {


      $svc_pack1 =3D $ary1[=
5];


      ($win_build1 =3D $ary=
1[7]) =3D~ 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_ver=
2, $svc_pack2, $win_build2) =3D (split( /\s+/, $string1 ))[1, 2, 5, 7];=


      last;


   };


}


 


$win_build2 =3D~ s{\)}{};


 


print "Proposed Method...\n"; p>

print "Type: \"$win_type2\", Version:=
\"$win_ver2\",\n";


print "Service Pack: \"$svc_pack2\", =
Build: \"$win_build2\"\n";


 


 


 


@ary1 =3D split( " ", $string1 ); p>


 


$win_type1 =3D $ary1[1];


$win_ver1 =3D $ary1[2];


 


for ($win_type1) {


   /XP/ && do {


      $svc_pack1 =3D $ary1[=
5];


      ($win_build1 =3D $ary=
1[7]) =3D~ 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_ver=
2, $svc_pack2, $win_build2) =3D (split( /\s+/, $string2 ))[1, 2, 5, 7];=


      last;


   };


}


 


$win_build2 =3D~ s{\)}{};


 


print "Proposed Method...\n"; p>

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&qu=
ot;,


Service Pack: "2", Build: "2600"=


 


Proposed Method...


Type: "XP", Version: "Professional&qu=
ot;,


Service Pack: "2", Build: "2600"=


 


FILE string:


Current Method...


Type: "XP", Version: "Professional&qu=
ot;,


Service Pack: "2", Build: "2600"=


 


Proposed Method... :p>


Type: "Windows",=
Version: "XP",


Service Pack: "Pack&q=
uot;, Build: "(build"


 


The red text illustrates the error (if such it be?).=
My SWAG is that the “nothing” between Perl’s ‘^=
217; anchor and the first space in $string2 is what’s causing the pro=
blem. 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 co=
de and properly grounded. – Peter Anspach’s list of 100 things =
to do when one becomes an Evil Overlord


 






--_000_115007D93766724DAE9F518846E121DE9399CFDMSS8MP2MBwagre so_--


--===============1119999654==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============1119999654==--

Re: Leading Spaces and split()

am 20.08.2011 00:45:00 von Bill Luebkert

On 8/19/2011 1:01 PM, Rothenmaier, Deane C. wrote:
> Greetings, O Wise Ones=85
>
> I=92m trying to understand the behavior I=92m getting from this code:
....
> The red text illustrates the error (if such it be?). My SWAG is that the =
=93nothing=94 between Perl=92s =91^=92 anchor and the first space in $strin=
g2 is what=92s causing the problem. Can someone confirm this? Or provide me=
with the correct answer if I=92m wrong?

perlfunc under split:
As a special case, specifying a PATTERN of space (' ') will sp=
lit on
white space just as "split" with no arguments does. Thus,
"split(' ')" can be used to emulate awk's default behavior, wh=
ereas
"split(/ /)" will give you as many null initial fields as ther=
e are
leading spaces. A "split" on "/\s+/" is like a "split(' ')" ex=
cept
that any leading whitespace produces a null first field. A "sp=
lit"
with no arguments really does a "split(' ', $_)" internally.

# My version:

use strict;
use warnings;
my $string1 =3D
'Windows XP Professional Service Pack 2 (build 2600) Hewlett-Packard';

my @ary1 =3D split ' ', $string1;
my ($win_type1, $win_ver1, $svc_pack1, $win_build1);
$win_type1 =3D $ary1[1];
$win_ver1 =3D $ary1[2];
if ($win_type1 =3D~ /XP/) {
$svc_pack1 =3D $ary1[5];
($win_build1 =3D $ary1[7]) =3D~ 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 =3D~ /XP/) {
my ($win_type2, $win_ver2, $svc_pack2, $win_build2) =3D
(split /\s+/, $string1)[1, 2, 5, 7];
$win_build2 =3D~ s/\)//;
print "Proposed Method...\n";
print "Type: '$win_type2', Version: '$win_ver2',\n";
print "Service Pack: '$svc_pack2', Build: '$win_build2'\n";
}

@ary1 =3D split ' ', $string1;
$win_type1 =3D $ary1[1];
$win_ver1 =3D $ary1[2];
if ($win_type1 =3D~ /XP/) {
$svc_pack1 =3D $ary1[5];
($win_build1 =3D $ary1[7]) =3D~ 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 =3D
" Windows XP Professional Service Pack 2 (build 2600) Hewlett-Packa=
rd ";

if ($string2 =3D~ /XP/) {
$string2 =3D~ s/^\s*//; # I would either use ' ' split or add this line
my ($win_type2, $win_ver2, $svc_pack2, $win_build2) =3D
(split /\s+/, $string2)[1, 2, 5, 7];
$win_build2 =3D~ 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

Re: Leading Spaces and split()

am 20.08.2011 03:48:53 von Ken Slater

--===============1775864997==
Content-Type: multipart/alternative; boundary=000e0cd3e81265255904aae60c75

--000e0cd3e81265255904aae60c75
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: quoted-printable

On Fri, Aug 19, 2011 at 4:01 PM, Rothenmaier, Deane C. <
deane.rothenmaier@walgreens.com> wrote:

> Greetings, O Wise Ones=85****
>
> ** **
>
> I=92m trying to understand the behavior I=92m getting from this code:****
>
> ** **
>
> #!Perl****
>
>
> ############################################################ #############=
#######
> ****
>
> # PROGRAM: array_test1.pl****
>
>
> ############################################################ #############=
#######
> ****
>
> ** **
>
> use strict;****
>
> use warnings;****
>
> ** **
>
> my $string1 =3D "Windows XP Professional Service Pack 2 (build 2600)
> Hewlett-Packard";****
>
> my $string2 =3D " Windows XP Professional Service Pack 2 (buil=
d
> 2600) Hewlett-Packard ";****
>
> ** **
>
> my @ary1;****
>
> my @ary2;****
>
> ** **
>
> @ary1 =3D split( " ", $string1 );****
>
> ** **
>
> my ($win_type1, $win_ver1, $svc_pack1, $win_build1);****
>
> ** **
>
> $win_type1 =3D $ary1[1];****
>
> $win_ver1 =3D $ary1[2];****
>
> ** **
>
> for ($win_type1) {****
>
> /XP/ && do {****
>
> $svc_pack1 =3D $ary1[5];****
>
> ($win_build1 =3D $ary1[7]) =3D~ 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) =3D (split( /\s+/,
> $string1 ))[1, 2, 5, 7];****
>
> last;****
>
> };****
>
> }****
>
> ** **
>
> $win_build2 =3D~ s{\)}{};****
>
> ** **
>
> print "Proposed Method...\n";****
>
> print "Type: \"$win_type2\", Version: \"$win_ver2\",\n";****
>
> print "Service Pack: \"$svc_pack2\", Build: \"$win_build2\"\n";****
>
> ** **
>
> ** **
>
> ** **
>
> @ary1 =3D split( " ", $string1 );****
>
> ** **
>
> $win_type1 =3D $ary1[1];****
>
> $win_ver1 =3D $ary1[2];****
>
> ** **
>
> for ($win_type1) {****
>
> /XP/ && do {****
>
> $svc_pack1 =3D $ary1[5];****
>
> ($win_build1 =3D $ary1[7]) =3D~ 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) =3D (split( /\s+/,
> $string2 ))[1, 2, 5, 7];****
>
> last;****
>
> };****
>
> }****
>
> ** **
>
> $win_build2 =3D~ 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
> =93nothing=94 between Perl=92s =91^=92 anchor and the first space in $str=
ing2 is
> what=92s causing the problem. Can someone confirm this? Or provide me wit=
h the
> correct answer if I=92m wrong?****
>
> ** **
>
> Thanks!****
>
> ** **
>
> Deane Rothenmaier****
>
> Programmer/Analyst =96 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. =96 Peter Anspach=92s 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

--000e0cd3e81265255904aae60c75
Content-Type: text/html; charset=windows-1252
Content-Transfer-Encoding: quoted-printable


On Fri, Aug 19, 2011 at 4:01 PM, Rothenmaier=
, Deane C. < ens.com">deane.rothenmaier@walgreens.com> wrote:
te class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1=
px solid rgb(204, 204, 204); padding-left: 1ex;">








Greetings, O Wise Ones=85


=A0


I=92m trying to understand the behavior I=92m gettin=
g from this code:


=A0


#!Perl


####################################################=
############################


# PROGRAM: =3D"_blank">array_test1.pl


####################################################=
############################


=A0


use strict;


use warnings;


=A0


my $string1 =3D "Windows XP Professional Servic=
e Pack 2 (build 2600) Hewlett-Packard";


my $string2 =3D "          =
=A0 Windows XP Professional Service Pack 2 (build 2600) Hewlett-Packard &qu=
ot;;


=A0


my @ary1;


my @ary2;


=A0


@ary1 =3D split( " ", $string1 ); >


=A0


my ($win_type1, $win_ver1, $svc_pack1, $win_build1);=


=A0


$win_type1 =3D $ary1[1];


$win_ver1 =3D $ary1[2];


=A0


for ($win_type1) {


   /XP/ && do {


    =A0 $svc_pack1 =3D $ary1[5]; u>


    =A0 ($win_build1 =3D $ary1[7]) =3D~ s{\)=
}{};


    =A0 last;


   };


}


=A0


print "Current Method...\n";=


print "Type: \"$win_type1\", Version:=
\"$win_ver1\",\n";


print "Service Pack: \"$svc_pack1\", =
Build: \"$win_build1\"\n\n";


=A0


my ($win_type2, $win_ver2, $svc_pack2, $win_build2);=


=A0


for ($string1) {


   /XP/ && do {


    =A0 ($win_type2, $win_ver2, $svc_pack2, =
$win_build2) =3D (split( /\s+/, $string1 ))[1, 2, 5, 7];


    =A0 last;


   };


}


=A0


$win_build2 =3D~ s{\)}{};


=A0


print "Proposed Method...\n"; >


print "Type: \"$win_type2\", Version:=
\"$win_ver2\",\n";


print "Service Pack: \"$svc_pack2\", =
Build: \"$win_build2\"\n";


=A0


=A0


=A0


@ary1 =3D split( " ", $string1 ); >


=A0


$win_type1 =3D $ary1[1];


$win_ver1 =3D $ary1[2];


=A0


for ($win_type1) {


   /XP/ && do {


    =A0 $svc_pack1 =3D $ary1[5]; u>


    =A0 ($win_build1 =3D $ary1[7]) =3D~ s{\)=
}{};


    =A0 last;


   };


}


=A0


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";


=A0


for ($string2) {


   /XP/ && do {


    =A0 ($win_type2, $win_ver2, $svc_pack2, =
$win_build2) =3D (split( /\s+/, $string2 ))[1, 2, 5, 7];


    =A0 last;


   };


}


=A0


$win_build2 =3D~ s{\)}{};


=A0


print "Proposed Method...\n"; >


print "Type: \"$win_type2\", Version:=
\"$win_ver2\",\n";


print "Service Pack: \"$svc_pack2\", =
Build: \"$win_build2\"\n";


=A0


=A0


####################################################=
############################


# PROGRAM OUTPUT:


####################################################=
############################


=A0


Current Method...


Type: "XP", Version: "Professional&qu=
ot;,


Service Pack: "2", Build: "2600"=


=A0


Proposed Method...


Type: "XP", Version: "Professional&qu=
ot;,


Service Pack: "2", Build: "2600"=


=A0


FILE string:


Current Method...


Type: "XP", Version: "Professional&qu=
ot;,


Service Pack: "2", Build: "2600"=


=A0


Proposed Method... >


Type: "Windows"=
;, Version: "XP",


Service Pack: "Pack=
", Build: "(build"


=A0


The red text illustrates the error (if such it be?).=
My SWAG is that the =93nothing=94 between Perl=92s =91^=92 anchor and the =
first space in $string2 is what=92s causing the problem. Can someone confir=
m this? Or provide me with the correct answer
if I=92m wrong?


=A0


Thanks!


=A0


Deane Rothenmaier


Programmer/Analyst =96 IT-StdCfg


Walgreens Corp.


2 Overlook Point #N51022D


MS 6515


Lincolnshire, IL 60069


target=3D"_blank">224-542-5150


=A0


I will make sure that my doomsday device is up to co=
de and properly grounded. =96 Peter Anspach=92s list of 100 things to do wh=
en one becomes an Evil Overlord




Look at t=
he documentation on the split function (perldoc -f split). Particularly the=
section that states:
          =A0 As a special cas=
e, specifying a PATTERN of space (' ') will

          =A0 split on white space just as "split&=
quot; with no arguments does.
          =A0 Thus, &q=
uot;split(' ')" can be used to emulate awk's default
=
          =A0 behavior, whereas "split(/ /)" =
will give you as many initial

          =A0 null fields (empty string) as there are l=
eading spaces. A
          =A0 "split" on =
"/\s+/" is like a "split(' ')" except that any<=
br>          =A0 leading whitespace produces a null fir=
st field.


HTH, Ken




--000e0cd3e81265255904aae60c75--

--===============1775864997==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============1775864997==--

Re: Leading Spaces and split()

am 22.08.2011 12:17:01 von Stanislaw Romanski

This is a multi-part message in MIME format.

--===============1845214858==
Content-Type: multipart/alternative;
boundary="----=_NextPart_000_0038_01CC60C5.65C031A0"

This is a multi-part message in MIME format.

------=_NextPart_000_0038_01CC60C5.65C031A0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi,

Let us assume
my $string2 =3D " 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 -----=20
From: Rothenmaier, Deane C.=20
To: activeperl@listserv.ActiveState.com=20
Sent: Friday, August 19, 2011 10:01 PM
Subject: Leading Spaces and split()


Greetings, O Wise Ones.

=20

I'm trying to understand the behavior I'm getting from this code:

=20

#!Perl

=
############################################################ #############=
#######

# PROGRAM: array_test1.pl

=
############################################################ #############=
#######

=20

use strict;

use warnings;

=20

my $string1 =3D "Windows XP Professional Service Pack 2 (build 2600) =
Hewlett-Packard";

my $string2 =3D " Windows XP Professional Service Pack 2 =
(build 2600) Hewlett-Packard ";

=20

my @ary1;

my @ary2;

=20

@ary1 =3D split( " ", $string1 );

=20

my ($win_type1, $win_ver1, $svc_pack1, $win_build1);

=20

$win_type1 =3D $ary1[1];

$win_ver1 =3D $ary1[2];

=20

for ($win_type1) {

/XP/ && do {

$svc_pack1 =3D $ary1[5];

($win_build1 =3D $ary1[7]) =3D~ s{\)}{};

last;

};

}

=20

print "Current Method...\n";

print "Type: \"$win_type1\", Version: \"$win_ver1\",\n";

print "Service Pack: \"$svc_pack1\", Build: \"$win_build1\"\n\n";

=20

my ($win_type2, $win_ver2, $svc_pack2, $win_build2);

=20

for ($string1) {

/XP/ && do {

($win_type2, $win_ver2, $svc_pack2, $win_build2) =3D (split( =
/\s+/, $string1 ))[1, 2, 5, 7];

last;

};

}

=20

$win_build2 =3D~ s{\)}{};

=20

print "Proposed Method...\n";

print "Type: \"$win_type2\", Version: \"$win_ver2\",\n";

print "Service Pack: \"$svc_pack2\", Build: \"$win_build2\"\n";

=20

=20

=20

@ary1 =3D split( " ", $string1 );

=20

$win_type1 =3D $ary1[1];

$win_ver1 =3D $ary1[2];

=20

for ($win_type1) {

/XP/ && do {

$svc_pack1 =3D $ary1[5];

($win_build1 =3D $ary1[7]) =3D~ s{\)}{};

last;

};

}

=20

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";

=20

for ($string2) {

/XP/ && do {

($win_type2, $win_ver2, $svc_pack2, $win_build2) =3D (split( =
/\s+/, $string2 ))[1, 2, 5, 7];

last;

};

}

=20

$win_build2 =3D~ s{\)}{};

=20

print "Proposed Method...\n";

print "Type: \"$win_type2\", Version: \"$win_ver2\",\n";

print "Service Pack: \"$svc_pack2\", Build: \"$win_build2\"\n";

=20

=20

=
############################################################ #############=
#######

# PROGRAM OUTPUT:=20

=
############################################################ #############=
#######

=20

Current Method...

Type: "XP", Version: "Professional",

Service Pack: "2", Build: "2600"

=20

Proposed Method...

Type: "XP", Version: "Professional",

Service Pack: "2", Build: "2600"

=20

FILE string:

Current Method...

Type: "XP", Version: "Professional",

Service Pack: "2", Build: "2600"

=20

Proposed Method...

Type: "Windows", Version: "XP",

Service Pack: "Pack", Build: "(build"

=20

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?

=20

Thanks!

=20

Deane Rothenmaier

Programmer/Analyst - IT-StdCfg

Walgreens Corp.

2 Overlook Point #N51022D

MS 6515

Lincolnshire, IL 60069

224-542-5150

=20

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

=20



------------------------------------------------------------ -------------=
-----


_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
------=_NextPart_000_0038_01CC60C5.65C031A0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable


"urn:schemas-microsoft-com:vml" xmlns:o =
"urn:schemas-microsoft-com:office:office" xmlns:w =
"urn:schemas-microsoft-com:office:word" xmlns:m =
"http://schemas.microsoft.com/office/2004/12/omml">
charset=3Diso-8859-1">




Hi,

 


Let us assume

    my $string2 =
"           =20
Windows   XP Professional Service Pack 2 (build 2600) =
Hewlett-Packard=20
";
Then

    split( /\s+/, =
$string2=20
)

returns the list starting from =
an empty=20
element, as it matches /\s+/ in the beginning of=20
$string2.

 

You probably like to use

    split( ' ', $string2 =

)

which is the special case for 'split'; =
it=20
does the matching AFTER skipping the =
leading=20
spaces in $string2.

The returned list will start form=20
'Windows'.

 

HTH.

 

Cheers,

 

Stanislaw Romanski

style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
----- Original Message -----

style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: =
black">From:=20
href=3D"mailto:deane.rothenmaier@walgreens.com">Rothenmaier, Deane =
C.

To: title=3Dactiveperl@listserv.ActiveState.com=20
=
href=3D"mailto:activeperl@listserv.ActiveState.com">activepe rl@listserv.A=
ctiveState.com=20

Sent: Friday, August 19, 2011 =
10:01=20
PM

Subject: Leading Spaces and =
split()




Greetings, O Wise Ones=85


 


I=92m trying to understand the behavior I=92m =
getting from this=20
code:


 


#!Perl


=
class=3DMsoNormal>########################################## #############=
#########################


# PROGRAM: array_test1.pl


=
class=3DMsoNormal>########################################## #############=
#########################


 


use strict;


use warnings;


 


my $string1 =3D "Windows XP Professional Service =
Pack 2=20
(build 2600) Hewlett-Packard";


my $string2 =
"            =
Windows XP=20
Professional Service Pack 2 (build 2600) Hewlett-Packard =
";


 


my @ary1;


my @ary2;


 


@ary1 =3D split( " ", $string1 );


 


my ($win_type1, $win_ver1, $svc_pack1,=20
$win_build1);


 


$win_type1 =3D $ary1[1];


$win_ver1 =3D $ary1[2];


 


for ($win_type1) {


   /XP/ && do {


      $svc_pack1 =
$ary1[5];


      ($win_build1 =3D =
$ary1[7]) =3D~=20
s{\)}{};


      =
last;


   };


}


 


print "Current Method...\n";


print "Type: \"$win_type1\", Version:=20
\"$win_ver1\",\n";


print "Service Pack: \"$svc_pack1\", Build:=20
\"$win_build1\"\n\n";


 


my ($win_type2, $win_ver2, $svc_pack2,=20
$win_build2);


 


for ($string1) {


   /XP/ && do {


      ($win_type2, =
$win_ver2,=20
$svc_pack2, $win_build2) =3D (split( /\s+/, $string1 ))[1, 2, 5,=20
7];


      =
last;


   };


}


 


$win_build2 =3D~ s{\)}{};


 


print "Proposed Method...\n";


print "Type: \"$win_type2\", Version:=20
\"$win_ver2\",\n";


print "Service Pack: \"$svc_pack2\", Build:=20
\"$win_build2\"\n";


 


 


 


@ary1 =3D split( " ", $string1 );


 


$win_type1 =3D $ary1[1];


$win_ver1 =3D $ary1[2];


 


for ($win_type1) {


   /XP/ && do {


      $svc_pack1 =
$ary1[5];


      ($win_build1 =3D =
$ary1[7]) =3D~=20
s{\)}{};


      =
last;


   };


}


 


print "\nFILE string:\n";


print "Current Method...\n";


print "Type: \"$win_type1\", Version:=20
\"$win_ver1\",\n";


print "Service Pack: \"$svc_pack1\", Build:=20
\"$win_build1\"\n\n";


 


for ($string2) {


   /XP/ && do {


      ($win_type2, =
$win_ver2,=20
$svc_pack2, $win_build2) =3D (split( /\s+/, $string2 ))[1, 2, 5,=20
7];


      =
last;


   };


}


 


$win_build2 =3D~ s{\)}{};


 


print "Proposed Method...\n";


print "Type: \"$win_type2\", Version:=20
\"$win_ver2\",\n";


print "Service Pack: \"$svc_pack2\", Build:=20
\"$win_build2\"\n";


 


 


=
class=3DMsoNormal>########################################## #############=
#########################


# PROGRAM OUTPUT:


=
class=3DMsoNormal>########################################## #############=
#########################


 


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=20
Method...


Type: "Windows", =
Version:=20
"XP",


Service Pack: "Pack", =
Build:=20
"(build"


 


The red text illustrates the error (if such it =
be?). My=20
SWAG is that the =93nothing=94 between Perl=92s =91^=92 anchor and the =
first space in=20
$string2 is what=92s causing the problem. Can someone confirm this? Or =
provide=20
me with the correct answer if I=92m wrong?


 


Thanks!


 


Deane Rothenmaier


Programmer/Analyst =96 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=20
properly grounded. =96 Peter Anspach=92s list of 100 things to do when =
one becomes=20
an Evil Overlord


 






_______________________________________________
ActivePerl =
mailing=20
list
ActivePerl@listserv.ActiveState.com
To unsubscribe:=20
=
http://listserv.ActiveState.com/mailman/mysubs=


------=_NextPart_000_0038_01CC60C5.65C031A0--


--===============1845214858==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============1845214858==--

Problem with DB_FIle and Perl 5.12

am 24.08.2011 18:08:37 von Eric Robertson

I have a problem with DB_File and Perl 5.12. I posted a message on the ASPN=
message board but no one there has come back with an
answer so I=92m trying here.

I have some Perl CGI scripts which were developed some time ago using Activ=
ePerl 5.8. When my Mac OS started using 5.10 I considered
upgrading my version of ActivePerl to 5.10 but I couldn't find a couple of =
modules I needed - DBD::CSV and DBD::mysql - so I decided
to continue to use 5.8. These scripts produce BerkeleyDB files using DB_Fil=
e and I found that when I was remotely debugging these
scripts on my virtual host on the Mac these files could be read OK although=
the Mac was using the inbuilt 5.10 because the scripts
contained the normal /usr/bin/perl shebang line.

I've recently upgraded the Mac's OS to Lion and I see that the inbuilt Perl=
version has been changed to 5.12. I now find that I get
an error when I try to read the BerkeleyDB files. I've used the followed co=
de to test the situation and found that 5.12 won't access
files produced by ActivePerl 5.8 and vice versa - the error message is "Can=
't open test.dbm: Inappropriate file type or format".

=A0 =A0 use strict;
=A0 =A0 use Fcntl;
=A0 =A0 use DB_File;
=A0 =A0 my $file =3D 'test.dbm';=A0 =A0 =

my %dbase;
=A0 =A0 my $error;
=A0 =A0 tie (%dbase, 'DB_File', $file, O_RDWR|O_CREAT, 0666, $DB_BTREE) || =
($error=3D1);
=A0 =A0 if ($error) {
=A0 =A0 =A0 print "Can't open $file: $!\n";
=A0 =A0 }
=A0 =A0 $dbase{1} =3D 'one';
=A0 =A0 $dbase{2} =3D 'two';
=A0 =A0 $dbase{3} =3D 'three';
=A0 =A0 untie %dbase;

=A0 =A0 tie (%dbase, 'DB_File', $file, O_RDONLY, 0666, $DB_BTREE) || ($erro=
r=3D1);
=A0 =A0 if ($error) {
=A0 =A0 =A0 print "Can't open $file: $!\n";
=A0 =A0 }

Is the problem something to do with the BerkeleyDB code rather than Perl an=
d how can I correct this? I know I could change the
shebang line in my code when testing on my server but I'd have to remember =
to change this before I uploaded the code to the live
server - which I'm likely to forget!

Eric Robertson

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Problem with DB_FIle and Perl 5.12

am 24.08.2011 19:15:29 von Bill Luebkert

On 8/24/2011 9:08 AM, Eric Robertson wrote:
> I have a problem with DB_File and Perl 5.12. I posted a message on the AS=
PN message board but no one there has come back with an
> answer so I=92m trying here.
>
> I have some Perl CGI scripts which were developed some time ago using Act=
ivePerl 5.8. When my Mac OS started using 5.10 I considered
> upgrading my version of ActivePerl to 5.10 but I couldn't find a couple o=
f modules I needed - DBD::CSV and DBD::mysql - so I decided
> to continue to use 5.8. These scripts produce BerkeleyDB files using DB_F=
ile and I found that when I was remotely debugging these
> scripts on my virtual host on the Mac these files could be read OK althou=
gh the Mac was using the inbuilt 5.10 because the scripts
> contained the normal /usr/bin/perl shebang line.
>
> I've recently upgraded the Mac's OS to Lion and I see that the inbuilt Pe=
rl version has been changed to 5.12. I now find that I get
> an error when I try to read the BerkeleyDB files. I've used the followed =
code to test the situation and found that 5.12 won't access
> files produced by ActivePerl 5.8 and vice versa - the error message is "C=
an't open test.dbm: Inappropriate file type or format".
>
....
>
> Is the problem something to do with the BerkeleyDB code rather than Perl =
and how can I correct this? I know I could change the
> shebang line in my code when testing on my server but I'd have to remembe=
r to change this before I uploaded the code to the live
> server - which I'm likely to forget!

I don't know the exact answer to your problem, but a while back I had a
similar problem and I ended up writing a script that converted the DB
to a flat file (using the old DB module) and then I wrote a script that
converted the flat file back to a DB using the new modules (I believe I
ran the first part at home and then uploaded the flat files to my website
and then ran the conversion to the new DB there). It's at least a
workaround.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Problem with DB_FIle and Perl 5.12

am 25.08.2011 11:52:46 von Eric Robertson

On 24 Aug 2011, at 18:15, Bill Luebkert wrote:

> On 8/24/2011 9:08 AM, Eric Robertson wrote:
>> I have a problem with DB_File and Perl 5.12. I posted a message on the A=
SPN message board but no one there has come back with an
>> answer so I=92m trying here.
>> =

>> I have some Perl CGI scripts which were developed some time ago using Ac=
tivePerl 5.8. When my Mac OS started using 5.10 I considered
>> upgrading my version of ActivePerl to 5.10 but I couldn't find a couple =
of modules I needed - DBD::CSV and DBD::mysql - so I decided
>> to continue to use 5.8. These scripts produce BerkeleyDB files using DB_=
File and I found that when I was remotely debugging these
>> scripts on my virtual host on the Mac these files could be read OK altho=
ugh the Mac was using the inbuilt 5.10 because the scripts
>> contained the normal /usr/bin/perl shebang line.
>> =

>> I've recently upgraded the Mac's OS to Lion and I see that the inbuilt P=
erl version has been changed to 5.12. I now find that I get
>> an error when I try to read the BerkeleyDB files. I've used the followed=
code to test the situation and found that 5.12 won't access
>> files produced by ActivePerl 5.8 and vice versa - the error message is "=
Can't open test.dbm: Inappropriate file type or format".
>> =

> ...
>> =

>> Is the problem something to do with the BerkeleyDB code rather than Perl=
and how can I correct this? I know I could change the
>> shebang line in my code when testing on my server but I'd have to rememb=
er to change this before I uploaded the code to the live
>> server - which I'm likely to forget!
> =

> I don't know the exact answer to your problem, but a while back I had a
> similar problem and I ended up writing a script that converted the DB
> to a flat file (using the old DB module) and then I wrote a script that
> converted the flat file back to a DB using the new modules (I believe I
> ran the first part at home and then uploaded the flat files to my website
> and then ran the conversion to the new DB there). It's at least a
> workaround.

Thanks for that workaround. The only copy of my scripts and files are in my=
virtual host. I'd need a script running locally under 5.8 that read the =

DB files and dumped them using, say, Storable and then another script runni=
ng under 5.12 that read the dumps and produced 5.12 compatible =

DB files. =


These DB files would work OK when I was debugging them from my virtual host=
as the shebang would ensure that 5.12 was being used but if I was =

trying to test them locally from Komodo I'd have to change my default setti=
ng for Perl to the 5.12 location. That's certainly a possibility. =


It would be grand if there's a way in Komodo to determine which version of =
Perl to use on a script by script basis.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs