Home > Archive > PERL Beginners > March 2005 > Counting Multiple lines of data with a unique column of information
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Counting Multiple lines of data with a unique column of information
|
|
| Wilson, Josh --- Systems Analyst --- GO 2005-03-09, 3:56 am |
| I have a scenario in which palettes are weighed prior to delivery and
then that data is submitted to a server. Once the data is transferred,
I have to parse a batch file and strip out information for each shipment
number and format it for print. =20
The problem is that one shipment might have more than one palette and I
don't know how to strip multiple lines of data (with a unique shipment
number) for processing. I'm using Active Perl 5.8 in a Windows
environment.
For Example here are a few lines of dummy data
(header provided for information only):
ShipmentNumber Weight Date Time LocationID
01000 254 03082005 11:25:21 500
01000 210 03082005 11:27:36 500
01401 112 03082005 11:35:21 500
01401 678 03082005 11:37:36 500
01002 450 03082005 17:54:00 001
01785 105 03082005 03:05:67 250
I need to be able to parse this data and know that shipment #01000 has 2
pallets, that shipment #01401 has 2 pallets, that shipment #01002 has
only 1 pallet, and so on...
I also need to know which shipment # belongs to which locationid.
I think I know how to perform every task necessary with the exception of
knowing how many pallets are with each shipment #.
Any ideas?
****************************************
***************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
****************************************
***************
| |
| John W. Krahn 2005-03-09, 3:56 am |
| Wilson, Josh --- Systems Analyst --- GO wrote:
> I have a scenario in which palettes are weighed prior to delivery and
> then that data is submitted to a server. Once the data is transferred,
> I have to parse a batch file and strip out information for each shipment
> number and format it for print.
>
> The problem is that one shipment might have more than one palette and I
> don't know how to strip multiple lines of data (with a unique shipment
> number) for processing. I'm using Active Perl 5.8 in a Windows
> environment.
>
> For Example here are a few lines of dummy data
> (header provided for information only):
>
> ShipmentNumber Weight Date Time LocationID
>
> 01000 254 03082005 11:25:21 500
> 01000 210 03082005 11:27:36 500
> 01401 112 03082005 11:35:21 500
> 01401 678 03082005 11:37:36 500
> 01002 450 03082005 17:54:00 001
> 01785 105 03082005 03:05:67 250
>
>
> I need to be able to parse this data and know that shipment #01000 has 2
> pallets, that shipment #01401 has 2 pallets, that shipment #01002 has
> only 1 pallet, and so on...
>
> I also need to know which shipment # belongs to which locationid.
>
> I think I know how to perform every task necessary with the exception of
> knowing how many pallets are with each shipment #.
>
> Any ideas?
Off the top of my head (and untested.)
my %locations;
my %pallets;
while ( <FILE> ) {
my ( $ShipmentNumber, $LocationID ) = ( split )[ 0, -1 ];
$pallets{ $ShipmentNumber }++;
push @{ $locations{ $LocationID } }, $ShipmentNumber;
}
for my $ShipmentNumber ( keys %pallets ) {
print "$Shipment Number $ShipmentNumber has $pallets{$ShipmentNumber}
pallets.\n";
}
for my $LocationID ( keys %locations ) {
print "$Location ID $LocationID shipped to @{$locations{$LocationID}}.\n";
}
John
--
use Perl;
program
fulfillment
| |
| Willy West 2005-03-09, 3:56 am |
| forgot to reply to all for the following *laugh*
---------- Forwarded message ----------
From: Willy West <corenth@gmail.com>
Date: Tue, 8 Mar 2005 19:20:24 -0500
Subject: Re: Counting Multiple lines of data with a unique column of information
To: "Wilson, Josh --- Systems Analyst --- GO" <Josh.Wilson@freight.fedex.com>
>
> The problem is that one shipment might have more than one palette and I
> don't know how to strip multiple lines of data (with a unique shipment
> number) for processing. I'm using Active Perl 5.8 in a Windows
> environment.
>
> For Example here are a few lines of dummy data
> (header provided for information only):
>
> ShipmentNumber Weight Date Time LocationID
>
> 01000 254 03082005 11:25:21 500
> 01000 210 03082005 11:27:36 500
> 01401 112 03082005 11:35:21 500
> 01401 678 03082005 11:37:36 500
> 01002 450 03082005 17:54:00 001
> 01785 105 03082005 03:05:67 250
here is one way to do it:
use strict;
use warnings;
my @data =
(
"01000 254 03082005 11:25:21 500",
"01000 210 03082005 11:27:36 500",
"01401 112 03082005 11:35:21 500",
"01401 678 03082005 11:37:36 500",
"01002 450 03082005 17:54:00 001",
"01785 105 03082005 03:05:67 250"
);
my $storage;
#replace the for loop with a
# while(<FILEHANDLE> ) to get the results from an open file :)
#you may want to add chomp(); to the mix as
#well.
for (@data){
print "one line is $_\n";
/(\d{5})(.*)/; #assumes a 5 digit part #
push (@{$storage->{$1}},$2);
#this last bit puts the data into a hash that
#uses the part numbers as its keys..
}
for (keys(%{$storage})){
print "Item $_ has " . @{$storage->{$_}} . " entries\n";
}
----------------------------------------
and here is the sample data's output::
one line is 01000 254 03082005 11:25:21 500
one line is 01000 210 03082005 11:27:36 500
one line is 01401 112 03082005 11:35:21 500
one line is 01401 678 03082005 11:37:36 500
one line is 01002 450 03082005 17:54:00 001
one line is 01785 105 03082005 03:05:67 250
Item 01000 has 2 entries
Item 01401 has 2 entries
Item 01785 has 1 entries
Item 01002 has 1 entries
----------------------------------
look up "regular expressions" with perl and "hash references" in order
to make these
kinds of tasks easier.
good luck :)
Willy
http://www.hackswell.com/corenth
|
|
|
|
|