----- Original Message -----
From: "Ken Swift" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, January 31, 2002 12:03 AM
Subject: variable with a split regex


> Hello, I have this small snippet of code that if I
> print out the split, everything looks just fine, but
> if I set the split to a variable and then try to print
> ....it does not work.  For example:
>
> foreach my $host (<HOSTS>) {
>    chomp;
>    if($host =~ /\d+~$CheckHost~.+/) {
>       print split /\d+~\d+~/, $host;
>    }
> }
>
> I get:
> Test1
> Test2
> Test1           these results are correct
> Test2
>
> But
>
> If i change the code to this:
>
> foreach my $host (<HOSTS>) {
>    chomp;
>    if($host =~ /\d+~$CheckHost~.+/) {
>       $CheckHost = split /\d+~\d+~/, $host;
>       print "$CheckHost\n"
>    }
> }
>
> I get:
>
> 2
> 2            these results are wrong
> 2
> 2
>
Okay. Here's a solution:
foreach my $host (<HOSTS>) {
    chomp;
    if($host =~ /\d+~$CheckHost~.+/) {
       @CheckHosts = split /\d+~\d+~/, $host;
       print join("\n", @CheckHosts) . "\n";
    }
}

split returns an array of results if the variable or function
can accept it. It the variable is a scalar, it returns the
number of elements it found or the equivalent of
(scalar(@array).

ego
Edward G. Orton, GWN Consultants Inc.
Phone: 613-764-3186, Fax: 613-764-1721
email: [EMAIL PROTECTED]

_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users

Reply via email to