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

Discussion in 'Perl Misc' started by Neil, Feb 21, 2006.

  1. Neil

    Neil Guest

    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
    Neil, Feb 21, 2006
    #1
    1. Advertising

  2. "Neil" <> wrote in news:1140546690.918083.59270
    @g14g2000cwa.googlegroups.com:

    > 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
    A. Sinan Unur, Feb 21, 2006
    #2
    1. Advertising

  3. Neil

    Neil Guest

    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
    Neil, Feb 21, 2006
    #3
  4. A. Sinan Unur wrote:
    > "Neil" <> wrote in news:1140546690.918083.59270
    > @g14g2000cwa.googlegroups.com:
    >
    > > 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.


    lmfao.
    it_says_BALLS_on_your forehead, Feb 21, 2006
    #4
  5. "Neil" <> wrote in news:1140549790.304234.204140
    @o13g2000cwo.googlegroups.com:

    > 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
    A. Sinan Unur, Feb 21, 2006
    #5
  6. Neil

    Uri Guttman Guest

    >>>>> "N" == Neil <> writes:

    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

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Feb 21, 2006
    #6
  7. Neil

    Uri Guttman Guest

    >>>>> "ASU" == A Sinan Unur <> writes:

    ASU> "Neil" <> wrote in news:1140549790.304234.204140
    ASU> @o13g2000cwo.googlegroups.com:

    >> 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 ;)


    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

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Feb 21, 2006
    #7
  8. Neil

    Neil Guest

    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
    Neil, Feb 21, 2006
    #8
  9. Neil

    Uri Guttman Guest

    >>>>> "N" == Neil <> writes:

    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

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Feb 21, 2006
    #9
  10. Uri Guttman <> wrote in
    news::

    >>>>>> "ASU" == A Sinan Unur <> writes:

    >
    > ASU> "Neil" <> wrote in
    > news:1140549790.304234.204140 ASU> @o13g2000cwo.googlegroups.com:
    >
    > >> 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 ;)

    >
    > 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

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Feb 21, 2006
    #10
  11. Uri Guttman wrote:
    > 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.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Feb 21, 2006
    #11
  12. Neil

    Uri Guttman Guest

    >>>>> "ASU" == A Sinan Unur <> writes:

    ASU> Uri Guttman <> wrote in

    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).


    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

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Feb 21, 2006
    #12
  13. Neil

    Uri Guttman Guest

    >>>>> "GH" == Gunnar Hjalmarsson <> writes:

    GH> Uri Guttman wrote:
    >> 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).


    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

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Feb 21, 2006
    #13
  14. On Tue, 21 Feb 2006 12:09:12 -0800, Neil wrote:

    > 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


    --
    Ben.
    Ben Bacarisse, Feb 23, 2006
    #14
    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. Neil Zanella
    Replies:
    8
    Views:
    1,170
    mfmehdi
    Oct 20, 2006
  2. Raghavendra R A V, CSS India

    Program without conditional Statements - Possible ?

    Raghavendra R A V, CSS India, Feb 3, 2004, in forum: C Programming
    Replies:
    92
    Views:
    2,125
  3. Harry George
    Replies:
    6
    Views:
    363
    Bart Nessux
    Feb 23, 2004
  4. Vince
    Replies:
    12
    Views:
    739
    Martin Gregorie
    Jan 21, 2008
  5. John Crichton
    Replies:
    6
    Views:
    254
    John Crichton
    Jul 12, 2010
Loading...

Share This Page