Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

In my it is printing in the else i want to get output for the for loop in linux.

by Anonymous Monk
on Mar 28, 2022 at 05:40 UTC ( [id://11142441]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

if (($localdir =~ m/^[\w\W\s\S]+\\((H|HD)[0-9]{1,3}(FA|FC|C|P|CO)[0-9] +{1,3})/i) or ($localdir =~ m/^[\w\W\s\S]+\\((FC|LM|CO)+[\w\W\s\S]+)/i)) { $tcId = $1; } else { die "TC directory structure is not proper!"; }

In the above code it will printing the else loop statement but i want if loop statements i am executing these code in Linux for that I have to change anything in my code. So please Suggest me.

  • Comment on In my it is printing in the else i want to get output for the for loop in linux.
  • Download Code

Replies are listed 'Best First'.
Re: In my it is printing in the else i want to get output for the for loop in linux.
by Corion (Patriarch) on Mar 28, 2022 at 06:00 UTC

    Please rewrite your code as a SSCCE and tell us what output you get and what output you want.

    In your case, such a SSCCE could be:

    #!/usr/bin/perl use strict; use warnings; my $localdir = '\\some\\example\\dir\\HD1FA9'; my $tcId; if (($localdir =~ m/^[\w\W\s\S]+\\((H|HD)[0-9]{1,3}(FA|FC|C|P|CO)[0-9] +{1,3})/i) or ($localdir =~ m/^[\w\W\s\S]+\\((FC|LM|CO)+[\w\W\s\S]+)/i)) { $tcId = $1; } else { die "TC directory structure is not proper!"; } print "Have TC id '$tcId'\n";

    Note that under Linux, directory names are separated with the forward slash / not the backslash \.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: In my it is printing in the else i want to get output for the for loop in linux.
by NetWallah (Canon) on Mar 28, 2022 at 05:58 UTC
    Why are you attempting to match a backslash "\" in a Linux directory structure ?
    Some of your other choices in the regex are also questionable: The first part appears to be a "greedy" match for ALL characters.

    We could help you if you post some data as to what you are looking for, and some sample directories you want matched.
    Show both "match" and "not match" cases, along with what you are attempting to extract.

                    "These opinions are my own, though for a small fee they be yours too."

Re: In my it is printing in the else i want to get output for the for loop in linux.
by Marshall (Canon) on Mar 29, 2022 at 03:00 UTC
    This is bizarre to me: m/^[\w\W\s\S]+...

    This means the line starts with one or more characters which are either a word character or not a word character or a space character or not a space character...

    That is nonsense and matches anything.
    Describe in words that you are trying to match.

        Well one would hope that "in words" means showing some examples in order to fully describe what is supposed to happen. I really have no idea from the OP's current code this thing is supposed to do. I will add "show some actual examples of match and no match" in future such requests.

        Test::More is fine, but something even easier could be fine too. The OP could get his point across by flushing this out with more examples...

        use strict; use warnings; #wild ass guess foreach (qw( /HD123FA45 /HD123FA45/anything anything/HD123FA45 /FC/blah /LM /H4p5 asdf/blah/x/H4p9 /asdf7758 )) { print "$_ "; if (/\/(H|HD)[0-9]{1,3}(FA|FC|C|P|CO)[0-9]{1,3}$/i or /\/(FC|LM|CO)/i) { print "\t...match!\n"} else {print "\t...no match!\n";} } __END__ /HD123FA45 ...match! /HD123FA45/anything ...no match! anything/HD123FA45 ...match! /FC/blah ...match! /LM ...match! /H4p5 ...match! asdf/blah/x/H4p9 ...match! /asdf7758 ...no match!

      This is bizarre to me: m/^[\w\W\s\S]+...

      This means the line starts with one or more characters which are either a word character or not a word character or a space character or not a space character...

      No, that means the line starts with one or more of of \, w, W, s, S. Your description would be right if the code was m/^(\w|\W|\s|\S)+...

      Update: The above is wrong. See below.

      [...] in a regexp is for a character class. (...) is for grouping. See perlre.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        I am not sure about that.
        It is perfectly allowed to use a character class Perl short-cut within brackets, [\d] means the same as [0-9]
        [\w] would mean same as [a-zA-Z0-9_]
        use strict; use warnings; my $x = 'a34x5'; my @y = $x =~ /([\d]+)/g; my @z = $x =~ /([0-9]+)/g; print "@y\n"; # 34 5 \d worked fine print "@z\n"; # 34 5 my @b = $x =~ /([\d\w]+)/g; print "@b\n"; # a34x5 \d irrelevant but \w works my @c = $x =~ /([\w]+)/g; #brackets not needed print "@c\n"; #a34x5 my @d = $x =~ /(\w+)/g; print "@d\n"; #a34x5
        No matter what, the OP's code is bizarre.

        Added: The idea of using an anchor to the beginning of the string, followed by any amount of random stuff, makes no sense to me. Better to leave that off entirely (don't put /^.*(match)/, just put /(match)/.

        I think if you want \ in the character set, you have to escape it with another \

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-20 05:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found