Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Off topic? Or limited conditional execution...

Reply
Thread Tools

Off topic? Or limited conditional execution...

 
 
petersson
Guest
Posts: n/a
 
      07-22-2004
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','q2 9');
@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++;
}
}
 
Reply With Quote
 
 
 
 
Tore Aursand
Guest
Posts: n/a
 
      07-22-2004
On Wed, 21 Jul 2004 23:32:34 -0700, petersson wrote:
> I have tree lists:
>
> @rows=('q2','q8','q10','q11','q19','q20','q22','q2 9');
> @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)
 
Reply With Quote
 
 
 
 
Jonathan Tree
Guest
Posts: n/a
 
      07-22-2004
(petersson) wrote in message news:<. com>...
> 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','q2 9');
> @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','q2 9');
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";
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Internet Explorer "conditional compilation is turned off" SteveYoungTbird Javascript 16 01-03-2009 06:58 PM
limited connectivity for limited users =?Utf-8?B?aG9yc2VmbHk=?= Wireless Networking 1 03-24-2006 05:17 PM
? ELSE Conditional Comment / Using Conditional Comments Inside Other Tags To Comment Out Attributes Alec S. HTML 10 04-16-2005 02:21 AM
Off topic? Or limited conditional execution... petersson Perl Misc 2 07-22-2004 12:54 PM
conditional compilation is turned off Québec Javascript 9 11-14-2003 09:03 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57