in reply to A Better Way to Find the Position of the Last Non-Whitespace Character in the Last Element of an Array.

$MessageLines[$#MessageLines] =~ m/^.*\S/sg; my $CursorCol = pos $MessageLines[$#MessageLines];

Then substr can be used to obtain $Prompt.

  • Comment on Re: A Better Way to Find the Position of the Last Non-Whitespace Character in the Last Element of an Array.
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: A Better Way to Find the Position of the Last Non-Whitespace Character in the Last Element of an Array.
by roubi (Hermit) on Apr 22, 2009 at 17:06 UTC
    The code can also be simplified a bit like this too:
    $MessageLines[-1] =~ m/^.*\S/sg; my $CursorCol = pos $MessageLines[-1];
Re^2: A Better Way to Find the Position of the Last Non-Whitespace Character in the Last Element of an Array.
by MidLifeXis (Monsignor) on Apr 22, 2009 at 17:34 UTC

    I would have used '-1' as the subscript. Would have. As of about 5 minutes ago I changed my mind as to the appropriateness of using -1 as a subscript.

    Consider the following blocks...

    $[ = -4; @a = (qw(a b c d e f g h i j)); print $a[-1], "\n"; print $a[$#a], "\n"; __DATA__ d j

    and

    $[ = 0; @a = (qw(a b c d e f g h i j)); print $a[-1], "\n"; print $a[$#a], "\n"; __DATA__ j j

    There is some DWIMmery with the -1 option that could cause problems, especially if you are working on old, or someone else's code of unknown usage or localization of the $[ variable. And yes, I have read the disclaimers not to use the $[ variable in the fine manual. I didn't say that I would use the variable. :-)

    Update: This is perl, v5.8.8 built for PA-RISC2.0

    Comparison

    Update #2: Comparison of perl binary available on this machine:

    for x in 100 1 0 -1 -6 -7 -11 -12 -99; do perl -le "\$[=$x; my @ra=qw(a b c d e f); print \$[, ': -1:' , \$r +a[-1], ': $#: ', \$ra[\$#ra]" done
    • This is perl, version 5.005_02 built for PA-RISC1.1

      100: -1: f: $#: f 1: -1: f: $#: f 0: -1: f: $#: f -1: -1: f: $#: f -6: -1: f: $#: f -7: -1: f: $#: e -11: -1: f: $#: a -12: -1: f: $#: -99: -1: f: $#:
    • This is perl, v5.8.8 built for PA-RISC2.0

      100: -1: f: $#: f 1: -1: f: $#: f 0: -1: f: $#: f -1: -1: a: $#: f -6: -1: f: $#: f -7: -1: : $#: e -11: -1: : $#: a -12: -1: : $#: -99: -1: : $#:

    Conclusion: Don't use $[ as anything other than 0 (as presented by a couple of other monks so far.

    --MidLifeXis

    The tomes, scrolls etc are dusty because they reside in a dusty old house, not because they're unused. --hangon in this post

      Interesting. Running the same test on my ActiveState Win32 Perl 5.8.2 build 808, I get results indicating that -1 more reliably accesses the last element of an array.

      The following one-liner was run repeatedly with  $[ initialized to different values:

      >perl -wMstrict -le "$[ = 100; my @ra = qw(a b c d e f); print '$[: ', $[, ' -1: ', $ra[-1], ' $#: ', $ra[$#ra];" $[: 100 -1: f $#: f
      Output of successive runs of the one-liner:
      $[: 100 -1: f $#: f $[: 1 -1: f $#: f $[: 0 -1: f $#: f $[: -1 -1: f $#: f $[: -6 -1: f $#: f $[: -7 -1: f $#: e $[: -11 -1: f $#: a Use of uninitialized value in print at -e line 1. $[: -12 -1: f $#: Use of uninitialized value in print at -e line 1. $[: -99 -1: f $#:
      Perhaps more support for the don't do that part of the discussion in the docs about assigning to  $[ a value other than 0?

      (BTW, the latest perlvar sez wrt  $[ that As of release 5 of Perl, assignment to $[ is treated as a compiler directive, and cannot influence the behavior of any other file, so old code that plays too fast and loose with  $[ may be broken anyway.)

      I think  $[ was originally intended to allow use of either 0-based or 1-based arrays, and then things kinda got out of hand. Sanity is slowly being restored.

        See the Numerical Recipes series, IIRC. I seem to also remember that there was a 1-based vs 0-based set of changes going around, or perhaps it was in Numerical Recipes in Pascal.

        The general case of assigning any value to $[ opens up many interesting problems :-)

        --MidLifeXis

        The tomes, scrolls etc are dusty because they reside in a dusty old house, not because they're unused. --hangon in this post

Re^2: A Better Way to Find the Position of the Last Non-Whitespace Character in the Last Element of an Array.
by NateTut (Deacon) on Apr 22, 2009 at 21:16 UTC
    Thanks, I tried pos originally on my regex but it ket coming back with 20, which makes sense now that I think about it.