Re: splt and hash problem



Johnson, Reginald (GTS) wrote:

I am trying to split and entry from hash and keep getting the following
warning "Use of implicit split to @_ is deprecated at ./chargeback line
34."
I'm not sure how to correct this. What I want the code to do is take the
input file and get a hash of the node names. I did this to eliminate
duplicates.
Then I use "if exist" to get the sumbytes for each node. My final
output should be the nodename and the total bytes for the node.

I read the perldoc -f on split but it didn't give that "ahh" moment for
what I am doing wrong.

My input
tsmpa1,2008-06-05 09:00:03.000000,2008-06-05
09:00:05.000000,BACKUP,35453,MPTELCLTS001,Tcp/Ip,,MPLTTALTS001_2HOUR,138
,2,0,400180,0,0,1,YES,,,,,0,
tsmpa1,2008-06-05 09:00:04.000000,2008-06-05
09:00:10.000000,BACKUP,35455,MPLTTALTS001,Tcp/Ip,,MPLTTALTS001_2HOUR,210
8,2,0,873337,1,0,1,YES,,,,,0,
tsmpa1,2008-06-05 10:00:23.000000,2008-06-05 12:20:01.000000,STGPOOL
BACKUP,295,DIR_DISK ->
DIR_COPY_TE,,,DIR_DISK_BKUP,36865,36865,0,60157952,0,25,1,YES,,,,
,0,
tsmpa1,2008-06-05 10:26:36.000000,2008-06-05
10:29:37.000000,BACKUP,35696,MPLTTABUSADM01,Tcp/Ip,,,0,1,0,754280,0,0,1,
YES,,,,,180,
tsmpa1,2008-06-05 10:29:41.000000,2008-06-05
10:30:06.000000,BACKUP,35705,MPLTTABUSDAT02,Tcp/Ip,,,0,1,0,46088500,0,0,
1,YES,,,,,22,
tsmpa1,2008-06-05 10:29:42.000000,2008-06-05
10:29:47.000000,BACKUP,35708,MPLTTABUSDAT10,Tcp/Ip,,,0,1,0,47606106,0,0,
1,YES,,,,,3,
tsmpa1,2008-06-05 10:29:42.000000,2008-06-05
10:29:58.000000,BACKUP,35707,MPLTTABUSDAT01,Tcp/Ip,,,0,1,0,45704386,0,0,
1,YES,,,,,14,
tsmpa1,2008-06-05 10:29:42.000000,2008-06-05
10:29:58.000000,BACKUP,35709,MPLTTABUSDAT01,Tcp/Ip,,,0,1,0,46011159,1,0,
1,YES,,,,,13,
tsmpa1,2008-06-05 10:29:42.000000,2008-06-05
10:30:04.000000,BACKUP,35711,MPLTTABUSDAT04,Tcp/Ip,,,0,1,0,46056905,0,0,
1,YES,,,,,19,
tsmpa1,2008-06-05 10:29:42.000000,2008-06-05
10:30:04.000000,BACKUP,35706,MPLTTABUSDAT05,Tcp/Ip,,,0,1,0,56903405,0,0,
1,YES,,,,,20,
tsmpa1,2008-06-05 10:29:42.000000,2008-06-05
10:30:05.000000,BACKUP,35710,MPLTTABUSDAT09,Tcp/Ip,,,0,1,0,45471886,1,0,
1,YES,,,,,20,
tsmpa1,2008-06-05 10:29:43.000000,2008-06-05
10:29:58.000000,BACKUP,35725,MPLTTABUSDAT03,Tcp/Ip,,,0,1,0,46047982,2,0,
1,YES,,,,,10,
tsmpa1,2008-06-05 10:29:43.000000,2008-06-05
10:29:59.000000,BACKUP,35722,MPLTTABUSDAT09,Tcp/Ip,,,0,1,0,45182840,2,0,
1,YES,,,,,13,
tsmpa1,2008-06-05 10:29:43.000000,2008-06-05
10:30:00.000000,BACKUP,35714,MPLTTABUSDAT09,Tcp/Ip,,,0,1,0,46098890,0,0,
1,YES,,,,,15,
tsmpa1,2008-06-05 10:29:43.000000,2008-06-05
10:30:01.000000,BACKUP,35726,MPLTTABUSDAT03,Tcp/Ip,,,0,1,0,46155077,0,0,
1,YES,,,,,14,
~
Code

#!/usr/bin/perl
use strict;
use warnings;

&open_files;
#***********************************************************************
********
sub open_files {
open(SUMMARY, "<", "/home/johnsre/sum_file" ) or
die "Summary file could not be opened: $!";
my %nodename_Hash=();
my $total_for_node = 0;
while (<SUMMARY>) {
my ($sumdate,$sumactivity,$sumnode,$sumbytes) = (split
/,/)[1,3,5,12];
print "$sumdate,$sumactivity,$sumnode,$sumbytes\n";
chomp($sumnode);
$nodename_Hash{ $sumnode} = "$sumnode,$total_for_node";
} #end while
my ($key,$value);
for $key (keys %nodename_Hash) {
$value = $nodename_Hash{$key};
print "$key => $value\n"
}#for
close(SUMMARY);
open(SUMMARY, "<", "/home/johnsre/sum_file" ) or
die "Summary file could not be opened: $!";
my $temp_value=0;
while (<SUMMARY>) {
my $temp_node;
my ($sumdate,$sumactivity,$sumnode,$sumbytes) = (split
/,/)[1,3,5,12];
if (exists $nodename_Hash{$sumnode} ) {
my $line;
$line=$nodename_Hash{$sumnode};
#
$temp_node,$temp_value=split(/,/,$nodename_Hash{$sumnode});
$temp_node,$temp_value=split(/,/, $line);
print "$sumnode exists sumbytes=$sumbytes
temp_value=$temp_value temp_node=$temp_value $line\n";
my $new_bytes=0;
$new_bytes =$temp_value+$sumbytes;
$nodename_Hash{$sumnode}= "$sumnode,$new_bytes";
}
} #end while
for $key (keys %nodename_Hash) {
$value = $nodename_Hash{$key};
print "$key => $value\n"
}#for


close(SUMMARY);
} #close summary

There are a few problems with your code, but the error is because the line

$temp_node,$temp_value=split(/,/, $line);

should read

($temp_node,$temp_value) = split(/,/, $line);

It is hard to know how to improve what you ahve written as you have several
variables that are unused, and the subroutine is clearly unfinished. But I would
suggest the following:

- You subroutine open_files() is way too long

- It doesn't open files - it opens one file, twice, and reads the data

- Your indenting needs looking at - you shouldn't need to comment closing braces

- Try to declare variables closer to where they are used. For instance

my ($key,$value);
for $key (keys %nodename_Hash) {
$value = $nodename_Hash{$key};
print "$key => $value\n"
}#for

should be

for my $key (keys %nodename_Hash) {
my $value = $nodename_Hash{$key};
print "$key => $value\n"
}



Once you have tidied your code a little I'm sure we will be able to help you
further.

HTH,

Rob
.



Relevant Pages

  • Re: Hashes and subroutines
    ... values but I get the warning "Odd number of elements in hash assignment ... It means that you have to assign an even number of elements to a hash. ... # ASOS sites are then runs the metarcode subroutine to ... Because you haven't modified $/ the value in $stations should have just one ...
    (comp.lang.perl.misc)
  • Re: Hashes and subroutines
    ... values but I get the warning "Odd number of elements in hash assignment ... # ASOS sites are then runs the metarcode subroutine to ...
    (comp.lang.perl.misc)
  • Re: god im a noob
    ... The loop variable of a foreach is ... A DBM hash is stored on disk. ... Each invocation of this subroutine has its own %numbers_colors ...
    (comp.lang.perl.misc)
  • sort function warning, variable will not stay shared
    ... Suggestions for dealing with a warning for a sort function, ... My query of a database table returns a reference to a hash of all the ...
    (comp.lang.perl.misc)
  • Re: Using subroutines as hash dereferences. Bad idea?
    ... When a subroutine returns a hash reference how can you test ... Check whether you got a reference to a hash back. ... I don't follow why you would return 0 for failure, ...
    (comp.lang.perl.misc)