Thank very much .Your solution is quite similar to that of poj. I will attempt to explain what is being done and then how I used what I understood to try and arrive at the solution I need.
#prints everything print Dumper \%hashwhile (<IN>){ #remove spaces from the beginning or the end of the file s/^\s+|\s+$//g; # splits the files based on columns based on space and limit the amoun +t split by 4 my ($col1,$col2,$col3) = split /\s+/,$_,4; #checks to see if the word name is matched to get the variable next ov +er which would be the actual name , then put it in variable $name if ($col1 eq 'name'){ $name = $col2; #checks to see if the word device is matched to get the variable next +over which would be the actual type, then next over is another attrib +ute(not on the example) } elsif ($col1 eq 'device') { ##Here the push name, device type and other variable into a hash push @{$hash{$name}},$col2, $col3; } else { # skip line } } close IN;
My issue now comes when I need to print out the content in a structure way, or into a file name device $col3 device $col3 I can sort through hash and get the name only, not all the other attributes.But why? I put them all into the hash right?
foreach my $line(keys %hash) { print $line }
I believe you are doing somewhat similar
##defining the fields you want including the file, HEADER would be the + first field and if name or device then KEY is the next value over an +d VALUE the next use constant { IN_FILE => 'pm_1200636_text.txt', HEADER => 0, KEY => 1, VALUE => 2, }; my %parsed; { open my $fh, '<', IN_FILE; my $name; while (<$fh>) { my @fields = split; if ($fields[HEADER] eq 'name') { $name = $fields[KEY]; next; }
This is the part that gives me issues since I need to print the values in a specified format, so data dumper wouldnt work , any help please?
if ($fields[HEADER] eq 'device') { push @{$parsed{$name}{$fields[KEY]}}, $fields[VALUE]; next; } } }
In reply to Re^2: perl parsing
by cbtshare
in thread perl parsing
by cbtshare
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |