in reply to Re: Reg: Performance
in thread Reg: Performance

Thank you Salva. Now I have a code like

my %b; while (DUMP_B) { chomp; my ($id, $ac) = split /\|/, $_; push @{$b{$id}}, $ac; } my $readID; while(defined($readID = DUMP_A)){ #chomp; $readID =~ s!\s+!!g; my $ac = $b{$readID} || []; #print "$readID: ", join(', ', @$ac), "\n"; my $arrSize = scalar @$ac; if($arrSize > 0 ){ for (my $i = 0; $i < $arrSsize ; $i++) { print OUTPUT "$readID|@$ac[$i] \n"; } } }
This gives the expected output. Could you please explain me on 'my $ac = $b{$readID} || [];' Thank you once again and for all the Monks.

Replies are listed 'Best First'.
Re^3: Reg: Performance
by salva (Canon) on Oct 29, 2010 at 08:14 UTC
    $b{$readID} || []
    When there are no entries for $readID in DUMP_B, $b{$readID} becomes undef, appending || [] handles this special case replacing undef by a reference to an empty array so that we can dereference it later as @$ac.

    A simpler version of the code follows:

    my %b; open(B, "<" . DUMP_B) || die("Could not open file \n"); while (<B>) { chomp; my ($id, $ac) = split /\|/, $_; push @{$b{$id}}, $ac; } open( OUTPUT, '>', 'INACTIVE_LIST' ) || die "Could not write to file\n +"; open(A, "<" . DUMP_A) || die("Could not open file \n"); while(<A>) { chomp; s/\s+//g; if ($b{$_}) { for my $ac (@{$b{$_}}) { print OUTPUT "$_|$ac\n" } } }
    BTW, when programming in Perl it is very unusual to use C-like (or Java-like) for loops. Instead, for (@array) { ... } and for (0..$top) { ... } are used.

      Thank you Salva for your help and also for this clarification.

      - Thank you all Monks for spent your valuable time on this.