how about this one foreach...

K

Ken Sington

ok, how about this one:

foreach ("remote_host",
"remote_user",
"time",
"request",
"status_number",
"bytes_sent",
"referer",
"user_agent",
"server_name",
"post_connection_status",
"download_time"
){
$_ =~ s/_/ /; # with this line, no I get no output!
print "$_\n";
}


it gives no output!
 
B

Bob Walton

Ken said:
ok, how about this one:

foreach ("remote_host",
"remote_user",
"time",
"request",
"status_number",
"bytes_sent",
"referer",
"user_agent",
"server_name",
"post_connection_status",
"download_time"
){
$_ =~ s/_/ /; # with this line, no I get no output!
print "$_\n";
}


it gives no output!

Hmmmm...I get an output. It says:

"Modification of a read-only value attempted at junk459.pl line 13."

Your foreach loop is aliasing $_ to the elements of the given list, and
then attempting to modify said elements via the alias. Since those
values are read-only, you get the message above.
 
K

Ken Sington

Purl said:
Ken Sington wrote:





#!perl

@Array = ("remote_host", "remote_user", "time", "request", "status_number",
"bytes_sent", "referer", "user_agent", "server_name",
"post_connection_status", "download_time");

for (@Array)
{ $_ =~ tr/_/ /; print "$_\n";}


Purl Gurl


ach! looks like I'm trying to be lazier than larry.
 
B

Bart Van der Donck

Ken Sington wrote...
foreach ("remote_host",
"remote_user",
"time",
"request",
"status_number",
"bytes_sent",
"referer",
"user_agent",
"server_name",
"post_connection_status",
"download_time"
){
$_ =~ s/_/ /; # with this line, no I get no output!
print "$_\n";
}
it gives no output!

$_ is a readonly variable in the loop. It appears that this is caused
by the way your array is assigned (somewhat unusual in my experience).

If you want to modify $_, use something like:

my @array= ("remote_host", "remote_user", "time", "request",
"status_number", "bytes_sent", "referer", "user_agent", "server_name",
"post_connection_status", "download_time");
foreach (@array){
$_ =~ s/\_/ /;
print "$_\n";
}

I bet you also want
$_ =~ s/\_/ /i;
in stead of
$_ =~ s/\_/ /;
(the i would cause all underscores to be replaced by spaces)

Hope this helps
Bart
 
T

Tintin

Ken Sington said:
ok, how about this one:

Please don't start a new thread for an existing post.
foreach ("remote_host",
"remote_user",
"time",
"request",
"status_number",
"bytes_sent",
"referer",
"user_agent",
"server_name",
"post_connection_status",
"download_time"
){
$_ =~ s/_/ /; # with this line, no I get no output!
print "$_\n";
}


it gives no output!

Absolute rubbish. You would have got the message similar to:

"Modification of a read-only value attempted at foo.pl line 13."

Please ensure you are *very* specific with your question, your code sample,
your input and the expected results.

#!/usr/bin/perl
use strict;
use warnings;

my @items = qw(remote_host remote_user ...);

foreach (@items) {
tr /_/ /;
print "$_\n";
}
 
A

Anno Siegel

Tintin said:
Ken Sington said:
ok, how about this one:

Please don't start a new thread for an existing post.
foreach ("remote_host",
"remote_user",
[...]
"download_time"
){
$_ =~ s/_/ /; # with this line, no I get no output!
print "$_\n";
}

it gives no output!

Absolute rubbish. You would have got the message similar to:

"Modification of a read-only value attempted at foo.pl line 13."

Please ensure you are *very* specific with your question, your code sample,
your input and the expected results.

#!/usr/bin/perl
use strict;
use warnings;

my @items = qw(remote_host remote_user ...);

foreach (@items) {
tr /_/ /;
print "$_\n";
}

The auxiliary array can also be anonymous:

foreach ( @{[ qw( remote_host remote_user ...) ]} ) { ...

Anno
 
G

gnari

in case you really want to use the literal
list in the foreach statement, just use
a non-aliased temporary variable if you
need to modify the index value.
foreach ("remote_host",
foreach my $x ("remote_host",
...
$_ =~ s/_/ /; # with this line, no I get no output!
($_ = $x) =~ s/_/ /;

gnari
 
L

Lukas Mai

Bart Van der Donck schrob:
[...]
I bet you also want
$_ =~ s/\_/ /i;
in stead of
$_ =~ s/\_/ /;
(the i would cause all underscores to be replaced by spaces)

No, it wouldn't; /i is for case insensitive matching. You mean /g.
Also escaping "_" is completely useless. And "$_ =~" is implicit, so we
can shorten this to
s/_/ /g;
(or simply tr/_/ /, as mentioned elsewhere).

HTH, Lukas
 
A

Anno Siegel

gnari said:
in case you really want to use the literal
list in the foreach statement, just use
a non-aliased temporary variable if you
need to modify the index value.

The variable in "foreach ( ... )" is always an aliased to list elements,
whether it's named $_ or otherwise.
foreach my $x ("remote_host",

That gets the same complaint about read-only values.

Anno
 
G

gnari

Anno Siegel said:
The variable in "foreach ( ... )" is always an aliased to list elements,
whether it's named $_ or otherwise.


That gets the same complaint about read-only values.

that is why I made a change to another line a bit below,
which you snipped away, namely:
($_ = $x) =~ s/_/ /;
so there!

gnari
 
J

Jay Tilton

: : >
:
: in case you really want to use the literal
: list in the foreach statement, just use
: a non-aliased temporary variable if you
: need to modify the index value.
:
: > foreach ("remote_host",
: foreach my $x ("remote_host",
:
: > ...
: > $_ =~ s/_/ /; # with this line, no I get no output!
: ($_ = $x) =~ s/_/ /;

We don't want to step on $_ if it's the loop variable somewhere else.

(local $_ = $x) =~ s/_/ /;
 
M

Michele Dondi

foreach ("remote_host",
"remote_user",

(You'd better, IMO) use

for ( qw/remote_host
remote_user
...
download_time/) {
$_ =~ s/_/ /; # with this line, no I get no output!

$_ =~

is not necessary,

s/_/ /;

would be fine instead, if you *could* modify $_ which you can't, as
others have already duly told you. Also, if you could,

tr/_/ /;

would be better. Oh, and BTW, I suppose that as a general rule you
wouldn't want to substitute only the *first* underscore, would you?

To circumvent the problem you're facing you can do e.g.

local $_=$_;

as soon as you enter the loop. But definitely what you're trying to do
is awkward...
print "$_\n";

Aargh!! Set $\="\n" and

print;

To sum up things, for example,

#!/usr/bin/perl -l

use strict;
use warnings;

print for map {local $_=$_; tr/_/ /; $_} qw/a_bc a_bcd/;

__END__

does work as expected...


HTH,
Michele
 
G

gnari

Jay Tilton said:
: : >
:
: in case you really want to use the literal
: list in the foreach statement, just use
: a non-aliased temporary variable if you
: need to modify the index value.
:
: > foreach ("remote_host",
: foreach my $x ("remote_host",
:
: > ...
: > $_ =~ s/_/ /; # with this line, no I get no output!
: ($_ = $x) =~ s/_/ /;

We don't want to step on $_ if it's the loop variable somewhere else.

(local $_ = $x) =~ s/_/ /;

that's not very likely, as $_ was the OP's original loop variable.
I chose to do it this way just to minimize the changes from the OP's
code and make the difference clear.

I would actually prefer to use another lexical instead of $_ anyways;
although I am not a 'local' hater, this would not be a good example
of it's use, IMHO.

gnari
 
B

Ben Morrow

Quoth "gnari said:
that's not very likely, as $_ was the OP's original loop variable.

If $_ is an implicit loop variable it is implicitly localized to the
loop. A frequent gotcha is that 'while ( said:
I chose to do it this way just to minimize the changes from the OP's
code and make the difference clear.

I would have gone the other way:

for (...) {
...
(my $x = $_) =~ s/_/ /;
}

Not that it really makes any difference :).
of it's use, IMHO.
^
grrr

Ben
 
G

gnari

Ben Morrow said:
Quoth "gnari" <[email protected]>:
I would have gone the other way:

for (...) {
...
(my $x = $_) =~ s/_/ /;
}

Not that it really makes any difference :).

no, I agree i should have done it that way. I would have
had to change the print statement too, but at least the
changes would have been together

not native english speaker, but sorry.

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top