johnvandam has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
I came across an unexpected behavior in perl and I would very much like your input on what is happening in my code.
I have an array of which I want to check every element against the keys of a hash containing reference data. Because I want to be able to exclude options (genomes in this example) I want to put a '-' in front of genomes I want to exclude later on. The code below is meant to check if I have provided correct genome names and did not misspell them.
In the first peace of code I am manipulating $_ in a foreach loop. But somehow this ends up manipulating my array @species. The last element which initially contained '-CIOINT' now contains 'CIOINT', the - is removed.
In the code below that I'm doing the same but now I am not manipulating $_. The array is not changed.
I am not working with references so I am very curious as to why the array has changed in the first instance. I'm running perl 5.8.8 on a linux machine. Could someone clarify what happens in the first peace of code? I would be very grateful because it's racking my brain!
John
Below is the isolated code with the faulty and working code in it.
#!/usr/bin/perl use warnings; use strict; my %refspecies = ( HOMSAP => 'Homo sapiens', MUSMUS => 'Mus musculus', CIOINT => 'Ciona intestinalis' ); my @species =('HOMSAP','MUSMUS','-CIOINT'); my @duplicate = @species; # Incorrect working code print "Before processing: @species\n"; foreach (@species) { /^-?(.+)$/; $_ = $1; unless (exists $refspecies{$_}) { # Species not in list die "$_ is not in the species table\n"; } } print "After processing: @species\n"; #Correct working code #Duplicate run with minor code adjustment print "Before processing: @duplicate\n"; foreach (@duplicate) { /^-?(.+)$/; #$_ = $1; Removed this line and changed $_ below in $1. This shoul +dn't matter because I am manipulating $_ in the code above and NOT @s +pecies, but somehow it does! unless (exists $refspecies{$1}) { # Species not in list die "$1 is not in the species table\n"; } } print "After processing: @duplicate\n"; #Output is: #Before processing: HOMSAP MUSMUS -CIOINT #After processing: HOMSAP MUSMUS CIOINT #Before processing: HOMSAP MUSMUS -CIOINT #After processing: HOMSAP MUSMUS -CIOINT # Look at @species element 3. Why is @species changed?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Changing array by changing $_?
by JavaFan (Canon) on Oct 13, 2008 at 12:06 UTC | |
by johnvandam (Acolyte) on Oct 13, 2008 at 12:17 UTC | |
by gone2015 (Deacon) on Oct 13, 2008 at 12:41 UTC | |
by ikegami (Patriarch) on Oct 13, 2008 at 14:01 UTC | |
by GrandFather (Saint) on Oct 13, 2008 at 20:36 UTC | |
by busunsl (Vicar) on Oct 13, 2008 at 12:42 UTC | |
|
Re: Changing array by changing $_?
by wol (Hermit) on Oct 13, 2008 at 13:39 UTC | |
by JavaFan (Canon) on Oct 13, 2008 at 13:56 UTC | |
by repellent (Priest) on Oct 13, 2008 at 17:25 UTC | |
by gone2015 (Deacon) on Oct 13, 2008 at 15:20 UTC | |
by JavaFan (Canon) on Oct 13, 2008 at 15:28 UTC | |
by ikegami (Patriarch) on Oct 13, 2008 at 15:38 UTC | |
| |
by gone2015 (Deacon) on Oct 13, 2008 at 16:10 UTC | |
| |
by ikegami (Patriarch) on Oct 13, 2008 at 14:05 UTC | |
by TGI (Parson) on Oct 14, 2008 at 18:36 UTC | |
|
Re: Changing array by changing $_?
by ig (Vicar) on Oct 13, 2008 at 20:18 UTC | |
|
Re: Changing array by changing $_?
by TGI (Parson) on Oct 14, 2008 at 18:36 UTC |