-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
(e-mail address removed) (Greg Carlill) wrote in
Hi All,
I have a text file that contains column data like:
730 B13
730 B33
730 B53
730 B73
800 B10-1
800 B30-1
800 B50-1
800 B70-1
and want to get a output text file like this.
730 B13, B33, B53, B73
800 B10-1, B30-1, B50-1, B70-1
Perl 5.005_02 on NT is all I have to do this. What is the best way to
attack this. I have almost no knowledge of Perl but am willing to
learn what I need.
First of all, a couple questions: Are the values in the second column
going to be unique for each value in the right column? Do you need to
output the result in the same order as the values arrived in the first
column?
Assuming the answers are both "yes", I'd attack the problem as follows:
1. loop over each line in the file
a. split into two parts, $col1 and $col2.
b. push $col1 onto @col1values if $col2values{$col1} doesn't
exist.
c. push $col2 onto @{ $col2values{$col1} }.
2. loop over each value in @col1values
a. set $aref = $col2values{$_}
b. print $_, @$aref (suitably formatted).
In other words, keep the column 1 values in an array (to preserve order),
and keep the column 2 values in a hash-of-arrays, keyed on the column 1
value.
If the output order isn't significant, you can dispose of the col1values
array -- just use the keys() of %col2values.
If the second column values are not necessarily unique, but you want to
remove dups for the output, you'll have to modify %col2values to be a
hash-of-hashes, and output keys(%{$col2values{$_}} instead of @$aref in
step 2b. If you additionally need to preserve input order, you'll have
to keep a parallel array of col2 values, like you did in @col1values.
Make sense? If not, feel free to ask me to elaborate
- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print
-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <
http://www.pgp.com>
iQA/AwUBP1aqP2PeouIeTNHoEQJTOgCZAcCZryK+/dFk7l9jJvaEnnCQHroAoL9L
p4GdE9QjeVP1xab/Qqe/cj0y
=kcCw
-----END PGP SIGNATURE-----