Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hi perlmonks, i am writing a script that goes through an xml file reporting about the names and prices. I am having problems aligning the data. This is the xml file:
And this is the script:<breakfast_menu> <food> <name>Belgian Waffles</name> <price>$5.95</price> <description>Two of our famous Belgian Waffles with plenty of r +eal maple syrup</description> <calories>650</calories> </food> <food> <name>Strawberry Belgian Waffles</name> <price>$7.95</price> <description>Light Belgian waffles covered with strawberries an +d whipped cream</description> <calories>900</calories> </food> <food> <name>Berry-Berry Belgian Waffles</name> <price>$8.95</price> <description>Light Belgian waffles covered with an assortment o +f fresh berries and whipped cream</description> <calories>900</calories> </food> <food> <name>French Toast</name> <price>$4.50</price> <description>Thick slices made from our homemade sourdough brea +d</description> <calories>600</calories> </food> <food> <name>Homestyle Breakfast</name> <price>$6.95</price> <description>Two eggs, bacon or sausage, toast, and our ever-po +pular hash browns</description> <calories>950</calories> </food> </breakfast_menu>
open($fh,"<","xml.xml") or die $!; while(<$fh>){ if($_ =~ /<price>\$(.*)<\/price>/){ push @arr, $1; }elsif($_ =~ /<name>(.*)<\/name>/){ push @ar2,$1; } } @index = sort{$ar2[$a] cmp $ar2[$b]} 0..$#ar2; @har = @ar2[@index]; @har2 = @arr[@index]; $teller = 0; foreach(@har){ print "$_ "; print $har2[$teller]; print "\n"; $teller++; }
I have tried aligning the output-data with the use of spaces and tabs, but i'm not succeeding. Tx for the help.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: aligning text
by Tux (Canon) on Feb 02, 2018 at 15:14 UTC | |
Re: aligning text
by haukex (Archbishop) on Feb 02, 2018 at 17:22 UTC | |
Re: aligning text
by roboticus (Chancellor) on Feb 02, 2018 at 17:07 UTC | |
Re: aligning text
by choroba (Cardinal) on Feb 02, 2018 at 18:02 UTC | |
Re: aligning text
by LanX (Saint) on Feb 02, 2018 at 15:24 UTC | |
Re: aligning text
by hippo (Bishop) on Feb 02, 2018 at 15:16 UTC | |
by Anonymous Monk on Feb 02, 2018 at 15:28 UTC | |
by hippo (Bishop) on Feb 02, 2018 at 15:43 UTC | |
by dasgar (Priest) on Feb 02, 2018 at 15:57 UTC | |
Re: aligning text
by Anonymous Monk on Feb 03, 2018 at 08:43 UTC |
Back to
Seekers of Perl Wisdom