Visit Our UNIX and Linux User Community


Nested foreach in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nested foreach in perl
# 1  
Old 09-25-2009
Java Nested foreach in perl

I have two arrays

@nextArray contains some files like

\main\1\Xul.xml@@\main\galileo_integration_sjc\0
\main\1\PortToStorageDialog.xml@@\main\galileo_integration_sjc\0
.
.
.
\main\1\PreferencesDialog.xml@@\main\galileo_integration_sjc\0


@otherArray contains some files like

\main\1\Xul.xml@@\main\galileo_integration_sjc\LATEST
\main\1\PortToStorageDialog.xml@@\main\galileo_integration_sjc\LATEST
.
.
.
\main\1\PreferencesDialog.xml@@\main\galileo_integration_sjc\LATEST

I need to to pick first file from @nextArray and first file from @otherArray and compare using a diff statement. Then 2nd and 2nd....last and last to print the difference for all the file elements. No of file elements are same.
foreach my $one (@nextArray){
foreach my $two (@otherArray){
system ("cleartool diff @nextThing @otherThing");
}
}

But this picks the last from @nextArray and last from @otherArray and giving the comparison. I believe that i am missing break in the inner loop. Please help me. Output must be like:
\main\1\Xul.xml@@\main\galileo_integration_sjc\0 and
\main\1\Xul.xml@@\main\galileo_integration_sjc\LATEST are compared and the diff is printed.
I am new to perl, please help me. Thanks in advance.
# 2  
Old 09-25-2009
assuming your filenames are not just examples but actually named '0' and 'LATEST', and paths are identical, you only need one array, and one loop:
Code:
foreach my $next (@nextArray) {
    # replace the string '0' at end of line with 'LATEST'
    my $latest =~ s/0$/LATEST/;
    system(cleartool diff $next $latest);
}

# 3  
Old 09-28-2009
Nested foreach in perl

Thanks a lot varontron!!! I got that.

Previous Thread | Next Thread
Test Your Knowledge in Computers #315
Difficulty: Easy
To bypass US Munitions Export Laws, the creator of the PGP published all the source code in a binary format.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print perl hash value in foreach loop

Experts - Any advice on how to get a hash value in a foreach loop? Values print correctly on standalone print statements, but I can't access value in foreach loop. See sample code below and thanks in advance. foreach my $z (sort keys %hash) { for $y (@{$hash{$z}}) { print "$z... (6 Replies)
Discussion started by: timj123
6 Replies

2. UNIX for Dummies Questions & Answers

Executing nested loops+foreach

It's been a while since I used csh formatting and I am having a little bit of trouble with a few things. Things seem so much easier to execute in Matlab, however I need to do this on the terminal because of the programs I am trying to interact with. So here's what I want to do: I have a file... (0 Replies)
Discussion started by: katia
0 Replies

3. Shell Programming and Scripting

PERL foreach & open not working together

My script is as below: my $tile_list = `egrep "FCFP_TILE_LIST.*=" ${BudgetDir}/tile.params | sed -e 's/FCFP_TILE_LIST//' | sed -e 's/=//'`; print "Tile List = ".$tile_list."\n"; my @tiles = split(/\s+/, $tile_list); $unconst_out = "${DestDir}/Unconstrained_ports.rpt"; $check_tim_out =... (2 Replies)
Discussion started by: kuchi7
2 Replies

4. Shell Programming and Scripting

Perl - Sort and Count foreach line

Can any one please help I am trying to sort each item in every line and count the common (non case sensitive) and at the end printing all the unique alphabetically. Here is what i did ... I can print all the lines but struck to sort each line some were: I am getting the output as: I... (5 Replies)
Discussion started by: sureshcisco
5 Replies

5. Shell Programming and Scripting

Perl - nested substitutions

How can I nest substitutions ? My solution just seems cheap ... sample data Cisco Catalyst Operating System Software, Version 235.5(18) Cisco Catalyst Operating System Software, Version 17.6(7) Cisco Catalyst Operating System Software, Version 19.6(7) Cisco Catalyst Operating System... (1 Reply)
Discussion started by: popeye
1 Replies

6. Programming

PERL, search and replace inside foreach loop

Hello All, Im a Hardware engineer, I have written this script to automate my job. I got stuck in the following location. CODE: .. .. ... foreach $key(keys %arr_hash) { my ($loc,$ind,$add) = split /,/, $arr_hash{$key}; &create_verilog($key, $loc, $ind ,$add); } sub create_verilog{... (2 Replies)
Discussion started by: riyasnr007
2 Replies

7. UNIX for Dummies Questions & Answers

Foreach loop to run a perl script on multiple files

Hi, I have thousands of files in a directory that have the following 2 formats: 289620178.aln 289620179.aln 289620180.aln 289620183.aln 289620184.aln 289620185.aln 289620186.aln 289620187.aln 289620188.aln 289620189.aln 289620190.aln 289620192.aln.... and: alnCDS_1.fasta (1 Reply)
Discussion started by: greptastic
1 Replies

8. Shell Programming and Scripting

Shell Integer with nested foreach

I am scripting in tcsh and here is what I currently have: foreach group (g1 g2 g3 g4) set ppl = `cat $group.file.with.list.of.ppl.in.row.format` set label = 1 @ label += 1 foreach ppls ($ppl) echo $label >> file end end ... (0 Replies)
Discussion started by: bonesy
0 Replies

9. Shell Programming and Scripting

Perl - Iterating a hash through a foreach loop - unexpected results

i've reworked some code from an earlier post, and it isn't working as expected i've simplified it to try and find the problem. i spent hours trying to figure out what is wrong, eventually thinking there was a bug in perl or a problem with my computer. but, i've tried it on 3 machines with the... (5 Replies)
Discussion started by: quantumechanix
5 Replies

Featured Tech Videos