Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by toolic (Bishop)
on Nov 08, 2012 at 19:52 UTC ( [id://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.

Replies are listed 'Best First'.
Re^2: My first perl script is working, what did I do wrong?
by BillKSmith (Monsignor) 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1002969]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-23 10:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found