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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

re-arranging flat file into pipe-delimited

Status
Not open for further replies.

GZPM

IS-IT--Management
Jan 20, 2006
18
US
I've tried every which way (newbie here), but can't seem to come up with a good solution.

here's an example of the raw data:

<tag1>01234567890
<tag2>apple
<tag3>juice
<end>

<tag1>12345678901
<tag2>orange
<tag3>juice
<tag4>ACT
<end>

Here's the desired (pipe-delimited) output needed:

tag1|tag2|tag3|tag4|
01234567890|apple|juice||
12345678901|orange|juice|ACT|

Here's what I've tried for the tag portion, but can't figure out how to hash it.

Code:
open FILE, $ARGV[0] || die $!;
@values = <FILE>;
close FILE;
open TAGS, ">tags.txt" || die $!;

foreach my $tag(@values) {
$tag =~ s/\<\%(.*)\%\>.*$/\1/g;
chomp ($tag);
#how can I make this into a hash before printing it out?
print TAGS "$tag\|";
}
close TAGS;


and here's the data portion, but can't get each delimited record on separate lines:

Code:
open FILE, $ARGV[0] || die $!;
@values = <FILE>;
close FILE;
open DATA, ">data.txt" || die $!;

foreach my $data(@values) {
$data=~ s/.*\>(.*)$/\1/g;
chomp ($data);
print DATA "$data\|";
}
close DATA;

Is it possible to reformat without splitting into two files?

Any help would be appreciated,

thx!
 
I posted a reply on the other forum where you have this same questions posted

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thank you! I'm going to give it a try and post back with results and/or additional questions.
 
Hi Kevin,

Yes, I am stuck!

I tried your code (below) and saw the results, but not sure how to re-arrange what I see into what I need. Any guidance would be appreciated!

Code:
use Data::Dumper;
my %HoA = ();
while(<FILE>){
	if (/^<(\w+)>(.*)$/) {
	push @{$HoA{$1}},$2;
		}
}
print Dumper \%HoA;

below result shows a hash for each tag, whereas it should be a hash for each record. no?

$VAR1 = {
'tag1' => [
'01234567890',
'12345678901'
],
'tag2' => [
'apple',
'orange'
],
'tag3' => [
'juice'
],
'tag4' => [
'ACT' ]
};
 
The following does the reformatting. I'll let you figure out the file processing part yourself though:

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]%hash[/blue] = [red]([/red][red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]$index[/blue] = [fuchsia]0[/fuchsia][red];[/red]

[olive][b]while[/b][/olive] [red]([/red]<DATA>[red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
	[olive][b]next[/b][/olive] [olive][b]unless[/b][/olive] [red]/[/red][purple]^<(.*?)>(.*)[/purple][red]/[/red][red];[/red]
	
	[black][b]my[/b][/black] [red]([/red][blue]$tag[/blue], [blue]$value[/blue][red])[/red] = [red]([/red][blue]$1[/blue], [blue]$2[/blue][red])[/red][red];[/red]
	
	[olive][b]if[/b][/olive] [red]([/red][blue]$tag[/blue] eq [red]'[/red][purple]end[/purple][red]'[/red][red])[/red] [red]{[/red]
		[blue]$index[/blue]++[red];[/red]
		
	[red]}[/red] [olive][b]else[/b][/olive] [red]{[/red]
		[blue]$hash[/blue][red]{[/red][blue]$tag[/blue][red]}[/red] ||= [red][[/red][red]][/red][red];[/red]
		[blue]$hash[/blue][red]{[/red][blue]$tag[/blue][red]}[/red][red][[/red][blue]$index[/blue][red]][/red] = [blue]$value[/blue][red];[/red]
	[red]}[/red]
[red]}[/red]

[black][b]my[/b][/black] [blue]@tags[/blue] = [url=http://perldoc.perl.org/functions/sort.html][black][b]sort[/b][/black][/url] [url=http://perldoc.perl.org/functions/keys.html][black][b]keys[/b][/black][/url] [blue]%hash[/blue][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [url=http://perldoc.perl.org/functions/join.html][black][b]join[/b][/black][/url][red]([/red][red]'[/red][purple]|[/purple][red]'[/red], [blue]@tags[/blue][red])[/red], [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[olive][b]for[/b][/olive] [black][b]my[/b][/black] [blue]$i[/blue] [red]([/red][fuchsia]0..[/fuchsia][blue]$index[/blue]-[fuchsia]1[/fuchsia][red])[/red] [red]{[/red]
	[black][b]print[/b][/black] [black][b]join[/b][/black][red]([/red][red]'[/red][purple]|[/purple][red]'[/red], [url=http://perldoc.perl.org/functions/map.html][black][b]map[/b][/black][/url] [red]{[/red][blue]$hash[/blue][red]{[/red][blue]$_[/blue][red]}[/red][red][[/red][blue]$i[/blue][red]][/red][red]}[/red] [blue]@tags[/blue][red])[/red], [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]


[teal]__DATA__[/teal]
[teal]<tag1>01234567890[/teal]
[teal]<tag2>apple[/teal]
[teal]<tag3>juice[/teal]
[teal]<end>[/teal]

[teal]<tag1>12345678901[/teal]
[teal]<tag2>orange[/teal]
[teal]<tag3>juice[/teal]
[teal]<tag4>ACT[/teal]
[teal]<end>[/teal]

- Miller
 
below result shows a hash for each tag, whereas it should be a hash for each record. no?

Each hash key is an array, you could use that to get the output you wanted. Miller has posted a more complete solution for you. See if that works for you.




------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Oh, and remember to put "use strict;" at the beginning of that script. It's already compliant, but I missed it in my copy and paste.

- Miller
 
It worked like a charm! Thank you both for your assistance on this thread.

Hashes were never my strong suit, and this piece of code is sending me back to the reference books so as to brush up on them and add appropriate notes next to each code line for future reference.

Here's a strange thing though...the query runs perfectly with my test file, but not with the original files. Both, test and originals, are formatted as txt. What's up with that?

Again, thanks!
 
There has to be something different between the files. They may appear the same visually but they might differ in a way you can't see.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top