Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: My first perl script is working, what did I do wrong?

by rjt (Deacon)
on Nov 09, 2012 at 01:23 UTC ( #1003040=note: print w/ replies, xml ) Need Help??


in reply to Re^2: My first perl script is working, what did I do wrong?
in thread My first perl script is working, what did I do wrong?

  1. You are correct, use strict is not required here. Sorry, old habits die hard. :-)
  2. Not quite. Your example will not compile, but a similarly expanded translation of what I wrote would look like:
for my $field (@fields) { $re .= qr/(?<$field>$field\s*)/; }

Read $_ for a description of the $_ special variable, but the short version is, if you do not supply a variable name to for, Perl will automatically assume $_.

The regex itself might benefit from a bit more explanation. It's capturing all of the column names including trailing whitespace, and saving those as named captures in %+ for use in the line that builds $tmpl:

    my $tmpl = join(' ', map { "A[".length($+{$_})."]" } @fields);

That generates an (un)pack template string based on the field lengths read from the header, determining the length of each field in @fields by taking the length of the same-named capture in %+ . I'm using map here to "map" the values in @fields to the list that I want: a list of the lengths of each field.


Comment on Re^3: My first perl script is working, what did I do wrong?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2014-12-26 15:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls