put perl command within if-else

Discussion in 'Perl Misc' started by Jie, Aug 27, 2007.

  1. Jie

    Jie Guest

    Hi,

    I want to process a list. However, this list could exist as a provided
    file, or in a hash created. I am thinking to use the following code,
    first to find if this list exists in a file or in a hash, but this
    code does NOT work.... Do I need to put "do something here" into a
    function and then call this function? I am not very comfortable with
    function though...

    Your insight is appreciated!

    jie

    ==============my piece of code==========
    if ($list_file) {
    open (LIST, "< $list_file") or die "Can't open file";
    while (<LIST>) {
    } else { ## this list is in a hash created previously
    foreach $key (%LIST_HASH) {
    }

    !!!!do something here!!!;
    }
     
    Jie, Aug 27, 2007
    #1
    1. Advertising

  2. Jie

    Paul Lalli Guest

    On Aug 27, 11:15 am, Jie <> wrote:
    > I want to process a list. However, this list could exist as a
    > provided file, or in a hash created. I am thinking to use the
    > following code, first to find if this list exists in a file or
    > in a hash, but this code does NOT work.... Do I need to put "do
    > something here" into a function and then call this function?
    > I am not very comfortable with function though...


    > ==============my piece of code==========
    > if ($list_file) {
    > open (LIST, "< $list_file") or die "Can't open file";
    > while (<LIST>) {} else { ## this list is in a hash created previously
    >
    > foreach $key (%LIST_HASH) {


    You meant:
    foreach my $key (keys %LIST_HASH) {

    otherwise, you're iterating over both the keys and the values

    >
    > }
    >
    > !!!!do something here!!!;
    > }



    What you want is not possible. You cannot intersperce a while or
    foreach loop within the if statement. All blocks must fully nest.
    That is, one block cannot end until all blocks started within it have
    ended.

    So, yes, the best way to handle what you want is to create a
    subroutine and call that subroutine twice, once in the while loop,
    once in the foreach.

    For example:
    if ($list_file) {
    open my $LIST, '<', $list_file or die "Can't open $list_file: $!";
    while (my $line = <$LIST>) {
    chomp $line;
    process($line);
    }
    } else { ## this list is in a hash created previously
    foreach my $key (keys %LIST_HASH) {
    process($key);
    }
    }

    sub process {
    my $item = shift;
    #do whatever you want with the hash key or file line
    print "Item: $item\n";
    }


    If, as you say, you are not comfortable with subroutines, you should
    read:
    perldoc perlsub

    Paul Lalli
     
    Paul Lalli, Aug 27, 2007
    #2
    1. Advertising

  3. Jie

    Jie Guest

    Thank you very much, Paul! I just learned from you that "shift" is one
    to get a passed parameter!

    On Aug 27, 10:47 am, Paul Lalli <> wrote:
    > On Aug 27, 11:15 am, Jie <> wrote:
    >
    > > I want to process a list. However, this list could exist as a
    > > provided file, or in a hash created. I am thinking to use the
    > > following code, first to find if this list exists in a file or
    > > in a hash, but this code does NOT work.... Do I need to put "do
    > > something here" into a function and then call this function?
    > > I am not very comfortable with function though...
    > > ==============my piece of code==========
    > > if ($list_file) {
    > > open (LIST, "< $list_file") or die "Can't open file";
    > > while (<LIST>) {} else { ## this list is in a hash created previously

    >
    > > foreach $key (%LIST_HASH) {

    >
    > You meant:
    > foreach my $key (keys %LIST_HASH) {
    >
    > otherwise, you're iterating over both the keys and the values
    >
    >
    >
    > > }

    >
    > > !!!!do something here!!!;
    > > }

    >
    > What you want is not possible. You cannot intersperce a while or
    > foreach loop within the if statement. All blocks must fully nest.
    > That is, one block cannot end until all blocks started within it have
    > ended.
    >
    > So, yes, the best way to handle what you want is to create a
    > subroutine and call that subroutine twice, once in the while loop,
    > once in the foreach.
    >
    > For example:
    > if ($list_file) {
    > open my $LIST, '<', $list_file or die "Can't open $list_file: $!";
    > while (my $line = <$LIST>) {
    > chomp $line;
    > process($line);
    > }} else { ## this list is in a hash created previously
    >
    > foreach my $key (keys %LIST_HASH) {
    > process($key);
    > }
    >
    > }
    >
    > sub process {
    > my $item = shift;
    > #do whatever you want with the hash key or file line
    > print "Item: $item\n";
    >
    > }
    >
    > If, as you say, you are not comfortable with subroutines, you should
    > read:
    > perldoc perlsub
    >
    > Paul Lalli
     
    Jie, Aug 27, 2007
    #3
    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. Replies:
    25
    Views:
    675
    MonkeeSage
    Oct 1, 2006
  2. kj
    Replies:
    15
    Views:
    560
    Lawrence D'Oliveiro
    May 23, 2009
  3. A
    Replies:
    8
    Views:
    810
    James Kanze
    Aug 28, 2010
  4. Gergely Kontra
    Replies:
    16
    Views:
    232
    Devin Mullins
    Sep 20, 2005
  5. Replies:
    2
    Views:
    164
    Tad McClellan
    Mar 26, 2005
Loading...

Share This Page