Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how about this one foreach...

Reply
Thread Tools

how about this one foreach...

 
 
Ken Sington
Guest
Posts: n/a
 
      06-13-2004
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!
 
Reply With Quote
 
 
 
 
Bob Walton
Guest
Posts: n/a
 
      06-13-2004
Ken Sington wrote:

> 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.

--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl

 
Reply With Quote
 
 
 
 
Ken Sington
Guest
Posts: n/a
 
      06-13-2004
Purl Gurl wrote:
> Ken Sington wrote:
>
>
>>foreach ("remote_host",

>
>
>>$_ =~ s/_/ /; # with this line, no I get no output!

>
>
>
> #!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.
 
Reply With Quote
 
Bart Van der Donck
Guest
Posts: n/a
 
      06-13-2004
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
 
Reply With Quote
 
Tintin
Guest
Posts: n/a
 
      06-13-2004

"Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
news:(E-Mail Removed)...
> 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";
}




 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      06-13-2004
Tintin <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
> "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
> news:(E-Mail Removed)...
> > 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
 
Reply With Quote
 
gnari
Guest
Posts: n/a
 
      06-13-2004
"Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
news:(E-Mail Removed)...
>


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




 
Reply With Quote
 
Lukas Mai
Guest
Posts: n/a
 
      06-13-2004
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
--
print+74.117.115.116,,qq.\c!..not::.her,Perl=>q$ha cker,$,!($,=$")
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      06-13-2004
gnari <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
> news:(E-Mail Removed)...
> >

>
> 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 ("remote_host",

> foreach my $x ("remote_host",


That gets the same complaint about read-only values.

Anno
 
Reply With Quote
 
gnari
Guest
Posts: n/a
 
      06-13-2004
"Anno Siegel" <(E-Mail Removed)-berlin.de> wrote in message
news:cahi9f$g0$(E-Mail Removed)-Berlin.DE...
> gnari <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> > "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
> > news:(E-Mail Removed)...
> > >

> >
> > 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 ("remote_host",

> > foreach my $x ("remote_host",

>
> 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




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Nike air force one, air force 1, air force one low cut, air force one abdul_razak@indiatimes.com Digital Photography 2 12-31-2008 04:29 PM
Samsung SCD6040 - Anyone have one, used one, or have reviews on one? g wills Digital Photography 0 09-08-2004 08:06 PM
CSS aligning two things on one line with one left and one right news.frontiernet.net HTML 6 04-16-2004 02:44 AM
Using One XSLT and multiple XML Problem (One is XML and another one is XBRL) loveNUNO XML 2 11-20-2003 06:47 AM



Advertisments