put perl command within if-else
put perl command within if-else
am 27.08.2007 17:15:11 von Jie
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 () {
} else { ## this list is in a hash created previously
foreach $key (%LIST_HASH) {
}
!!!!do something here!!!;
}
Re: put perl command within if-else
am 27.08.2007 17:47:56 von Paul Lalli
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 () {} 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
Re: put perl command within if-else
am 27.08.2007 19:36:24 von Jie
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 () {} 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
Re: put perl command within if-else
am 28.08.2007 09:18:59 von Joe Smith
Jie wrote:
> Thank you very much, Paul! I just learned from you that "shift" is one
> to get a passed parameter!
You should also learn TIMTOWTDI (There Is More Than One Way To Do It).
sub process {
my ($first,$second,@rest) = @_;
...
}