Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Alternatives for index() ... substr() ?

by zarath (Beadle)
on Nov 24, 2017 at 10:02 UTC ( [id://1204194]=note: print w/replies, xml ) Need Help??


in reply to Alternatives for index() ... substr() ?

Thank you for all the suggestions! This place never disappoints!

Since my seemingly unclear naming of the variables has come up a few times, I'll explain this a bit: the variables that are only used for 'calculating' have a short name, because they are only 'tools', not a part of the end result. For example my $sl is short for 'source length' - the length of the string that will be called my $source later on. Other example: my $de is short for 'destination end' - the index of the last character of what will become my $destination.

I feel a little bit silly because in a way, I already use the method that I was looking for (my $text = /blabla/), but I did not know I could actually fetch strings in a way that is understandable to me. I have done some searching before posting this, but all I found about this method is stuff that looks like /"(?>(?:(?>[^"\\]+)|\\.)*)"/. I am truly sorry about my noobieness, but when I see something like that, I can't help but think "What the hell am I staring at?"

On the other hand...

$text =~ /copying(.*)from/ or die "Couldn't find the pattern"; my $msg = $1;

... as suggested by Eily, this I understand and will be testing what I can do with it.

And finally, is it... is it worth trying Parse::RecDescent? Have gone through it and it looks like it might take some time to get my head around it, I do have some time, but I don't like spending time on something just to see it does not help at all. Have read through the thread linked by LanX and I can't help feeling a bit suspicious about the suggestion.

Replies are listed 'Best First'.
Re^2: Alternatives for index() ... substr() ?
by Eily (Monsignor) on Nov 24, 2017 at 10:48 UTC

    You understand the variable names right now, but if you do that often enough, you'll start using the same names to mean different things ($dl for download, destination length, desired language etc...) and will get more confusing. And you shouldn't rely on being able to remember what the names mean to understand the code.

    I'm glad that you understand the solution I proposed (rather than just copy and paste it), be aware that it is not perfect though. As Laurent_R said, if "copying" or "from" can be found somewhere else in the string (in the file names or the paths) the regex might use the wrong ones. The solutions proposed by choroba or Laurent_R might be better fitted in some cases.

    is it worth trying Parse::RecDescent?
    ...
    Not for your problem (although it's a great tool in other cases). Sundial seems to like demonstrating that he has approximate knowledge of many things, so he favours being able to say a lot about something over relevance or even technical accuracy. Just ignore his posts.

Re^2: Alternatives for index() ... substr() ?
by hippo (Bishop) on Nov 24, 2017 at 10:49 UTC
    I am truly sorry about my noobieness, but when I see something like that, I can't help but think "What the hell am I staring at?"

    It's that sort of syntax which, at least anecdotally, is blamed for putting a lot of people off Perl. However, syntax is just syntax and the more you use it the less arcane it seems. Regular expressions are an absolute cornerstone of the langauge (and so useful that the PCRE is used in many other tools/languages to their benefit). So it is very worthwhile putting the effort in to learn.

    There's perlretut for the basics and perlre for reference. Then you can dive into the various wonders of Pattern Matching, Regular Expressions, and Parsing. Take it slowly - get comfortable with the basics then add in one new extra feature at a time. You will soon find that they are as ubiquitous as everyone claims. Good luck and enjoy the ride.

Log In?
Username:
Password:

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

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

    No recent polls found