Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: How do I get an exclusion with grep?

by Lady_Aleena (Priest)
on Apr 27, 2020 at 07:17 UTC ( [id://11116110]=note: print w/replies, xml ) Need Help??


in reply to Re: How do I get an exclusion with grep?
in thread How do I get an exclusion with grep?

The only thing I left out of the big block of code with the crossover_magic is the "heading" of the module. So, here is the contents of @cross_files and the results of textify on them side-by-side. textify is at the bottom of this post. Also, if you need any other subroutines, let me know.

@cross_filestextified
[ 'key.svg', 'Spy_Machete.svg', 'Two_Half_Bang.svg', 'McHale_UNCLE_Smart.svg', 'Howling_Gremlins.svg', 'Dragnet.svg', 'View_Scream.svg', 'blank_spinoff.svg', 'Westphall.png', 'Westerns_in_Crisis.svg', 'Mary_Tyler_Moore.svg', 'Better_Fast.svg', 'Terminator_Abyss.svg', 'Frozen_Hatchet.svg', 'Babylon_5_Cases.svg', 'Columbo.svg', 'crossover_archive.zip', 'Burkes_Law.svg', 'temp.svg', 'Westphall.svg', 'Arriving_to_Westphall.svg', 'key-big.svg', 'Departing_from_Westphall.svg', 'MCU.svg', 'Horror.svg', 'Alices_Hazzard.svg' ];
[ 'key', 'Spy Machete', 'Two Half Bang', 'McHale UNCLE Smart', 'Howling Gremlins', 'Dragnet', 'View Scream', 'blank spinoff', 'Westphall', 'Westerns in Crisis', 'Mary Tyler Moore', 'Better Fast', 'Terminator Abyss', 'Frozen Hatchet', 'Babylon 5 Cases', 'Columbo', 'crossover archive', 'Burkes Law', 'temp', 'Westphall', 'Arriving to Westphall', 'key-big', 'Departing from Westphall', 'MCU', 'Horror', 'Alices Hazzard' ];

I am using this in scripts, so no -- options. Here is how crossover_magic is being used in the scripts.

my $magic = crossover_magic(); # no big svgs here, s +o no $opt{big} # The rest have only one that should NOT have ' right' appended to $cl +ass my $magic = crossover_magic( big => ['Horror']); my $magic = crossover_magic( big => ['Westerns in Crisis']); # I'd lik +e to use the word 'Westerns' only my $magic = crossover_magic( big => ['McHale UNCLE Smart']); # I'd lik +e to use ONE word only # All svgs with Westphall in the name are big, so again no ' right' my $magic = crossover_magic( big => ['Westphall']); my $magic = crossover_magic( big => ['Arriving to Westphall']); # I prefer my $magic = crossover_magic( big => ['Westphall']); my $magic = crossover_magic( big => ['Departing from Westphall']); # I prefer my $magic = crossover_magic( big => ['Westphall']);

Here is textify for you to peruse. It looks messy, but regexen are never pretty for me at least.

sub textify { my ($text, $opt) = @_; my $root_link = base_path('link'); $text =~ s/$root_link\///; # removes the $ro +ot_link $text =~ s/_/ /g; # removes underso +res $text =~ s/ (Mr|Mrs|Ms|Dr) / $1. /g; # adds a period f +or Mr, Mrs, Ms, and Dr; I may need to add more $text =~ s/\s&\s/ &amp; /g; # converts ' & ' +to &amp; $text =~ s/\.{3}/&#8230;/g; # converts '...' +to &#8230; $text =~ s/(\w|\b|\s|^)'(\w|\b|\s|$)/$1&#700;$2/g; # converts "'" to + &#700; # The following takes out html tags unless I tell it not to $text =~ s/<.+?>//g unless ($opt->{'html'} && $opt->{'html'} + =~ /^[ytk1]/); # The following takes out parenthes unless I tell it not to $text =~ s/\s\(.*?\)$// unless ($opt->{'parens'} && $opt->{'parens' +} =~ /^[ytk1]/); # The following removes file extensions except .com, .net, and .org $text =~ s/\.\w{2,5}?$// unless $text =~ /\.(?:com|net|org)$/; # $text =~ s/(?<!\A)((?<! )\p{uppercase})/ $1/g; # from Kenosis, kcot +t, and tye on PerlMonks # I could not remember what the previous line was doing. return $text; }

My OS is Debian 10 (Buster); my perl version is 5.28.1.

No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
Lady Aleena

Replies are listed 'Best First'.
Re^3: How do I get an exclusion with grep?
by kcott (Archbishop) on Apr 27, 2020 at 10:15 UTC

    You've only provided snippets of code which has proven problematic. For instance, you have

    my $root_link = base_path('link');

    but don't show the &base_path code. Is it from a module? Did you write it?

    You also have several lines of code like these:

    my $magic = crossover_magic( big => ['Horror']); my $magic = crossover_magic( big => ['Westerns in Crisis']);

    but don't show any context. Are those strings hard-coded as shown? They actually look like return values from textify(). It's also unclear what the comments about preferences mean; for example, what does "# I'd like to use the word 'Westerns' only" refer to? — why not just write 'Westerns' instead of 'Westerns in Crisis'?

    And then there's the array @cross_files whose value you show as an arrayref. Next to it is textified which should probably be $textified if its value is also an arrayref.

    The end result of this is a lot of guesswork and assumptions which are not helpful in providing you with a straightforward answer.

    I put together an SSCCE to test all the possible things that I think you might be doing: it's in the spoiler below. The output is hundreds of lines long so I won't post it here: it should run without any problem with the OS and Perl version you indicated.

    I couldn't replicate "Everything is getting  right appended currently." as you stated in the OP. In fact, I got the expected output for everything except when filenames contained underscores.

    Reviewing what I've provided may help you to solve your problem. If not, please write your own SSCCE and post it. Use only sufficient sample data to demonstrate the issue and do not include code that isn't relevant to the problem: it should only be enough to demonstrate whatever is going wrong.

    Here's my SSCCE (in the spoiler):

    #!/usr/bin/env perl use 5.028_001; use warnings; use Test::More; my @cross_files = @{_get_cross_files()}; my @expected_textified = @{_get_expected_textified()}; plan tests => @cross_files * 7; for (my $i = 0; $i <= $#cross_files; ++$i) { my $got_textified = textify($cross_files[$i]); is($got_textified, $expected_textified[$i], "Test '$cross_files[$i]' --> '$expected_textified[$i]'" ); { my $class = cutdown_crossover_magic($cross_files[$i]); is($class, 'svg_group right', "Test cross_file '$cross_files[$i]' with no 'big' key"); } { my $class = cutdown_crossover_magic($expected_textified[$i]); is($class, 'svg_group right', "Test expected_textified '$expected_textified[$i]' with no + 'big' key"); } { my $class = cutdown_crossover_magic($got_textified); is($class, 'svg_group right', "Test got_textified '$got_textified' with no 'big' key"); } { my $class = cutdown_crossover_magic($cross_files[$i], big => [$cross_files[$i]]) +; is($class, 'svg_group', "Test cross_file '$cross_files[$i]' using 'big' key"); } { my $class = cutdown_crossover_magic($expected_textified[$i], big => [$expected_textifie +d[$i]]); is($class, 'svg_group', "Test expected_textified '$expected_textified[$i]' using ' +big' key"); } { my $class = cutdown_crossover_magic($got_textified, big => [$got_textified]); is($class, 'svg_group', "Test got_textified '$got_textified' using 'big' key"); } } sub cutdown_crossover_magic { my ($cross_file, %opt) = @_; my $class = 'svg_group'; my @big_images = $opt{big} ? @{$opt{big}} : (); my $text = textify($cross_file); $class .= ' right' unless grep(/$text/i, @big_images); return $class; } sub _get_cross_files { [ 'key.svg', 'Spy_Machete.svg', 'Two_Half_Bang.svg', 'McHale_UNCLE_Smart.svg', 'Howling_Gremlins.svg', 'Dragnet.svg', 'View_Scream.svg', 'blank_spinoff.svg', 'Westphall.png', 'Westerns_in_Crisis.svg', 'Mary_Tyler_Moore.svg', 'Better_Fast.svg', 'Terminator_Abyss.svg', 'Frozen_Hatchet.svg', 'Babylon_5_Cases.svg', 'Columbo.svg', 'crossover_archive.zip', 'Burkes_Law.svg', 'temp.svg', 'Westphall.svg', 'Arriving_to_Westphall.svg', 'key-big.svg', 'Departing_from_Westphall.svg', 'MCU.svg', 'Horror.svg', 'Alices_Hazzard.svg' ]; } sub _get_expected_textified { [ 'key', 'Spy Machete', 'Two Half Bang', 'McHale UNCLE Smart', 'Howling Gremlins', 'Dragnet', 'View Scream', 'blank spinoff', 'Westphall', 'Westerns in Crisis', 'Mary Tyler Moore', 'Better Fast', 'Terminator Abyss', 'Frozen Hatchet', 'Babylon 5 Cases', 'Columbo', 'crossover archive', 'Burkes Law', 'temp', 'Westphall', 'Arriving to Westphall', 'key-big', 'Departing from Westphall', 'MCU', 'Horror', 'Alices Hazzard' ]; } # Exactly as posted EXCEPT both '$root_link' lines have been commented + out sub textify { my ($text, $opt) = @_; #my $root_link = base_path('link'); #$text =~ s/$root_link\///; # removes the $r +oot_link $text =~ s/_/ /g; # removes underso +res $text =~ s/ (Mr|Mrs|Ms|Dr) / $1. /g; # adds a period f +or Mr, Mrs, Ms, and Dr; I may need to add more $text =~ s/\s&\s/ &amp; /g; # converts ' & ' +to &amp; $text =~ s/\.{3}/&#8230;/g; # converts '...' +to &#8230; $text =~ s/(\w|\b|\s|^)'(\w|\b|\s|$)/$1&#700;$2/g; # converts "'" to + &#700; # The following takes out html tags unless I tell it not to $text =~ s/<.+?>//g unless ($opt->{'html'} && $opt->{'html'} + =~ /^[ytk1]/); # The following takes out parenthes unless I tell it not to $text =~ s/\s\(.*?\)$// unless ($opt->{'parens'} && $opt->{'parens' +} =~ /^[ytk1]/); # The following removes file extensions except .com, .net, and .org $text =~ s/\.\w{2,5}?$// unless $text =~ /\.(?:com|net|org)$/; # $text =~ s/(?<!\A)((?<! )\p{uppercase})/ $1/g; # from Kenosis, kcot +t, and tye on PerlMonks # I could not remember what the previous line was doing. return $text; }

    — Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-20 15:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found