how about this one foreach...

Discussion in 'Perl Misc' started by Ken Sington, Jun 13, 2004.

  1. Ken Sington

    Ken Sington Guest

    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!
    Ken Sington, Jun 13, 2004
    #1
    1. Advertising

  2. Ken Sington

    Bob Walton Guest

    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
    Bob Walton, Jun 13, 2004
    #2
    1. Advertising

  3. Ken Sington

    Ken Sington Guest

    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.
    Ken Sington, Jun 13, 2004
    #3
  4. 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
    Bart Van der Donck, Jun 13, 2004
    #4
  5. Ken Sington

    Tintin Guest

    "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
    news:...
    > 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";
    }
    Tintin, Jun 13, 2004
    #5
  6. Ken Sington

    Anno Siegel Guest

    Tintin <> wrote in comp.lang.perl.misc:
    >
    > "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
    > news:...
    > > 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
    Anno Siegel, Jun 13, 2004
    #6
  7. Ken Sington

    gnari Guest

    "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
    news:...
    >


    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
    gnari, Jun 13, 2004
    #7
  8. Ken Sington

    Lukas Mai Guest

    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$hacker,$,!($,=$")
    Lukas Mai, Jun 13, 2004
    #8
  9. Ken Sington

    Anno Siegel Guest

    gnari <> wrote in comp.lang.perl.misc:
    > "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
    > news:...
    > >

    >
    > 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
    Anno Siegel, Jun 13, 2004
    #9
  10. Ken Sington

    gnari Guest

    "Anno Siegel" <-berlin.de> wrote in message
    news:cahi9f$g0$-Berlin.DE...
    > gnari <> wrote in comp.lang.perl.misc:
    > > "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
    > > news:...
    > > >

    > >
    > > 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
    gnari, Jun 13, 2004
    #10
  11. Ken Sington

    Jay Tilton Guest

    "gnari" <> wrote:

    : "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
    : news:...
    : >
    :
    : 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/_/ /;
    Jay Tilton, Jun 13, 2004
    #11
  12. On Sat, 12 Jun 2004 22:57:22 -0400, Ken Sington
    <ken_sington@nospam_abcdefg.com> wrote:

    >
    > 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
    --
    you'll see that it shouldn't be so. AND, the writting as usuall is
    fantastic incompetent. To illustrate, i quote:
    - Xah Lee trolling on clpmisc,
    "perl bug File::Basename and Perl's nature"
    Michele Dondi, Jun 13, 2004
    #12
  13. Ken Sington

    gnari Guest

    "Jay Tilton" <> wrote in message
    news:...
    > "gnari" <> wrote:
    >
    > : "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
    > : news:...
    > : >
    > :
    > : 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
    gnari, Jun 13, 2004
    #13
  14. Ken Sington

    Ben Morrow Guest

    Quoth "gnari" <>:
    > "Jay Tilton" <> wrote in message
    > news:...
    > > "gnari" <> wrote:
    > >
    > > : ($_ = $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.


    If $_ is an implicit loop variable it is implicitly localized to the
    loop. A frequent gotcha is that 'while (<$FH>)' *doesn't* localize.

    > 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

    --
    We do not stop playing because we grow old;
    we grow old because we stop playing.
    Ben Morrow, Jun 13, 2004
    #14
  15. Ken Sington

    gnari Guest

    "Ben Morrow" <> wrote in message
    news:caifjt$9ui$...
    >
    > Quoth "gnari" <>:
    > 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

    >
    > > of it's use, IMHO.

    > ^
    > grrr


    not native english speaker, but sorry.

    gnari
    gnari, Jun 13, 2004
    #15
    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. Kevin Spencer

    Re: foreach loop error

    Kevin Spencer, Aug 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    350
    Kevin Spencer
    Aug 22, 2003
  2. Luc Kumps

    Re: foreach loop error

    Luc Kumps, Aug 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    366
    Luc Kumps
    Aug 22, 2003
  3. news.frontiernet.net
    Replies:
    6
    Views:
    1,103
    news.frontiernet.net
    Apr 16, 2004
  4. loveNUNO
    Replies:
    2
    Views:
    892
    loveNUNO
    Nov 20, 2003
  5. Merciadri Luca
    Replies:
    4
    Views:
    799
Loading...

Share This Page