Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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

by killersquirel11 (Novice)
on Nov 09, 2012 at 00:43 UTC ( #1003038=note: print w/replies, xml ) Need Help??

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

Thanks for the reply, it actually looks like Perl! A couple quick questions on this:
1) Are both 'use 5.12.00' and 'use strict' necessary? From what I read, 'use 5.12.00' implies 'use strict'.
2) With the following line, I'm having a bit of difficulty understanding it.
$re .= qr/(?<$_>$_\s*)/ for @fields;
Is it essentially this?:
for my $field (@fields) $re=$re.qr/(?<$_>$_\s*)/

Replies are listed 'Best First'.
Re^3: My first perl script is working, what did I do wrong?
by rjt (Deacon) on Nov 09, 2012 at 01:23 UTC
    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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1003038]
[choroba]: Tire chains are mandatory here in mountains, and the only unprepared each year are gritters
[stevieb]: I have a 3 day winter survival kit in my vehicle in the event I get snowed in in the mountains (which has happened before due to avalanches closing the roads (and once in the summer due to a massive forest fire that trapped us
[perldigious]: a handful of people in my work area did not make it, but I live pretty close and it's flat the whole way, so I didn't have any trouble. The roundabout is the worst thing I have to negotiate.
[thezip]: G'day all!
[perldigious]: Well, that and dodging the people who forget how ice works right away. :-)
[stevieb]: hey, thezip
[perldigious]: I don't miss having to drive up and down in elevation on roads that are iced over, I white knuckled gripping the steering wheel every time I had to where I used to live.
[perldigious]: So I don't envy you mountain guys if you have to drive in the winter, even with tire chains.
MidLifeXis waves.

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2016-12-06 16:40 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (112 votes). Check out past polls.