Off topic? Or limited conditional execution...

P

petersson

Hi,

I'm only familiar with two programming environments. That's Perl and
SPSS (Statistical Package for the Social Science, www.spss.com). This
is actually an SPSS question, but I got no answer from
comp.soft-sys.stat.spss so I turn to this ng since it's the other
environment that I understand (at least roughly). And I can pretty
much translate my SPSS code to Perl.

I have tree lists:

@rows=('q2','q8','q10','q11','q19','q20','q22','q29');
@m_vars=('q10','q19','q20');
@m_labs=('Filter A','Filter B','Filter C');

and I want this print:

q2
q8
Filter A
q11
Filter B
Filter C
q22
q29

I want to loop through @rows, and whenever I encounter a match between
an item in @rows and an item in @m_vars, I want to print the
corresponding item in @m_labs.

Here's the catch: In SPSS I only have access to a foreach-like loop. I
can use a if-structure with "next" ("last", "redo" and "goto" are not
available). The "elsif" is not available in SPSS if-structure, only
"else".

I have a work-around for an auto increment like thingy (++).

With all the tools available in Perl, this is probably a no-brainer,
but the constraints replicated from SPSS suggests a robust solution...
Anyone?

This the translated SPSS code I have so far:

$match=0;
@rows=('q1','q2','q3','q4','q5','q6','q7','q8');
@m_vars=('q3','q5','q6');
@m_labs=('Filter A','Filter B','Filter C');

foreach $i (@rows) {
$i_count=1;
$j_count=1;
foreach $j (@m_vars) {
$k_count=1;
foreach $k (@m_labs){

# Set a flag to use in the
# last condition below...
if($i eq $j && $k_count == 1){
$match++;
}

# Print the main list
if ($i ne $j && $i_count == 1) {
print "$i\n";
}

# Print the "Filters"...
if ($i eq $j && $match == $k_count) {
print "$k\n";
}
$i_count++;
$k_count++;
}
$j_count++;
}
}
 
G

gnari

[snip problem and code that seem to work]

what is your question ?
doesn't your code work properly ?

gnari
 
J

Jürgen Exner

petersson said:
I have tree lists:

@rows=('q2','q8','q10','q11','q19','q20','q22','q29');
@m_vars=('q10','q19','q20');

I'd make this a hash. It's easier coding that way:
my %m_vars=('q10'=> undef, 'q19'=>undef ,'q20' =>undef);
@m_labs=('Filter A','Filter B','Filter C');

and I want this print:

q2
q8
Filter A
q11
Filter B
Filter C
q22
q29

I want to loop through @rows, and whenever I encounter a match between
an item in @rows and an item in @m_vars, I want to print the
corresponding item in @m_labs.

These 5 lines do the job:
for (@rows) {
if (exists $m_vars{$_}){
print ((shift @m_labs) . "\n");
} else {
print "$_\n";
}
}
Here's the catch: In SPSS I only have access to a foreach-like loop. I
can use a if-structure with "next" ("last", "redo" and "goto" are not
available). The "elsif" is not available in SPSS if-structure, only
"else".

Only foreach, no last, redo, goto elsif (which by the way is nothing but
syntactic sugar and can trivially be translated into a "ELSE IF".
Check.
I have a work-around for an auto increment like thingy (++).

My code doesn't use it, check.
With all the tools available in Perl, this is probably a no-brainer,

Indeed, see above.
but the constraints replicated from SPSS suggests a robust solution...

Sorry, I guess you are stuck with the SPSS folks.

jue
 

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
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top