Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Make string that results from split behave like double-quoted string

by protist (Monk)
on Aug 23, 2013 at 20:17 UTC ( #1050729=note: print w/replies, xml ) Need Help??


in reply to Re: Make string that results from split behave like double-quoted string
in thread Make string that results from split behave like double-quoted string

Been seeing this for (my $x = 0; $x < @arr; $x++) sort of thing around the Monastery of late.

Is there a reason people prefer it to for (@arr)?

or is there no reason at all?

Replies are listed 'Best First'.
Re^3: Make string that results from split behave like double-quoted string
by ww (Archbishop) on Aug 23, 2013 at 22:07 UTC
    If you've actually seen that, I hope the node had acquired some corrective replies.

    Perhaps what you've actually seen is:

    for (my $i = 0, $i < $#arr, $i++) { #corrections ^ ^^ ^ #strictly speaking, the commas instead of semis are merely my preferen +ces do something...; } <br> <c>

    because the line you posted won't even compile... whereas the example in this node is basicly a C-style loop, possible written that way to show the counter.

    C:\>perl -E "my @arr=qw(foo bar baz);for (my $x = 0; $x < @arr; $x++){ + say $x };" 0 1 2
    My apologies to all those electrons which were inconvenienced by the creation of this post.

      The "line" I posted was actually an excerpt, and identical

      to the one in your second example. I am confused about

      what you think I did wrong.

      I was actually referring to glenn's code, which

      made no use of the counter aside from indexing.

      Even if you were to want to show a counter, there are other ways

      my $c = 0; for(@a){ print "$c: $_\n"; $c++; }

        Sorry, in the first instance, for appearing to attribute error to you. The problem I intended to address, IMO, is threefold -- unwise use of a C-style loop, incorrect understanding of the /pattern/ in split and processing that doesn't touch the relevant data.

        1. You're absolutely correct to say "glenn's code...made no use of the counter aside from indexing." In fact, that pretty much encapsulates my first and third points -- sorry that wasn't clearer -- which is that iterating over an array but printing only the counter, $x, as in your code or glenn's won't get you the contents of the array. They're being extracted (NB exception below) and discarded to the bit bucket. If one merely wishes to create an ordered numeric sequence, there are better ways, one of which may, depending on circumstances, be the method you show.
        2. As to that 'exception' comment, glenn's @arr appears by magic, without a source, in the split line. If the source is a record andan array, as in OP's example, 1,1999,"ln with \n newline", and is somehow contained in the default var, $_ ... well, then the var isn't an array. Oops!
        3. And if the record is seen as a string, the cited code won't work because the pattern in split, "/\|/, is the token upon which the source data is separated. Perhaps the writer confused split and join. (Many replies seem to assume there are vbars in OP's data. I don't see them.)

        Apologies to all those electrons which were inconvenienced by the creation of this post.
Re^3: Make string that results from split behave like double-quoted string
by glenn (Scribe) on Aug 24, 2013 at 22:39 UTC

    I do not like using $_ I like to know exactly which element is called. Also $#arr returns the last index of the array while @arr returns the number of elements, the difference is $x <= $#arr vs $x < @arr.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2023-12-11 10:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (41 votes). Check out past polls.

    Notices?