Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^2: Array got modified!

by exilepanda (Pilgrim)
on Oct 10, 2011 at 02:54 UTC ( #930522=note: print w/replies, xml ) Need Help??

in reply to Re: Array got modified!
in thread Array got modified!

O!! Thx man! This explains a lot. Never notice for yrs.
So I am gonna change my habit to do this way :
@arr = qw/Foo Bar Blaz/; foreach ( @arr ) { my $ele = $_; $ele =~ s/^(.)/x$1/; } # or this while ( my $ele = <@arr> ) { $ele =~ s/^(.)/x$1/; } print "@arr"; # still 'Foo Bar Blaz'
Would you suggest which one is better, or any well suggested practice?

Replies are listed 'Best First'.
Re^3: Array got modified!
by Jenda (Abbot) on Oct 10, 2011 at 10:25 UTC

    Could you explain, in your own words, what does the while ( my $ele = <@arr> ) { do?

    Once you do, go check the docs!

    It may seem to do what you seem to want if you are unlucky enough and the @arr doesn't contain anything interesting to the diamond operator, but even then it's horribly inefficient.

    Enoch was right!
    Enjoy the last years of Rome.

      Hmm... my try is to take that array as a file handle to read, like @ARGV;

      I add up this code :

      @x = qw/Foo Bar Blaz/; #@x = qw//; print "Before: @x$/"; while ( my $ele = <@x> ) { next unless $ele; print "$ele becomes "; $ele =~ s/^(.)/x$1/; print $ele . $/; } print "$/After: @x"; __END__ Then I have : Before: Foo Bar Blaz Foo becomes xFoo Bar becomes xBar Blaz becomes xBlaz After: Foo Bar Blaz
      Is this work out properly by accident? This is what I want though...
      My observation is:
      if that's a empty array, it skips fine.
      And, without the diamond quote, it loops forever...
        $ perl -MO=Deparse junk print "Before: @x$/"; use File::Glob (); while (defined(my $ele = glob(join $", @x))) { do { next unless $ele; print "$ele becomes "; $ele =~ s/^(.)/x$1/; print $ele . $/ }; } print "$/After: @x"; __DATA__ junk syntax OK

        See glob, readline

        Yes, it's working by accident (no spaces nor * in @x). For example:
        perl -E '@arr=("a b", "c d", qw/e f g h a*/); while (my $e = <@arr>) { say $e }'
        gives this output for me:
        a b c d e f g h address.txt address.txt~ analytic ascii.htm aux-no-lex.lst
Re^3: Array got modified!
by BrowserUk (Pope) on Oct 10, 2011 at 03:36 UTC

    What is the point in editing $ele, if you then discard it?

      haha.. sorry I mislead something... In general to say, I am not trying to edit the array, I just want to loop through and read the array

      By all means, I have to pick elements out, one by one, change to some expected pattern, then throw it to other subs to do something( which I didn't state in the code ).

      Also, it implies that, I will reuse the created array again, so that I don't want it changed.

Re^3: Array got modified!
by choroba (Bishop) on Oct 10, 2011 at 07:31 UTC
    Never mind, I did not read the post properly.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://930522]
and the fire pops...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2018-01-18 06:51 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (206 votes). Check out past polls.