With "IF" statements is this possible........

N

Neil

ok,

code first me thinks

my @Inc_Types = (".avi", ".mp3"); # Included File Types


my $ifstring;
my $count = 0;
foreach my $temp(@Inc_Types) {
if ($temp eq 0) {
$ifstring = "(\$File::Find::name =~ /$temp\$/i)";
}
else {
$ifstring .= " || (\$File::Find::name =~ /$temp\$/i)";
}

if ($ifstring) {


#Does its thing if would treat $ifstring as a string and not just tell
me that it is defined


}


think thats the best way i can explane it. im looking for:

"if ($ifstring) {" to be treated as "if ($File::Find::name =~ /.avi$/i)
|| ($File::Find::name =~ /.mp3$/i) {"

and not a check to see if its defined


fix for my way or does it need to be done different?

any help/points in the right direction greatly appriciated
 
A

A. Sinan Unur

ok,

code first me thinks

You have a very odd model of programming in mind.
my @Inc_Types = (".avi", ".mp3"); # Included File Types


my $ifstring;
my $count = 0;
foreach my $temp(@Inc_Types) {
if ($temp eq 0) {
$ifstring = "(\$File::Find::name =~ /$temp\$/i)";
}
else {
$ifstring .= " || (\$File::Find::name =~ /$temp\$/i)";
}

What on God's Stinkin Earth does this mean.

You have two elements, '.avi' and '.mp3' in @Inc_Types.

$temp will be aliased to these elements during the for loop.

$temp can never equal 0.
if ($ifstring) {


#Does its thing if would treat $ifstring as a string and not just tell
me that it is defined


}


think thats the best way i can explane it. im looking for:

"if ($ifstring) {" to be treated as "if ($File::Find::name =~ /.avi$/i)
|| ($File::Find::name =~ /.mp3$/i) {"


OK, you might be able to do what you want by using striong eval, but that
is just messing with everyone's heads.


Are you just trying to see if
and not a check to see if its defined

if ( $ifstring ) {

would not check if $ifstring is defined or not. It checks if $ifstring
contains a true value. Big ... big ... major difference

#!/usr/bin/perl

use strict;
use warnings;

my $ext = join '|', qw( avi mp3 );
my $re = qr{ \. $ext \z }ix;

while (<DATA>) {
print if $re;
}

__DATA__
please read the posting guidelines.avi
please get an introductory book and learn some programming.mp3
please read the faq.avi
please read the documentation.mp3
any help/points in the right direction greatly appriciated

perldoc -q learn

Sinan
 
N

Neil

whoohaaa

eval work thanxyou very much

the example you typed wouldnt have worked the way i wanted it to but
the eval does now to test max length of this method ;)

thanx again for you time
 
A

A. Sinan Unur

whoohaaa

eval work thanxyou very much

the example you typed wouldnt have worked the way i wanted it to but
the eval does now to test max length of this method ;)

Oh, please, that is the wrong way to go.

Sinan
 
U

Uri Guttman

N> whoohaaa
N> eval work thanxyou very much

you are sadly mistaken. eval is the absolutely worst way to solve your
simple problem. why risk the issues with eval (of which there are MANY)
when you could solve your problem without it and very easily?

one idea is to wrap your conditional code in subs. then just call the
appropriate one in the if statement. that could be done with a hash
table which has your condition names as its keys and the code refs
(named or anon) as values.

but i suspect you will stick with eval based on your answer above. too
bad because if you get into an 'eval solves everything' world, your code
will be worthless to anyone else.

uri
 
U

Uri Guttman

ASU> Oh, please, that is the wrong way to go.

this is why you *NEVER* mention eval as a possible solution to
newbies. it can do most of their inane requests (access vars by name
being the most common) but is so evil in general. i know you know not to
use it but you have to also learn to not even mention it to the wrong
audience. it looks too good and they don't know enough to know when to
not use it (which is almost all the time).

uri
 
N

Neil

Uri,

for now it works, wrong way i now know but until i can find another way
this way is better. i generaly have a habbit of going back through my
code to see where i can cut out bits and make them more streamlined

i cant get my head around any other ways of doing it not even sure how
i would get a sub working the way i would like

if you could give a short example it would be great
 
U

Uri Guttman

N> for now it works, wrong way i now know but until i can find another
N> way this way is better. i generaly have a habbit of going back
N> through my code to see where i can cut out bits and make them more
N> streamlined

eval isn't better at all. just work on getting better code now and not
later. that lie is told by so many coders and that is why so much code
is bad as it never gets rewritten as promised. you write it as well as
possible the first time and don't use nasty tricks like eval when they
are not needed.

N> i cant get my head around any other ways of doing it not even sure
N> how i would get a sub working the way i would like

N> if you could give a short example it would be great

that is easy. my general instructions were clear enough. write a named
sub for each condition (your job). make a hash table like this:

my %dumb_ass_conditions = (

bad_test => \&bad_test_condition,
dumber_test => \&dumber_test_condition,
) ;

then look up the code and execute it:

my $condition_code = $dumb_ass_conditions{$ass_key} ;
die "unknown condition $ass_key} unless $condition_code ;
if ( $condition_code->() ) {

blah
}

uri
 
A

A. Sinan Unur

ASU> Oh, please, that is the wrong way to go.

this is why you *NEVER* mention eval as a possible solution to
newbies. it can do most of their inane requests (access vars by name
being the most common) but is so evil in general. i know you know not
to use it but you have to also learn to not even mention it to the
wrong audience. it looks too good and they don't know enough to know
when to not use it (which is almost all the time).

I have learned my lesson. Sometimes you need to get burned to learn to
stay away from fire.

Sinan
 
G

Gunnar Hjalmarsson

Uri said:
this is why you *NEVER* mention eval as a possible solution to
newbies. it can do most of their inane requests (access vars by name
being the most common) but is so evil in general. i know you know not to
use it but you have to also learn to not even mention it to the wrong
audience. it looks too good and they don't know enough to know when to
not use it (which is almost all the time).

If this had been a newsgroup about how to best raise your kids, that
philosophy might have been applicable and worth considering. Now, since
this is a global newsgroup about Perl programming, applying that
philosophy is ridiculous. Please stop bullying the readers of clpmisc by
assuming that they are incapable of taking the responsibility for their
programming decisions.
 
U

Uri Guttman

ASU> Oh, please, that is the wrong way to go.
ASU> I have learned my lesson. Sometimes you need to get burned to
ASU> learn to stay away from fire.


here is some nice burn salve to put on! :)

uri
 
U

Uri Guttman

GH> If this had been a newsgroup about how to best raise your kids, that
GH> philosophy might have been applicable and worth considering. Now,
GH> since this is a global newsgroup about Perl programming, applying that
GH> philosophy is ridiculous. Please stop bullying the readers of clpmisc
GH> by assuming that they are incapable of taking the responsibility for
GH> their programming decisions.

i wouldn't call that bullying. i was just emphasizing how important i
think it is to not give out eval to newbies. i have seen it done too
often and also seen the trainwrecks that follow. and sinan acknowledged
the burn quite professionally. so i feel fine about how i said that.

uri
 
B

Ben Bacarisse

i cant get my head around any other ways of doing it not even sure how i
would get a sub working the way i would like

if you could give a short example it would be great

This might fill in some of the gaps:

use strict;
use warnings;

sub make_or
{
my ($cond1, $cond2) = @_;
return sub {
my $a = shift;
return &$cond1($a) || &$cond2($a);
};
}

my $has_an_o = sub { return shift =~ /o/; };
my $is_5_chars = sub { return length(shift) == 5; };
my $condition = make_or($has_an_o, $is_5_chars);

while (<DATA>) {
print if &$condition($_);
}

__DATA__
one
two
three
four
five
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top