Off topic? Or limited conditional execution...

Discussion in 'Perl Misc' started by petersson, Jul 22, 2004.

  1. petersson

    petersson Guest

    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++;
    }
    }
     
    petersson, Jul 22, 2004
    #1
    1. Advertising

  2. petersson

    gnari Guest

    "petersson" <> wrote in message
    news:...

    [snip problem and code that seem to work]

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

    gnari
     
    gnari, Jul 22, 2004
    #2
    1. Advertising

  3. petersson wrote:
    > 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
     
    Jürgen Exner, Jul 22, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wayne...
    Replies:
    15
    Views:
    772
    Toby Inkster
    Jul 23, 2004
  2. Alec S.
    Replies:
    10
    Views:
    10,172
    Alec S.
    Apr 16, 2005
  3. kk

    limited no'of times execution

    kk, Jul 26, 2005, in forum: C Programming
    Replies:
    7
    Views:
    284
    Keith Thompson
    Jul 26, 2005
  4. jacob navia
    Replies:
    5
    Views:
    548
    spinoza1111
    May 30, 2010
  5. petersson
    Replies:
    2
    Views:
    101
    Jonathan Tree
    Jul 22, 2004
Loading...

Share This Page