%HashTempRec = ( 'Name' => '', 'AlarmConsistencyMgr' => '', 'AlarmForwarder' => '', 'AlarmForwarderServer' => '', 'alert_publisher' => '', 'AnalogGatewayMain' => '', 'AssetTracking' => '', 'CallerPosition' => '', );There are more fields, but for readability I've removed them. So now when I read the csv file, ace.dll,,,X,X,,X,X,X,X,X,X,X,,,,,,,X,X,X,,,,X,,,,,,,,X,,,,,X,,, -- there are more records, but I want to keep this post reasonable. I want to populate the Hash Table with the elements from this file. So I tried to do
for each $item (@Dependency)
{
@HashTempRec = $item;
}
This obvisuouly fails and I don't know where to go from here.
Is there a way to load a hash table is this fashion?
--Hash table definiion here--
$Test1 = "D:\\Profiles\\p57571\\Desktop\\Book3.csv";
if( -e $Test1)
{
print "Opening $Test1\n";
open(PACKAGEINPUT, "<$Test1");
my(@lines) = ;
foreach $temp(@lines)
{
chomp $temp;
if( $temp =~ m/\S/ig)
{
#find the extension
my $fileExt = substr( $temp, -3 ) ||'';
#this searches for exe's then any coresponding dlls
if( $fileExt eq 'exe' )
{
foreach my $element( split /,/, $temp)
{
if( $element =~ m/\.exe/)
{
#print "element = $element\n";
}
}
}
else
{
print "temp = $temp\n";
%HastTempRec = $temp;
}
}
}
}
else
{
print "$Test1 does not exist\n";
}
thanks in advance.
I hope my question and reasons why are clear enough.
Use [cpan://Text::xSV]:
use Text::xSV;
my $csv = Text::xSV->new(
filename => "foo.csv",
header => \@column_names,
);
while ($csv->get_row()) {
# You could use fetchrow_hash here, but I'll just print a message
# If you want a HoH or AoH, read up on extract_hash
my ($name, $alert_publisher) = $csv->extract(qw(Name alert_publisher));
print "Dll: $name\t is "
. (($alert_publisher eq 'X')? '' : 'not ')
. "an alert publisher\n";
}
It's worth noting that your problem as posed overwrites the hash each time you read in a new line. If this is not what you want, you need to construct an array (or a hash) of hashes ... see the comment for pointers.
use strict;
use warnings;
my @keys = (
'Name',
'AlarmConsistencyMgr',
'AlarmForwarder',
'AlarmForwarderServer',
'alert_publisher',
'AnalogGatewayMain',
'AssetTracking',
'CallerPosition',
);
$_ = ; chomp;
my @vals = split /,/;
my %hash;
@hash{@keys} = @vals;
for (@keys) {
print "$_ -> $hash{$_}\n";
}
__DATA__
,,,X,X,,X,X,X,X,X,X,X,,,,,,,X,X,X,,,,X,,,,,,,,X,,,,,X,,,
Your problem was that hash keys are not stored in the order they're specified, so your initialization order had nothing to do with the order in which the values were assigned. You have to use an array to preserve order.
perl -ne 'BEGIN{$\="\n";
@keys = qw /
Name
AlarmConsistencyMgr
AlarmForwarder
AlarmForwarderServer
alert_publisher
AnalogGatewayMain
AssetTracking
CallerPosition
/}
@array=split /,/;END{@hash{@keys}=@array;print "$_ -> $hash{$_}" foreach(keys %hash)}' ace.dll
perlmonks.org content © perlmonks.org and idsfa, JFarr, smokemachine, TedPride
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03