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:


dog23=bird209=cat25
dog33=bird211=cat45
dog53=bird26=cat67

The second file format is:

cat25=cat99
cat25=cat100
cat25=cat449
cat33=cat42
cat55=cat109

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=cat99

I'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

Here I my code:
@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;
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?

In reply to Help needed please: Data Manipulation in 2D Array by Syntenty

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.