Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

if fooled by pesky leading white space

by partymeeple (Novice)
on Jul 14, 2008 at 03:30 UTC ( #697372=perlquestion: print w/replies, xml ) Need Help??

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

      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.


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

      once again... thanks to all the responders

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

      beware the farmers.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://697372]
Approved by GrandFather
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2022-09-27 21:59 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (124 votes). Check out past polls.