Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

How to find out a character position ?

by balakrishnan (Monk)
on Feb 25, 2009 at 07:03 UTC ( #746186=perlquestion: print w/ replies, xml ) Need Help??
balakrishnan has asked for the wisdom of the Perl Monks concerning the following question:

$str="1\n234\n1234567890"; $pos=10;
in the above, i need to find out a newline character position which is previous to 10th character of $str.
Any ideas?

Comment on How to find out a character position ?
Download Code
Replies are listed 'Best First'.
Re: How to find out a character position ?
by almut (Canon) on Feb 25, 2009 at 07:15 UTC
    my $pos_of_nl = rindex $str, "\n", 10-1;

    (whether it's 10-1, 10-2 etc. depends on how exactly you're counting... feel free to finetune)

      It matches my need. Thanks.
Re: How to find out a character position ?
by imrags (Monk) on Feb 25, 2009 at 07:43 UTC
    Have a look at this:
    Index function examples
    Hope it is of some help
    Also try

    perldoc -f index

    perldoc -f rindex

    Raghu

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://746186]
Approved by almut
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2015-07-28 03:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (252 votes), past polls