Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^3: Array got modified!

by Jenda (Abbot)
on Oct 10, 2011 at 10:25 UTC ( #930575=note: print w/ replies, xml ) Need Help??


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

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.

Jenda
Enoch was right!
Enjoy the last years of Rome.


Comment on Re^3: Array got modified!
Download Code
Re^4: Array got modified!
by exilepanda (Monk) on Oct 10, 2011 at 12:51 UTC
    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

        Sorry but I don't get it.

        First, after a perldoc on the glob, I get totally lost, I never touch a Unix shell, so I couldn't imagine or relate to anything.. I am on Win32 all time..

        Second, would you explain more for what is your code try to demonstrate? Is this a safe/suggested way to do what I want? or it's proving my way is danger? or anything else ?

      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
        Got it! Didn't aware of it at all....
        Lesson learned!! Thanks a lot!!! =)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://930575]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2014-09-23 00:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (209 votes), past polls