For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2006 > foreach loop









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 foreach loop
Irfan Sayed

2006-07-21, 6:57 pm

Hi,

I am executing following code.

foreach (@vob_repl_list)

{

print $_;

`$MT chreplica -host puvob02 $_`;

}

but I am not getting the value of $_ in the command `$MT chreplica -host
puvob02 $_`; if I print the value of $_ then it's printing fine.

but in command it's not taking that value. so my execution of command is
failing.

can u plz help.

regards

irfan.


Paul Lalli

2006-07-21, 6:57 pm

Irfan Sayed wrote:
> I am executing following code.
>
> foreach (@vob_repl_list)
>
> {
>
> print $_;
>
> `$MT chreplica -host puvob02 $_`;


Please read: `perldoc -q backticks`
Found in /opt2/Perl5_8_4/lib/perl5/5.8.4/pod/perlfaq8.pod
What's wrong with using backticks in a void context?

>
> }
>
> but I am not getting the value of $_ in the command `$MT chreplica -host
> puvob02 $_`; if I print the value of $_ then it's printing fine.
>
> but in command it's not taking that value.


You're wrong.

> so my execution of command is failing.


If that's the case, you've misdiagnosed your problem. By what means
are you coming to the conclusion that your problem is that the value of
$_ suddenly disappears between the print line and the backticks?

> can u plz help.


Would you please stop typing like a 10 year old discovering the
internet for the first time? If your posts are bad enough, this style
of typing makes you look like an idiot. It does not require a great
deal of effort nor time to type the words "you" and "please".

Paul Lalli

Stephen Kratzer

2006-07-21, 6:57 pm

On Friday 21 July 2006 08:59, Sayed, Irfan (Irfan) wrote:
> Hi,
>
> I am executing following code.
>
> foreach (@vob_repl_list)
>
> {
>
> print $_;
>
> `$MT chreplica -host puvob02 $_`;
>
> }
>
> but I am not getting the value of $_ in the command `$MT chreplica -host
> puvob02 $_`; if I print the value of $_ then it's printing fine.
>
> but in command it's not taking that value. so my execution of command is
> failing.
>
> can u plz help.
>
> regards
>
> irfan.


$_ is being interpolated, but if you don't do anything with the returned
output (like print it), you won't see any results. If that isn't the case,
it's possible that @vob_repl_list is empty.
Irfan Sayed

2006-07-24, 3:56 am

Hi All,

Following is the code which I am executing.

my $fname3 =3D "/tmp/vob_repl_list_test1";
open(FILE2, $fname3);
my @vob_repl_list =3D <FILE2>;
close(FILE2);
foreach my $a(@vob_repl_list)
{
`$MT chreplica -host puvob02 $a`;
}

I am not getting the proper value of $a in the command `$MT chreplica
-host puvob02 $a`;

I know that array @vob_repl_list is not empty.=20

plz help

regards
irfan.




-----Original Message-----
From: Stephen Kratzer [mailto:kratzers@pa.net]=20
Sent: Friday, July 21, 2006 6:46 PM
To: beginners@perl.org
Subject: Re: foreach loop

On Friday 21 July 2006 08:59, Sayed, Irfan (Irfan) wrote:
> Hi,
>
> I am executing following code.
>
> foreach (@vob_repl_list)
>
> {
>
> print $_;
>
> `$MT chreplica -host puvob02 $_`;
>
> }
>
> but I am not getting the value of $_ in the command `$MT chreplica=20
> -host
> puvob02 $_`; if I print the value of $_ then it's printing fine.
>
> but in command it's not taking that value. so my execution of command=20
> is failing.
>
> can u plz help.
>
> regards
>
> irfan.


$_ is being interpolated, but if you don't do anything with the returned
output (like print it), you won't see any results. If that isn't the
case, it's possible that @vob_repl_list is empty.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org For additional
commands, e-mail: beginners-help@perl.org <http://learn.perl.org/>
<http://learn.perl.org/first-response>


Dr.Ruud

2006-07-24, 7:56 am

"Sayed, Irfan (Irfan)" schreef:

> Following is the code which I am executing.


Missing:

use warnings ;
use strict ;

>
> my $fname3 = "/tmp/vob_repl_list_test1";
> open(FILE2, $fname3);


Missing: error checking.

open my $fh3, '<', $fname3 or die "open $fname3, stopped $!" ;


> my @vob_repl_list = <FILE2>;
> close(FILE2);



It might be a good idea to have a robust file2array() around, for
answers in this list.
Or is that too PHP?

First try:

#!/usr/bin/perl
# Script-ID: f2a.pl

use warnings ;
use strict ;

sub file2array ($\@;$)
{
my ($fname, $aref, $src_line) = @_ ;

my $fh ;
$src_line ? do {open $fh, '<', $fname
or die "line $src_line: open '$fname': $!\n"}
: do {open $fh, '<', $fname
or die "open '$fname': $!\n"} ;

@$aref = <$fh> ;

$src_line ? do {close $fh
or die "line $src_line: close '$fname': $!\n"}
: do {close $fh
or die "close '$fname': $!\n"} ;
return 1 ;
}

my @test ;

file2array $0, @test, __LINE__ or die ;
print @test, "\n" ;

file2array "$0.bad", @test or die ;
print @test, "\n" ;
__END__


> foreach my $a(@vob_repl_list)
> {
> `$MT chreplica -host puvob02 $a`;
> }


It is not a good idea to use $a or $b, see `perldoc perlvar`.


> I am not getting the proper value of $a in the command `$MT chreplica
> -host puvob02 $a`;
>
> I know that array @vob_repl_list is not empty.


Try again with warnings and strictures on, and always have them on from
now on.

--
Affijn, Ruud

"Gewoon is een tijger."


Paul Lalli

2006-07-24, 7:56 am

Irfan Sayed wrote:
> Following is the code which I am executing.
>
> my $fname3 = "/tmp/vob_repl_list_test1";
> open(FILE2, $fname3);
> my @vob_repl_list = <FILE2>;
> close(FILE2);
> foreach my $a(@vob_repl_list)
> {
> `$MT chreplica -host puvob02 $a`;
> }
>
> I am not getting the proper value of $a in the command `$MT chreplica
> -host puvob02 $a`;


How do you know that? What values of $a *are* you getting? How do you
know *that*?

>
> I know that array @vob_repl_list is not empty.


Really? How do you know that? Certainly not by the code above you
don't!

How many times on this list have you been asked now to produce a
short-but-complete script WITH SAMPLE INPUT AND OUTPUT!? Why is that
so damned hard for you to do? We are not asking much. In fact, we're
TRYING to help you, but you continue to be difficult about simplistic
stuff like this, hence making it hard for *anyone* to help you
effectively.

> plz help


"plz" help yourself, and help us help you. We can't do this alone.
You need to put some effort into this.

Now, as a random guess, have you considered the fact that in that loop,
$a has a newline appended to it? Does your command have a problem with
arguments having newlines attached? Have you tried chomping $a before
using it?

Paul Lalli

unique user

2006-07-25, 3:57 am


> I know that array @vob_repl_list is not empty.
>


How come u know that array is not empty? Do u know the values in it?
use Data::Dumper to see the array.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com