PERL to VBScript help

J

Jeremy

Hey VBScript gurus....

I'm a long time Perl programmer who recently learned VBScript and am
pretty familiar with VBScript, but not enough to solve this problem.

I've had serveral Perl scripts that I had written in the past that I
have had to convert to VBScript. Usually no problems.

Today, I was doing some converting, and ran across this code in Perl:

@union = @intersection = @difference = ();
%count = ();
foreach $element (@added, @subList) { $count{$element}++ }
foreach $element (keys %count) {
push @union, $element;
push @{ $count{$element} > 1 ? \@intersection : \@difference },
$element;
}

Basically, we're taking two arrays (@added & @subList) and comparing
them to see what different elemetns @added has vs. @subList and
putting the results into @intersection.

Anyone know how to do this in VBScript? :)

Thanks in advance,
Jeremy
 
J

Jürgen Exner

Jeremy said:
Today, I was doing some converting, and ran across this code in Perl:

@union = @intersection = @difference = ();
%count = ();
foreach $element (@added, @subList) { $count{$element}++ }
foreach $element (keys %count) {
push @union, $element;
push @{ $count{$element} > 1 ? \@intersection : \@difference },
$element;
}

Basically, we're taking two arrays (@added & @subList) and comparing
them to see what different elemetns @added has vs. @subList and
putting the results into @intersection.

Well, yeah, that is a verbatim copy from the FAQ
Anyone know how to do this in VBScript? :)

Why are you asking in a Perl NG about VBScript?

jue
 
R

Ross Presser

(e-mail address removed) (Jeremy) wrote in @posting.google.com:
Today, I was doing some converting, and ran across this code in Perl:

@union = @intersection = @difference = ();
%count = ();
foreach $element (@added, @subList) { $count{$element}++ }
foreach $element (keys %count) {
push @union, $element;
push @{ $count{$element} > 1 ? \@intersection : \@difference },
$element;
}

Basically, we're taking two arrays (@added & @subList) and comparing
them to see what different elemetns @added has vs. @subList and
putting the results into @intersection.

Anyone know how to do this in VBScript? :)


With the aid of the Dictionary object (to emulate a hash) and a helper
function to emulate push for arrays:

Function AAdd(arr,el)
Redim arr(UBound(arr)+1)
arr(UBound(arr))=el
End Function

Dim intersection,union,difference
intersection = Array()
union = Array()
difference = Array()
Dim count
Set count = Createobject("Scripting.Dictionary")
For Each el In added
If Not count.Exists(el) Then
count.Add(el,0)
End If
count(el) = count(el)+1
Next
For Each el in subList
If Not count.Exists(el) Then
count.Add(el,0)
End If
count(el) = count(el)+1
Next
For Each el In count.Keys
AAdd(union,el)
If count(el) > 1 Then
AAdd(intersection,el)
Else
AAdd(difference,el)
End If
Next

(Let me note that your Perl code assumes there are no repeated elements
in either @added or @sublist. My VBscript translation assumes the same.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,378
Latest member
BlakeLig

Latest Threads

Top