Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Transform single arrays to a multi-dimensional array

Status
Not open for further replies.

Evan145

Technical User
Dec 24, 2001
33
IE
Looking for help please........
I am trying to produce an tabular output (multi-dimensional array) based on values derived from an XML file. The search strings work correctly BUT as 'push' appends the collected input to '@results' it gives me this:

NB:Ignore the subscripts, they arent part of the output, just trying showing the '@results'array

Present Output
--------------
[0,0] StringAttrib1
[0,1] StringAttrib2
[0,2] StringAttrib3
[0,3] StringAttrib4

[1,0] 100
[1,1] 150
[1,2] 276
[1,3] 345

[2,0] 1
[2,1] 2
[2,2] 4
[2,3] 3


BUT I want it to look like this:
NB: again subscripts are NOT part of output, just how I would like the array to look.

[0,0] StringAttrib1 [0,1] 100 [0,2] 1
[1,0] StringAttrib2 [1,1] 150 [1,2] 2
[2,0] StringAttrib3 [2,1] 276 [2,2] 4
[3,0] StringAttrib4 [3,1] 345 [3,2] 3

Code:
 open FILE, "test.xml";
 while ( $line = <FILE> ) {
    if( $line =~ m~<$pattern1>(.*?)</$pattern1>~m ) {
         #extract values between XML tags
         push @results, [split(' ', $1)]; 
    }

    if( $line =~ m~<$pattern2>(.*?)</$pattern2>~m ) {
         push @results, [split(' ', $1)];
    }

    if( $line =~ m~<$pattern3>(.*?)</$pattern3>~m ) {
         push @results, [split(' ', $1)];
    }
  }

  for my $list_ref (@results) {
     for my $item_ref (@{$list_ref}) {
         print OUTFILE $item_ref,&quot;\n&quot;;
     }
     print OUTFILE &quot;\n&quot;;
  }

This isn't a quite print format problem, what I think Im trying to do is create a multidimensional array that I can then use in further scripts.

My problem is in the 'push' line I figure:
push @results, [split(' ', $1)];

Evan
 
This is a print format problem. The newline in the inner forloop should be a space instead (or a tab)
[tt]
for my $list_ref (@results) {
for my $item_ref (@{$list_ref}) {
print OUTFILE $item_ref,&quot;\n&quot;;
} ^^^^-- should be a space: &quot; &quot;
print OUTFILE &quot;\n&quot;;
}
[/tt]
In addition, Perl's standard format for stringification of an array is to space-separate the elements. So you could have just as easily done
Code:
for my $list_ref (@array) {
     print OUTFILE &quot;@{$list_ref}\n&quot;;
}
Or even (using $_ as the default of a for loop)
Code:
print OUTFILE &quot;@$_\n&quot; for @array;
Btw, if you find yourself dumping data structres to files then you should look at either the FreezeThaw module, which packs data structures into strings--'freeze'--for printing that can be retrieved--'thaw'ed--later by the same or a different program, OR the Storable module which will dump a data structure directly to a file, as simple as
Code:
store \@array, 'file.dat';

just some ideas,
jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top