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.
while (<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;
#prints everything
print Dumper \%hash
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;
}
}
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.