if statement w/hash and subroutines

Discussion in 'Perl Misc' started by Mark Connell, Jun 22, 2004.

  1. Mark Connell

    Mark Connell Guest

    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
    Mark Connell, Jun 22, 2004
    #1
    1. Advertising

  2. Mark Connell

    Anno Siegel Guest

    Mark Connell <> 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
    Anno Siegel, Jun 22, 2004
    #2
    1. Advertising

  3. Mark Connell

    Mark Connell Guest

    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
    Mark Connell, Jun 22, 2004
    #3
  4. [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)
    Jeff 'japhy' Pinyan, Jun 22, 2004
    #4
  5. Mark Connell

    gnari Guest

    "Mark Connell" <> wrote in message
    news:...
    >
    > 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
    gnari, Jun 24, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ReaprZero

    References and subroutines

    ReaprZero, Dec 4, 2003, in forum: Perl
    Replies:
    1
    Views:
    438
    Gunnar Hjalmarsson
    Dec 4, 2003
  2. =?Utf-8?B?UGF1bA==?=

    Multiple Page_Load subroutines and User Controls

    =?Utf-8?B?UGF1bA==?=, Oct 26, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    635
    //Rutger Smit
    Oct 26, 2005
  3. rp
    Replies:
    1
    Views:
    499
    red floyd
    Nov 10, 2011
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    598
    David A. Black
    Jul 2, 2008
  5. Derek Basch
    Replies:
    5
    Views:
    110
    Dr.Ruud
    May 10, 2006
Loading...

Share This Page