Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: To print the file in spool with the new format of input file also

by GotToBTru (Prior)
on Oct 14, 2015 at 19:50 UTC ( [id://1144910]=note: print w/replies, xml ) Need Help??


in reply to To print the file in spool with the new format of input file also

It looks like line 165 is meant to recognize a valid filename and breaks up $basename into components, and in line 177 those components are named and stored in 7 variables. You could come up with a second regular expression to recognize and decompose the new filename format into those same 7 variables.

But if these are truly fixed length and format file names, you might find it easier to use unpack or substr instead of regular expressions for this purpose. It is said that the man who tries to solve his problem with a regular expression now has two problems, and that applies especially for the new programmer. When they work, they can do amazing things. But if you don't really need them, you can save yourself some grief.

Dum Spiro Spero

Replies are listed 'Best First'.
Re^2: To print the file in spool with the new format of input file also
by flexvault (Monsignor) on Oct 14, 2015 at 23:33 UTC

    GotToBTru,

    ++ Well done and well said...Ed

    Regards...Ed

    "Well done is better than well said." - Benjamin Franklin

Re^2: To print the file in spool with the new format of input file also
by skr302 (Initiate) on Oct 15, 2015 at 04:38 UTC
    Hi, I am new to PErl. Can you please modify this script and update if you dont mind. Actually i have not written even a single perl script.
      Actually i have not written even a single perl script.

      So said every monk ever. There's always a first time!

      Dum Spiro Spero
      Can some one please explain me what is happening here?
      # # Care needs to be taken as the full file name (containing the path) i +s # sent to the RPM server. # # Validate Filename Structure unless ($basename =~ /^(.{4}|.{10})_(.{10})_(.{8})_(.{6})_(.{10})_(.{8 +})\.(.{3})(\.gz)?$/) { if ((system @COPY, $filename, sprintf "%s%s%s%s", $UNHANDLED, $del +imiter, $basename, $dupe_extn) == 0) { abort __LINE__,"bad filename '$filename', moved to unhandled d +irectory"; } else { abort __LINE__,"failed to move unhandled file '$filename'!"; } } else { # # File Name Format Fits # ($issuercode, $outputname, $date, $time, $userid, $sequenceno, $fi +letype) = ($1, $2, $3, $4, $5, $6, $7); }
      what is unless ($basename= .............is doing?

        Here's some explanatations and links (the useful kind, I hope):

        • unless is a negated if. Just like if executes certain code if a condition is true, unless executes it if it is not - that is to say, it executes the code unless the condition is true. See Compound Statements for more.

        • =~ is the regular expression binding operator. With a "regular" regular expression (rather than a substitution or transliteration), it matches a variable ($basename) against a regular expression. If you're not familiar with regular expressions, think of it as a pattern describing what the contents of $basename should look like. See perlop.html#Binding-Operators for more on the =~ operator, and perlretut and perlre for more on regular expressions.

        If you're new to Perl, there's a number of helpful books as well, e.g. Learning Perl and Beginning Perl; I also highly recommend Programming Perl, affectionally known as "The Camel". (There's many more books, but they're not necessarily well-suited for beginners.)

        unless ($basename =~ /^(.{4}|.{10})_(.{10})_(.{8})_(.{6})_(.{10})_(.{8 +})\.(.{3})(\.gz)?$/) { # ...
        If the base name does not match the pattern, then abort with either of the two messages, depending on the condition on the next code line. If it does match, do what's in the else instruction.

        It is equivalent (and usually slightly clearer) to turn the instructions around:

        # Validate Filename Structure if ($basename =~ /^(.{4}|.{10})_(.{10})_(.{8})_(.{6})_(.{10})_(.{8})\. +(.{3})(\.gz)?$/) { # # File Name Format Fits # ($issuercode, $outputname, $date, $time, $userid, $sequenceno, $fi +letype) = ($1, $2, $3, $4, $5, $6, $7); } else { if ((system @COPY, $filename, sprintf "%s%s%s%s", $UNHANDLED, $del +imiter, $basename, $dupe_extn) == 0) { abort __LINE__,"bad filename '$filename', moved to unhandled d +irectory"; } else { abort __LINE__,"failed to move unhandled file '$filename'!"; } }

        In addition to what has been said: the "(" and ")" characters create so-called "groups", and the first group is assigned to $1, the second to $2, and so on. See perlre, especially the section on Capture groups.

        For more "basic" information you might want to look into perlintro (which is part of perl's "official" doc), or http://learn.perl.org or http://perl-begin.org

        BTW if you have perl on your computer, you can get at perl's documentation via perldoc, e.g.
        perldoc perlintro

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1144910]
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: (6)
As of 2024-03-28 22:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found