Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by toolic (Chancellor)
on Nov 08, 2012 at 19:52 UTC ( #1002969=note: print w/ replies, xml ) Need Help??


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

If your part numbers will never have spaces in them, you could split your lines and build up a hash of arrays:

use strict; use warnings; print "ARGV: $#ARGV\n"; if ($#ARGV != 0) { print "One, and only one (not $#ARGV), command line parameter is +expected\n"; exit; } my $inputFile=$ARGV[0]; if ( not -e $inputFile) { die "$inputFile doesn't exist!!!\nExiting.\n"; } print "Attempting to open $inputFile\n"; open my $file, $inputFile or die "Could not open $inputFile: $!"; my %parts; while (<$file>) { next if /^Part/; my ($part, $info) = split /\s+/, $_, 2; $info =~ s/\s//g; push @{ $parts{$info} }, $part; } for my $info (sort keys %parts) { print "@{ $parts{$info} } - $info - ", scalar @{ $parts{$info} }, +"\n"; } __END__ ARGV: 0 Attempting to open 1.txt P10 - CircleBlue4 - 1 P9 - CircleGreen3 - 1 P1 P4 - CircleRed1 - 2 P7 - RectangleBlue4 - 1 P3 - RectangleRed4 - 1 P5 - SquareBlue1 - 1 P2 P6 - SquareGreen3 - 2 P8 - SquareRed2 - 1

With a little more work, you could sort on hash values to get the output in the order you want.


Comment on Re: My first perl script is working, what did I do wrong?
Download Code
Re^2: My first perl script is working, what did I do wrong?
by BillKSmith (Chaplain) on Nov 08, 2012 at 23:58 UTC
    If you can depend on the fixed columns, use unpack the same way as split.
    Bill
      Yes, I can depend on fixed columns more so than I can on the contents being nicely-formatted. Thanks for the quick replies.
Re^2: My first perl script is working, what did I do wrong?
by Anonymous Monk on Nov 09, 2012 at 11:36 UTC

    1 == @ARGV or die "One, and only one argument expected\n";

    1 == @ARGV or die Usage();

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2014-09-02 05:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (20 votes), past polls