Chris Stinemetz wrote:
Hello all,

Hello,

I am having problems using hash function. I would like to only extract
4 columns of data from a text file that is ; delimited.

Below is my code along with the errors I am receiving. Any help is
appreciated.

-Chris


1.       #!/usr/bin/perl
2.       use warnings;
3.       use strict;

4.       my $data = (<>);

You read the first line only and assign it to $data. So $data now contains "PACE | EVDOPCMD | 33.0 | 101218 | 07 |".


5.       my %fieldMap = (
6.       "Mtype" =>  5,
7.       "Cell" =>  31,
8.       "Sector" =>  32,
9.       "RLPtxAT" =>  44,
10.   );

You create a hash %fieldMap and assign some values to it.


11.   %fieldMap = split(/;/, $data);

You now overwrite any previous data by assigning to the hash again. The data "PACE | EVDOPCMD | 33.0 | 101218 | 07 |" in $data appears to contain no ';' characters so that is the same as:

   %fieldMap = $data;

And that is why you get "Odd number of elements in hash assignment" because you are assigning a key with no value (a list of one element.)


12.   foreach my $data (keys %fieldMap) {

13.   print "$fieldMap{$data}\t";

And when you try to interpolate that non-existent value you get "Use of uninitialized value within %fieldMap in concatenation (.) or string".


14.   }

Odd number of elements in hash assignment at./beta2.pl line 15,<>
 line 1.
Use of uninitialized value within %fieldMap in concatenation (.) or
string at ./beta2.pl line 18,<>  line 1.

First 3 lines of data looks like:

PACE | EVDOPCMD | 33.0 | 101218 | 07 |
8;1023240136;1218;0;1;00a000001a2bcdc7;0310003147702376;ac016d4a;;;5.6.128.8;0;;;;;43234169;43234349;;;10000;1;1;;0;;19;5.6.128.22;172.30.151.5;304;3;304;3;;;;;15;175;15;175;15;175;1;1798;1251;0;0;2;19;20;;;;;1;1;1;0;128;5.6.128.8;;;;;;;301;5.6.128.8;;;8;304;3;;;;1;43244037;;;1;18;43234169;0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;1;;;;;;43234416;0;0;304;3;21;19;175;15;405;1;1;1;1;0;125;1;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|
8;1023240137;1218;0;1;00a000001db74ace;;ac0174ca;43243423;1678442111;5.6.128.8;1;0;;43242544;43244207;43243423;43243647;;;1000;1;1;;0;;19;5.6.128.26;;372;2;372;2;;43243012;0;43243562;15;175;15;175;15;175;1;;;;;5;48;19;20;49;50;;0;1;2;0;68;5.6.128.8;;;;;;;301;5.6.128.8;;;8;372;2;;;;1;43244207;;;1;18;43243423;0;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;1;;;




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to