Home > Archive > PERL Miscellaneous > June 2004 > if statement w/hash and subroutines
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
if statement w/hash and subroutines
|
|
| Mark Connell 2004-06-25, 6:48 pm |
| Hello,
I am using a subroutine multiple times. Depending on the type of hash
I am sending to teh subroutine, I am trying to output different things
to a file. I was wondering how I can do this--I tried creating an if
statement using the name of 1 type of input to output a certain way,
and else for the other type of input... but I was unable to do this
correctly. If I have two diff hashes (ex %1 and %2) that will be used
in my subroutine, can I do something like inside the subroutine:
if (@_ eq %1) {print this}
elseif (@_ eq %2) {print this}
else {...}
That didnt work for me, but I was wondering if the idea was reasonable
or if I should try something different.
Thanks for you time
Mark
| |
| Anno Siegel 2004-06-25, 6:48 pm |
| Mark Connell <mhc5@duke.edu> wrote in comp.lang.perl.misc:
> Hello,
> I am using a subroutine multiple times. Depending on the type of hash
> I am sending to teh subroutine, I am trying to output different things
> to a file. I was wondering how I can do this--I tried creating an if
> statement using the name of 1 type of input to output a certain way,
> and else for the other type of input... but I was unable to do this
> correctly. If I have two diff hashes (ex %1 and %2) that will be used
> in my subroutine, can I do something like inside the subroutine:
>
> if (@_ eq %1) {print this}
> elseif (@_ eq %2) {print this}
> else {...}
%1 and %2 aren't legal hash names in Perl. Please show your real
code. How are the hashes passed to the sub?
> That didnt work for me, but I was wondering if the idea was reasonable
> or if I should try something different.
Depending on how you pass the hash to the sub, Perl may not even know
you gave it a hash.
You could mark the hashes themselves by adding a particular key (and
optionally value) that distinguishes it from other hashes.
Anno
| |
| Mark Connell 2004-06-25, 6:48 pm |
| I guess here are the relevant bits of code...
sub MICe {
%Seq=@_;
lots of calculations...then I print to a file.
I tried to include the printing by doing this:
if ( @_ == %Newsq) {
print DAA so on to a file.
}
else {print something else}
}
I later cued the subroutine by this line:
MICe(%Newsq);
or
MICe(%Seqac);
I think that includes all the relevant code. Would references be
helpful? I haven't learned much about them yet so I'm not sure if
they would be.
Thanks again
Mark
| |
| Jeff 'japhy' Pinyan 2004-06-25, 6:48 pm |
| [posted & mailed]
On 22 Jun 2004, Mark Connell wrote:
> if ( @_ == %Newsq) {
Suffice to say, you cannot compare aggregate data structures (arrays or
hashes) in this manner. You can use the Data::Compare module, though, or
you can determine "same-ness" in your own way (but that can be tricky).
--
Jeff Pinyan RPI Acacia Brother #734 RPI Acacia Corp Secretary
"And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
| |
| Joe Smith 2004-06-25, 6:48 pm |
| Mark Connell wrote:
> MICe(%Newsq);
> or
> MICe(%Seqac);
You'd be better off using
MICe(\%Newsq);
or
MICe(\%Seqac);
and change sub MICe to expect a reference to a hash.
-Joe
| |
|
| "Mark Connell" <mhc5@duke.edu> wrote in message
news:64211cc6.0406220613.56a07862@posting.google.com...
>
> sub MICe {
>
> %Seq=@_;
> lots of calculations...then I print to a file.
> I tried to include the printing by doing this:
> if ( @_ == %Newsq) {
> print DAA so on to a file.
> }
> else {print something else}
> }
>
>
> I later cued the subroutine by this line:
>
> MICe(%Newsq);
> or
> MICe(%Seqac);
>
> I think that includes all the relevant code. Would references be
> helpful? I haven't learned much about them yet so I'm not sure if
> they would be.
if you hardcode the 2 possible hashes (for example by using refs),
you are just making this more difficult to maintain.
rather, add an argument to specify the different types.
MICe(1,%Newsq);
MICe(0,%Seqac);
sub MICe {
my ($action,%Seq)=@_;
...
if ($action) {
...
} else {
...
}
}
gnari
|
|
|
|
|