Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

printing the subscript separator

by jacques (Priest)
on Feb 09, 2005 at 18:14 UTC ( #429447=perlquestion: print w/replies, xml ) Need Help??
jacques has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to print the value of the subscript separator ($;) in an html page, but I am trying to avoid printing values that show up as spaces and variables that are not defined with:
if ((not defined ($operator)) || ($operator !~ /[^\s+]/))
But when I pass this the subscript separator, the if statement is evaluated to false and a blank or empty space appears in the html page. What's going on?

Problem solved: The issue was with the subscript separator itself. The default value is "\034". This value cannot be rendered in my html, so it appears as space. Thank you to all those who replied.

Replies are listed 'Best First'.
Re: printing the subscript separator
by RazorbladeBidet (Friar) on Feb 09, 2005 at 18:24 UTC
    $operator !~ /[^\s+]/ means "the value of operator does not have, somewhere inside it, anything that is not a whitespace character or a '+' symbol" which basically means the only thing this will match is a word that is all whitespace and/or "+" symbols... not quite what you want :) I'd go with $operator =~ /^\s*$/
Re: printing the subscript separator
by cog (Parson) on Feb 09, 2005 at 18:30 UTC
    Let's put it in English:

    $operator !~ /[^\s+]/

    $operator does not match "a character that is not a space nor the plus sign"

    I think what you want is:

    $operator does not match "a space"

    Which would be:

    $operator !~ /\s/

    Do remember that you're not testing if $operator has only that; you're testing for the existence of one single character in the string (maybe /^\s$/ or /^\s+$/ is what you really want?)

      Let's put it in English

      I want to say: $operator is something more than *just* a space or spaces. So saying as you suggest: "$operator does not match a space". Wouldn't do that.

      You are right about the plus sign and I have removed it. Thanks. So now I have:

      $operator !~ /[^\s]/
      But I am still seeing the same problem with $;
        $operator !~ /^\s*$/

        I used a plus, but I should have used an asterisk :-)

        How about now? Does it work? :-)

Re: printing the subscript separator
by Fletch (Chancellor) on Feb 09, 2005 at 18:17 UTC

    Perhaps you mean $operator !~ /^\s*$/? (which is zero or more characters of just whitespace) You don't really want a character class there. Read perldoc perlretut and perldoc perlre.

      Perhaps you mean $operator !~ /^\s*$/?

      Nope. That if statement is suppose to filter values that are space characters (not zero or more space char).

        Perhaps you could be more descriptive :)

        Update: That's zero or more space characters from start to end... in other words... either nothing (no chars) or all whitespace.

        my $var = '';  
        That will not be caught by /^\s+$/
Re: printing the subscript separator
by trammell (Priest) on Feb 09, 2005 at 18:29 UTC
    If you want to avoid printing undef's and pure whitespace:
    print $op if defined($op) and $op =~ /\S/;
    If any spaces at all are verboten, you want
    print $op if defined($op) and $op !~ /\s/;
    It would help if you clearly stated what you did and did not want to print.
      You should not fear no warnings 'uninitialized'.

      It's not generally a good idea to set it globally but when you really want undef to behave like an empty string for a few lines it's not a bad approach.

      { no warnings 'uninitialized'; print $op if $op =~ /\S/; }

      To be honest it's probably not appropriate for a single line like this but it's a good tool to have in your box.

      Another useful tool is for(SCALAR).

      for ($op) { print if defined && /\S/; }
      It would help if you clearly stated what you did and did not want to print.

      I updated my OP.

Re: printing the subscript separator
by Enlil (Parson) on Feb 09, 2005 at 19:46 UTC
    So what you want is to print out the subscript separator $;, but not print it out if it is completely blank (i.e. just whitespace, empty string), or if it is undefined?

    If I understand you right you want:

    if ( defined $operator and $operator =~ /\S/ ) { #print operator here #it is defined and contains something #that is not whitespace } else { #do something else as $operator is either #not defined or it does not contain a #non whitespace character }
    If I misinterpretted your intention let us know.


      Thanks, I like the way you put things, but I figured out what was wrong and updated my OP.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://429447]
Approved by herveus
Front-paged by friedo
and a moth chases the moon...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2017-11-23 08:05 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (330 votes). Check out past polls.