http://qs1969.pair.com?node_id=697372

partymeeple has asked for the wisdom of the Perl Monks concerning the following question:

a humble dead-set newbie looks for sandles to kiss...
the if statement in the while loop is fooled by white space that apears infron of some of the array elements after they have been passed though the web server. i tried stripping these but it broke other parts of my code.
what's a newbie freindly way to ignore white space in the array elements my if??

$i=0; my $lic_number; while ($staff[$i*3]){ if (($staff[$i*3+$staff_last] eq $names[0]) && ($staff[$i*3+$staff +_first] eq $names[1])){ do some stuff... last; } $i++; }

your humble novice,
meeple (who is comming to terms with the limitations of the llama book.. i think i need a camel)

beware the farmers.

Replies are listed 'Best First'.
Re: if fooled by pesky leading white space
by GrandFather (Saint) on Jul 14, 2008 at 03:50 UTC

    Use a regex instead of eq to perform a more relaxed match:

    if ( ($staff[$i * 3 + $staff_last] =~ /^\s*\Q$names[0]\E\s*$/) && ($staff[$i * 3 + $staff_first] =~ /^\s*\Q$names[1]\E\s*$/))

    Perl is environmentally friendly - it saves trees
      YAY!!

      that's what i was looking for.. i knew it must be possible but i've not got my head around regular expressions yet..

      thank you both for your help

      -- bows low--

      beware the farmers.

        Some of the resources I list here may be of help to you.

        HTH,

        planetscape
Re: if fooled by pesky leading white space
by Fletch (Bishop) on Jul 14, 2008 at 03:37 UTC

    It's not if, it's eq; the string "abc" is not equal to " abc" (computers are quite pedantic that way). If stripping white space is causing problems elsewhere then create a stripped copy and compare that instead.

    ( my $staff_last_clean = $staff[ $i * 3 + $staff_last ] ) =~ s/^\s+//; if( $staff_last_clean eq $names[ 0 ] ) { # ... }

    That'll leave the original untouched for elsewhere.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: if fooled by pesky leading white space
by Grey Fox (Chaplain) on Jul 14, 2008 at 16:18 UTC
      i went on another google style pilgramage and found a sample of somones tech writing. so anyone else comming across this node looking for help, like this novice, would do well to visit it. it's very "layman".

      http://work.lauralemay.com/samples/perl.html

      once again... thanks to all the responders

      --wanders off too look at the other nodes the brothers have pointed too--

      beware the farmers.