Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by rjt (Deacon)
on Nov 08, 2012 at 21:16 UTC ( #1002985=note: print w/ replies, xml ) Need Help??


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

You have a lot of options in your approach, here. I humbly offer my somewhat more Perl-ish approach to this problem.

The basis of what I did is to get rid of hard-coded fields and allow you to specify them near the top of the script in @fields, building a regexp to capture them in the header line. I relaxed the usage of the program as well, to allow for specifying a filename on the command line, or, if none is specified, accepting input from STDIN. You can easily change the output format by modifying the final for (sort keys %inv) { ... } loop, and you can change how records are grouped by changing the key() function to suit your tastes. In both cases, I tried to stay with what you had, since I don't know what you want to ultimately do with the data.

use strict; use warnings; use 5.12.00; die "usage: $0 [filename]\n" if (@ARGV > 1); # Set required fields and determine fixed widths my @fields = qw/Part Shape Color Size/; my $re; $re .= qr/(?<$_>$_\s*)/ for @fields; my $header = <>; die "Header must match " . join(' ', @fields) unless ($header =~ /^$re +$/); my $expected_len = length($header); my $tmpl = join(' ', map { "A[".length($+{$_})."]" } @fields); my %inv; # Inventory; $inv{key(%rec)} while (<>) { if (length != $expected_len) { die sprintf("Got length of %d, expecting %d", length, $expecte +d_len); } my @rec = map { /(.+?)\s*$/ } unpack $tmpl; # Get (trimmed) record +s my %rec = map { $_ => shift @rec } @fields; # Add the part to our inventory push @{$inv{key(%rec)}}, $rec{Part}; } # Now print out the inventory in the desired format for (sort keys %inv) { say join(' ',@{$inv{$_}}) . " - $_ - " . scalar @{$inv{$_}}; } # Our custom hashing function for inventory items. Expects %rec argume +nt sub key { my %rec = @_; $rec{Shape} . $rec{Color} . $rec{Size}; }


Comment on Re: My first perl script is working, what did I do wrong?
Select or Download Code
Re^2: My first perl script is working, what did I do wrong?
by killersquirel11 (Novice) on Nov 09, 2012 at 00:43 UTC
    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*)/
      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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2015-07-06 08:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (70 votes), past polls