hotkitty wrote:
Hi,
Hello,
I have two arrays, as follows: Array1=( date 11/01/2008 newstuff1, date 10/27/2008 newstuff2, date 10/24/2008 newstuff3 ) Array2=( date 11/01/2008 oldstuff1, date 10/31/2008 oldstuff2, date 10/30/2008 oldstuff3, date 10/29/2008 oldstuff4, date 10/28/2008 oldstuff5, date 10/27/2008 oldstuff6, date 10/26/2008 oldstuff7, date 10/25/2008 oldstuff8, date 10/24/2008 oldstuff9, date 10/23/2008 oldstuff10 ) How do I combine the arrays so that the the "newstuff" in array1 gets appended only to an item in array2 if the dates match? In other words: Array3=( date 11/01/2008 oldstuff1 and newstuff1, date 10/31/2008 oldstuff2, date 10/30/2008 oldstuff3, date 10/29/2008 oldstuff4, date 10/28/2008 oldstuff5, date 10/27/2008 oldstuff6 and newstuff2, date 10/26/2008 oldstuff7, date 10/25/2008 oldstuff8, date 10/24/2008 oldstuff9 and newstuff3, date 10/23/2008 oldstuff10 ) All I can figure out is how to append array2 onto array1 and vice versa, but that just doesn't work for my project.
$ perl -le' my @Array1 = ( "date 11/01/2008 newstuff1", "date 10/27/2008 newstuff2", "date 10/24/2008 newstuff3", ); my @Array2 = ( "date 11/01/2008 oldstuff1", "date 10/31/2008 oldstuff2", "date 10/30/2008 oldstuff3", "date 10/29/2008 oldstuff4", "date 10/28/2008 oldstuff5", "date 10/27/2008 oldstuff6", "date 10/26/2008 oldstuff7", "date 10/25/2008 oldstuff8", "date 10/24/2008 oldstuff9", "date 10/23/2008 oldstuff10", ); LINE: for my $line ( @Array2 ) { for ( @Array1 ) { if ( substr( $line, 5, 10 ) eq substr( $_, 5, 10 ) ) { $line .= " and " . substr $_, 16; next LINE; } } } print for @Array2; ' date 11/01/2008 oldstuff1 and newstuff1 date 10/31/2008 oldstuff2 date 10/30/2008 oldstuff3 date 10/29/2008 oldstuff4 date 10/28/2008 oldstuff5 date 10/27/2008 oldstuff6 and newstuff2 date 10/26/2008 oldstuff7 date 10/25/2008 oldstuff8 date 10/24/2008 oldstuff9 and newstuff3 date 10/23/2008 oldstuff10 John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/