in reply to Storing the log file name as key/value in hash

I am not sure that you are asking "the right question".

There are better and more secure ways of exporting and then re-importing data than using some sort of eval{}. Consider according to your question:

#!usr/bin/perl use strict; use warnings; use Data::Dumper; my %results; #a hash of array's, (hash key refs an array) print "Input Data parsed into 3 columns:\n"; while (my $line = <DATA>) { chomp $line; my ($text, $ip_thing, $IP_host) = $line =~ /^([^.]+)\.(.*?)\.(IP.*)/; printf ("Data: %-25s %-20s %-s\n", $text,$ip_thing,$IP_host); push @{$results{$ip_thing}},$text; } print "\n"; print "Dumping each hash key's value(s):\n"; foreach my $key (keys %results) { print "$key =>", join (", ", @{$results{$key}}), "\n"; } =Prints Input Data parsed into 3 columns: Data: ULTRIX CW18.72.0.3 IP-HOST1.log Data: DEC_DECSTATION CW180.72.0.3 IP_HOST_AL.log Data: DEC_DECSTATION_ADDR CW180.72.0.3 IP_HOST_al2.log Data: FOR_VISITORS23_HOST HL617.253.1101.2 IP_HOST_hostinfo. +log Data: FOR_VISITORS24_HOST HL617.253.1101.2 IP_HOST_hostinfo2 +.log Data: FOR_VISITORS25_HOST HL617.253.1101.2 IP_HOST_webform3. +log Dumping each hash key's value(s): HL617.253.1101.2 =>FOR_VISITORS23_HOST, FOR_VISITORS24_HOST, FOR_VISIT +ORS25_HOST CW180.72.0.3 =>DEC_DECSTATION, DEC_DECSTATION_ADDR CW18.72.0.3 =>ULTRIX =cut __DATA__ ULTRIX.CW18.72.0.3.IP-HOST1.log DEC_DECSTATION.CW180.72.0.3.IP_HOST_AL.log DEC_DECSTATION_ADDR.CW180.72.0.3.IP_HOST_al2.log FOR_VISITORS23_HOST.HL617.253.1101.2.IP_HOST_hostinfo.log FOR_VISITORS24_HOST.HL617.253.1101.2.IP_HOST_hostinfo2.log FOR_VISITORS25_HOST.HL617.253.1101.2.IP_HOST_webform3.log

Replies are listed 'Best First'.
Re^2: Storing the log file name as key/value in hash
by Magnolia25 (Sexton) on Jun 07, 2017 at 03:43 UTC

    Hi, I approached with your suggestions, but facing some issues after appending some identical data for a key.

    my %HoA; print "Input Data parsed into 3 columns:"; while (my $line = <DATA>) { my ($col1,$restcol) = split /:\s*/, $line ,2; my @restColumns = split '', $restcol; $HoA{$col1} = [@restColumns]; } print "Dumping Output \n"; for my $dataKey (keys %HoA) { print "$dataKey: ", join("\t\t", @{$HoA{$dataKey}}), \n"; } __DATA__ ULTRIX : CW18.72.0.3 IP-HOST1.log DEC_DECSTATION : CW180.72.0.3 IP_HOST_AL.log DEC_DECSTATION_ADDR : CW180.72.0.3 IP_HOST_al2.log FOR_VISITORS23_HOST : HL617.253.1101.2 IP_HOST_hostinfo.log FOR_VISITORS24_HOST : HL617.253.1101.2 IP_HOST_hostinfo2.log FOR_VISITORS25_HOST : HL617.253.1101.2 IP_HOST_webform3.log FOR_VISITORS25_HOST : HL617.253.1101.2 IP_HOST_webform4.log FOR_VISITORS25_HOST : HL617.253.1201.2 IP_HOST_webform4.log

    I get the output as below:

    FOR_VISITORS25_HOST HL617.253.1201.2 IP_HOST_webform4.log DEC_DECSTATION CW180.72.0.3 IP_HOST_AL.log FOR_VISITORS23_HOST HL617.253.1101.2 IP_HOST_hostinfo.log DEC_DECSTATION_ADDR CW180.72.0.3 IP_HOST_al2.log ULTRIX CW18.72.0.3 IP-HOST1.log FOR_VISITORS24_HOST HL617.253.1101.2 IP_HOST_hostinfo2.log

    As shown DATA contains two rows for key FOR_VISITORS25_HOST. But in output is considered only once(As keys are unique to hash).

    FOR_VISITORS25_HOST HL617.253.1101.2 IP_HOST_webform3.log FOR_VISITORS25_HOST HL617.253.1101.2 IP_HOST_webform4.log

    I understand every entry for the same key overwrites previous one and keeps the last value encountered for the key. Tried swapping key and values but no luck!!!

    Another approach is, Now I can have data in below format

    FOR_VISITORS25_HOST HL617.253.1101.2 : IP_HOST_webform3.log FOR_VISITORS25_HOST HL617.253.1101.2 : IP_HOST_webform4.log

    Where I can consider key as <FOR_VISITORS25_HOST HL617.253.1101.2> and values are IP_HOST_webform3.log & IP_HOST_webform4.log

    But yet, Is there a way out I can prepare a output as below or I am thinking wrong?
    FOR_VISITORS25_HOST HL617.253.1101.2 IP_HOST_webform3.log IP_HOST_webform4.log ...... ...... <more values for + same key>
    Thanks.

      use strict; use warnings; my %HoA; while (my $line = <DATA>) { chomp $line; my ($col1,$restcol) = split /:\s*/, $line ,2; my ($col2,$col3) = split ' ', $restcol,2; push @{$HoA{$col1}{$col2}},$col3; } print "Dumping Output \n"; for my $col1 (sort keys %HoA) { my $c1txt=$col1; for my $col2 (sort keys %{$HoA{$col1}}) { my $c2txt=$col2; for my $col3 (@{$HoA{$col1}{$col2}}) { printf "%-22s %-22s %-22s \n",$c1txt,$c2txt,$col3; $c1txt=''; $c2txt=''; } } } __DATA__ ULTRIX : CW18.72.0.3 IP-HOST1.log DEC_DECSTATION : CW180.72.0.3 IP_HOST_AL.log DEC_DECSTATION_ADDR : CW180.72.0.3 IP_HOST_al2.log FOR_VISITORS23_HOST : HL617.253.1101.2 IP_HOST_hostinfo.log FOR_VISITORS24_HOST : HL617.253.1101.2 IP_HOST_hostinfo2.log FOR_VISITORS25_HOST : HL617.253.1101.2 IP_HOST_webform3.log FOR_VISITORS25_HOST : HL617.253.1101.2 IP_HOST_webform4.log FOR_VISITORS25_HOST : HL617.253.1201.2 IP_HOST_webform4.log

      Dumping Output DEC_DECSTATION CW180.72.0.3 IP_HOST_AL.log DEC_DECSTATION_ADDR CW180.72.0.3 IP_HOST_al2.log FOR_VISITORS23_HOST HL617.253.1101.2 IP_HOST_hostinfo.log FOR_VISITORS24_HOST HL617.253.1101.2 IP_HOST_hostinfo2.log FOR_VISITORS25_HOST HL617.253.1101.2 IP_HOST_webform3.log IP_HOST_webform4.log HL617.253.1201.2 IP_HOST_webform4.log ULTRIX CW18.72.0.3 IP-HOST1.log

      I am not quite sure about the requirements. If all that this needed is to put blanks for cols 1 and 2 when they are the same as the previous line, then..perhaps..???
      #!/usr/bin/perl use strict; use warnings; my $prev_line = <DATA>; $prev_line =~ tr/:/ /; # BTW, tr/// is really fast print $prev_line; while (my $this_line = <DATA>) { $this_line =~ tr/:/ /; my ($prev_cols ,$prev_file_name) = $prev_line =~ /^\s*(.*?)(\S+)\s* +$/; my ($this_cols, $this_file_name) = $this_line =~ /^\s*(.*?)(\S+)\s* +$/; if ($prev_cols eq $this_cols) { $this_cols =~ s/\S/ /g; } print $this_cols, $this_file_name,"\n"; $prev_line = $this_line; } =Prints ULTRIX CW18.72.0.3 IP-HOST1.log DEC_DECSTATION CW180.72.0.3 IP_HOST_AL.log DEC_DECSTATION_ADDR CW180.72.0.3 IP_HOST_al2.log FOR_VISITORS23_HOST HL617.253.1101.2 IP_HOST_hostinfo.log FOR_VISITORS24_HOST HL617.253.1101.2 IP_HOST_hostinfo2.log FOR_VISITORS25_HOST HL617.253.1101.2 IP_HOST_webform3.log IP_HOST_webform4.log FOR_VISITORS25_HOST HL617.253.1201.2 IP_HOST_webform4.log =cut __DATA__ ULTRIX : CW18.72.0.3 IP-HOST1.log DEC_DECSTATION : CW180.72.0.3 IP_HOST_AL.log DEC_DECSTATION_ADDR : CW180.72.0.3 IP_HOST_al2.log FOR_VISITORS23_HOST : HL617.253.1101.2 IP_HOST_hostinfo.log FOR_VISITORS24_HOST : HL617.253.1101.2 IP_HOST_hostinfo2.log FOR_VISITORS25_HOST : HL617.253.1101.2 IP_HOST_webform3.log FOR_VISITORS25_HOST : HL617.253.1101.2 IP_HOST_webform4.log FOR_VISITORS25_HOST : HL617.253.1201.2 IP_HOST_webform4.log