Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Finding length of line if have any position of any char inside line

by jwkrahn (Abbot)
on Oct 10, 2019 at 19:58 UTC ( [id://11107324]=note: print w/replies, xml ) Need Help??


in reply to Finding length of line if have any position of any char inside line

This appears to do what you require:

my $string = "test\nI want length of this line\n test"; my $position = 12; # Note this postion can be any position in any line +. currently considering it inside 2nd line my $length_of_line; if ( $string =~ /\n.*(?=\n)/ && $-[ 0 ] < $position && $+[ 0 ] > $posi +tion ) { $length_of_line = $+[ 0 ] - $-[ 0 ]; }
  • Comment on Re: Finding length of line if have any position of any char inside line
  • Download Code

Replies are listed 'Best First'.
Re^2: Finding length of line if have any position of any char inside line
by AnomalousMonk (Archbishop) on Oct 11, 2019 at 03:59 UTC

    But the following gives no result even though the
        "\nwant length of this line\n"
    substring containing a character at offset 12 in the overall string matches the  /\n.*(?=\n)/ regex:

    c:\@Work\Perl\monks>perl -wMstrict -le "my $string = qq{test\nI\nwant length of this line\n test}; my $position = 12; ;; my $length_of_line; if ( $string =~ /\n.*(?=\n)/ && $-[ 0 ] < $position && $+[ 0 ] > $pos +ition ) { $length_of_line = $+[ 0 ] - $-[ 0 ]; } print $length_of_line; " Use of uninitialized value in print at -e line 1.


    Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11107324]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-23 06:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found