Syntenty has asked for the wisdom of the Perl Monks concerning the following question:
Hi all! This is my first time asking a question so please direct me to the appropriate place to ask questions if this isn't it.
I currently have to files that I need to extract data from and manipulate them. The final steps outputs this manipulated date to a file. The data format from the 2 file are similar. The first file's format is like:
In each of these files, the dog=bird=cat are individual elements that were clumped together with a '=' sign in between them.
What i need to do, for example, using the first line ( dog23=bird209=cat25) is to split this data by removing the '=' and to put each of the three dog, bird and cat, into its own individual index.
This would then become a two-dimensional array, where each row has three columns of elements, [0][0] = dog23, [0][ 1 ] = bird209 and [0][ 2 ] = cat25.
I need to do the other same thing to the other file's data, that is split the elements and put them in a 2D array.
My task is to take each line from FILE 1, take the last element, (ie the cat#) and compare this to the first element of FILE2. If these two elements are equal, I have to put element 0,1,2 and the LAST element from FILE two in a single line and put it in single index of a new array.
So line 1 from FILE1 and Line 1 from FILE2
dog23=bird209=cat25. cat25=cat99I'll take the last element, cat25 from the first file, scan all the lines from FILE2. Where ever I see a match, I need to append the last element of file2, ie cat99 to the first line of FILE1.
So my final output must be:
IN @NEW ARRAY,
Index[0]: dog23=bird209=cat25=cat99
Index[ 1 ]: dog23=bird209=cat25=cat100
Index[ 2 ]: dog23=bird209=cat25=cat449
...etc
This is my code but so far it doesn't work. The last double loop is to compare if the elements are equal. If they are take line 1 from FILE ONE and append element 2 from FILE TWO. Could anyone please tell me what I'm doing wrong?@array = (); $NAME = "$ARGV[0]"; #FILE 1. open NAME or die "No file 0B $NAME\n"; $NAME1 = "$ARGV[1]"; #FILE 2. open NAME1 or die "No file C0 $NAME1\n"; @array = <NAME>; $cc = scalar @array; #FILE 1 array. @AoA = (); for $i ( 0 .. $cc ) { $line = <>; $line =~ s/=/ /g; chomp $line; $AoA[$i] = [ split ' ', $line ]; } #FILE 2 array. @temp = <NAME1>; $cd = scalar @temp; @AoB = (); for $k ( 0 .. $cd ) { $line2 = $temp[$k]; $line2 =~ s/=/ /g; chomp $line2; $AoB[$k] = [ split ' ', $line2 ]; } close NAME; close NAME1; @NEW = (); $x = 0; $y = 0; for $aref ( @AoA) { for $bref (@AoB) { if ($aref[-1] eq $bref[0]) { my $z = 0; foreach $AoA (@AoA) { push @{ $NEW[$y] }, $aref[$z]; $z++; } push @{ $NEW[$y] }, $bref[-1]; $y++; } else {} } } open (OUTFILE, ">V"); print OUTFILE \@NEW; close OUTFILE;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Help needed please: Data Manipulation in 2D Array
by pc88mxer (Vicar) on May 05, 2008 at 05:27 UTC | |
|
Re: Help needed please: Data Manipulation in 2D Array
by ikegami (Patriarch) on May 05, 2008 at 05:42 UTC | |
by Syntenty (Initiate) on May 05, 2008 at 06:00 UTC | |
by ikegami (Patriarch) on May 05, 2008 at 06:07 UTC | |
by Syntenty (Initiate) on May 05, 2008 at 06:53 UTC | |
by ikegami (Patriarch) on May 05, 2008 at 07:34 UTC | |
| |
|
Re: Help needed please: Data Manipulation in 2D Array
by GrandFather (Saint) on May 05, 2008 at 22:42 UTC |