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

    Tore Aursand Guest

    On Wed, 21 Jul 2004 23:32:34 -0700, petersson wrote:
    > 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 a suggestion for you; the idea is to convert @m_vars to hash where
    the element value is the key and the element's position is the value. I
    haven't tested it, though;

    #!/usr/bin/perl
    #
    use strict;
    use warnings;

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

    ## Convert @m_vars to a hash
    my %m_hash = ();
    for ( my $i = 0; $i <= $#m_vars; $i++ ) {
    $m_hash{$m_labs[$i]} = $i;
    }

    ## Output
    foreach ( @rows ) {
    if ( exists $m_hash{$_} ) {
    print $m_labs[$m_hash{$_}] . "\n";
    }
    else {
    print $_ . "\n";
    }
    }


    --
    Tore Aursand <>
    "There are three kinds of lies: lies, damn lies, and statistics."
    (Benjamin Disraeli)
    Tore Aursand, Jul 22, 2004
    #2
    1. Advertising

  3. (petersson) wrote in message news:<>...
    > 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++;
    > }
    > }



    This might be simpler (although not extremely perlish):

    #!/usr/bin/perl
    use strict;
    #
    my @rows=('q2','q8','q10','q11','q19','q20','q22','q29');
    my @m_vars=('q10','q19','q20');
    my @m_labs=('Filter A','Filter B','Filter C');
    my $out;
    my ($i, $j);

    for ($i=0;$i<@rows;$i++) {
    $out = $rows[$i];
    for ($j=0;$j<@m_vars;$j++) {
    $out = $m_labs[$j] if ($rows[$i] eq $m_vars[$j]);
    }
    print $out, "\n";
    }
    Jonathan Tree, 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:
    754
    Toby Inkster
    Jul 23, 2004
  2. Alec S.
    Replies:
    10
    Views:
    10,088
    Alec S.
    Apr 16, 2005
  3. kk

    limited no'of times execution

    kk, Jul 26, 2005, in forum: C Programming
    Replies:
    7
    Views:
    269
    Keith Thompson
    Jul 26, 2005
  4. jacob navia
    Replies:
    5
    Views:
    530
    spinoza1111
    May 30, 2010
  5. petersson
    Replies:
    2
    Views:
    116
    J├╝rgen Exner
    Jul 22, 2004
Loading...

Share This Page