At 11:25 -0700 06/01/2011, Chris Stinemetz wrote:
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.
1. #!/usr/bin/perl
2. use warnings;...
Well, let's you suppose you really wrote this and didn't write the
unnecessary brackets etc. and require people to remove line numbers:
#!/usr/bin/perl
use warnings;
use strict;
my $data = <DATA>;
my %fieldMap = (
"Mtype" => 5,
"Cell" => 31,
"Sector" => 32,
"RLPtxAT" => 44,
);
%fieldMap = split /;/, $data;
for (keys %fieldMap) {
print "$fieldMap{$_}\t";
}
__DATA__
PACE | EVDOPCMD | 33.0 | 101218 | 07 |
8;1023240136;1218;0;1;00a000001a2bcdc7;;;|
8;1023240137;1218;0;1;00a000001db74ace;;;
Your $data ends up as the first line of your three lines of _DATA_,
which contains none of the semicolons you hope later to break it up
with into an array containing an unknown number of elements with a
50/50 chance of not qualifying for conversion to the hash that you
seem to intend but which would be useless even if you succeeded.
In the process of creating this useless hash you destroy the valid
hash you have already painstakingly created at the beginning of the
script. I think you need to make it clear just what you are
intending to do, but to deal simply with the data issue, my $data =
<DATA> means $data is the contents of __DATA__ up to the first $/ and
you can see what I mean if you run this script:
#!/usr/local/bin/perl
use strict;
my $data = (<DATA>);
print "\$data: $data\________\n\n";
$data = (<DATA>);
print "\$data: $data\________\n\n";
my $delimiter = $/;
undef $/;
$data = <DATA>;
$/ = $delimiter;
print "\$data: $data\________\n\n";
__DATA__
a
b
c
d
e
f
END
JD
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/