if statement w/hash and subroutines

M

Mark Connell

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
 
A

Anno Siegel

Mark Connell said:
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
 
M

Mark Connell

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
 
J

Jeff 'japhy' Pinyan

[posted & mailed]

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).
 
G

gnari

Mark Connell said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top