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

Re: Most efficient way to remove some text from a string

by Marshall (Canon)
on Dec 06, 2016 at 21:26 UTC ( [id://1177334]=note: print w/replies, xml ) Need Help??


in reply to Most efficient way to remove some text from a string

I doubt that you will actually need "the most efficient way" which would mean some sort of performance objective to me. My advice is to do the job in a way that you can understand. All of the Perl ways will run quickly. Worry about efficient after you get something that works and that you understand how it works.

I'm not sure what the indentation level is exactly. Could it be that this "Eminem" token should be deleted it it is there?

use strict; use warnings; use Data::Dumper; my @cases=( '/Volumes/WD/Not Migrating/Music/Ana Tijoux (An Artist)', '/Volumes/WD/Not Migrating/Music/Eminem/Ana Tijoux/Luchin (An Album)', '/Volumes/WD/Not Migrating/Music/Eminem/Ana Tijoux/Luchin/Luchin.m4a ( +A Song)'); foreach my $case (@cases) { $case =~ s|^.+Music/||; # remove /Volumes/WD/Not Migrating/Music/ $case =~ s|\(.+\)||; # remove trailing (An Artist), etc. print "\nLooking at case:$case...\n"; my @parts = split ('/',$case); my $indent = 0; if (@parts == 3){$indent=1;} elsif (@parts==4) {$indent=2;} print "# parts=".@parts," , the indent level=$indent\n"; print "indent line is next:\n"; print "\t"x$indent,"$parts[-1]\n"; #use only last part?? and indent +?? print "all parts:\n"; print Dumper \@parts; } __END__ PRINTOUT: Looking at case:Ana Tijoux ... # parts=1 , the indent level=0 indent line is next: Ana Tijoux all parts: $VAR1 = [ 'Ana Tijoux ' ]; Looking at case:Eminem/Ana Tijoux/Luchin ... # parts=3 , the indent level=1 indent line is next: Luchin all parts: $VAR1 = [ 'Eminem', 'Ana Tijoux', 'Luchin ' ]; Looking at case:Eminem/Ana Tijoux/Luchin/Luchin.m4a ... # parts=4 , the indent level=2 indent line is next: Luchin.m4a all parts: $VAR1 = [ 'Eminem', 'Ana Tijoux', 'Luchin', 'Luchin.m4a ' ];

Replies are listed 'Best First'.
Re^2: Most efficient way to remove some text from a string
by adamZ88 (Beadle) on Dec 07, 2016 at 20:14 UTC

    You are correct! I may be getting more confused trying to make things in the most efficient way with my level of knowledge. I did mean what I said about the most efficient way, but I do see your point. For a newbie this makes things more confusing. I appreciate your feedback, I will try your suggestions later.

      Hi adamZ88,

      You wrote: "I did mean what I said about the most efficient way, but I do see your point." I am not sure that you do indeed "get it yet". There will be no user perceptible speed difference between any code in this thread. Forget it! No difference! All of these program snippets are I/O bound under Windows.

      There are 2 basic approaches to your first problem of how to remove the ...Music stuff from the beginning of the string. (1)involves counting a number of characters in a "prototype" and then removing that exact number of characters from the input string. (2) Use a regular expression like I did to remove the first part of the file path. This is a much more flexible approach and can be used where the complete path to the Music differs.

      Your spec and the introduction of "Eminem" confused me and others. Hence the strange mapping between number of slashes and indentation level in my code.

      My advice is to run all of the proposed solutions on your machine and see if you can figure out how all of them work.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-29 01:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found