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

How to make it simple?

by Anonymous Monk
on Mar 26, 2004 at 17:23 UTC ( #340085=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, i need a better way to do:
my ($a, $b, $c, $d, $e, $f, $g, $h) = splice(@array, 0, 8); $b =~ s/\.//g; $c =~ s/\.//g; $d =~ s/\.//g; $e =~ s/\.//g; $f =~ s/\.//g; $g =~ s/\.//g;
any hints?

Replies are listed 'Best First'.
Re: How to make it simple?
by esskar (Deacon) on Mar 26, 2004 at 17:24 UTC
    my @list = splice(@array,0,8); s/\.//g foreach @list[1..6];
•Re: How to make it simple?
by merlyn (Sage) on Mar 26, 2004 at 18:19 UTC
    Naming variables sequentially, then wanting to do the same thing to many of them, is almost always a sign that they shouldn't be separate variables, but rather elements of a larger structure.

    Just a clue.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Update: Argh! I just re-read the node and discovered I just wrote a bad re-implementation of esskar's code.

      Update2: Here is essentially what I was thinking, implemented correctly:

      #!/usr/bin/perl use strict; use warnings; my @array = qw( a..a b...b c....c d.....d e......e f.......f g........ +g h.........h i..........i j.j k..k l...l ); foreach ( do { my @subset = splice( @array,0,8 ); my @chunk = @subset[1..6]; } ) { s/\.//g; print $_,"\n"; }

      Here is what I orginally posted:

      And on that note...

      my @chunk = splice(@array,0,8); s/\.//g foreach (@smallchunk);

      Or skipping the intermediate array:

      s/\.//g foreach ( splice(@array,0,8) );

      I suppose I should test that code. I think in Perl, but I have the vocabulary of a two-year-old ;)


Re: How to make it simple?
by etcshadow (Priest) on Mar 26, 2004 at 18:17 UTC
    Above points perfectly good, but if you want to keep the variables named (at least in a sense), then use a hash and an array of keys:
    my @names = qw( a b c d e f g ); my %var; @var{@names} = splice(@array, 0, 8); foreach my $name (@names) { $var{$name} =~ s/\.//g; } # now, you can reference what was $a in your old code as $var{a}, etce +tera
    ------------ :Wq Not an editor command: Wq
Re: How to make it simple?
by flyingmoose (Priest) on Mar 26, 2004 at 18:22 UTC

    This is a bit of an explanation on what the other monks were doing with @array.

    splice(@array, 0, 8);

    splice destructively removes elements from @array, so you have to ask if you really want to do that. Since you are not splicing elements into the array (like you would splice a rope), splice probably doesn't make sense in this context.

    A slice (like you do with pizza or pie, not like a rope) (see below) seems to be what you want, and it is a little more intuitive to read. The splice just accesses the elements, without removing them.


    The other monks comments on how to do a foreach still apply to. So you can just foreach over a slice. It's better than repeating statements or using a for loop with a counter variable. If you want just part of the array, you can specify the start and stop boundaries of slice using something like @array[1..6]

    foreach $item (@array[1..6]) { $item =~ s/\.//g; }

    So this modifies only certain elements of the array, and it's pretty darn clean to look at. You may to modify the indexes (indices) if 1..6 isn't what you want exactly, but you get the general idea.

Re: How to make it simple?
by eclark (Scribe) on Mar 26, 2004 at 17:59 UTC
    my ($a, $b, $c, $d, $e, $f, $g, $h) = ($array[0], map { $_ =~ s/\.//g; + $_ } @array[1..8]);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://340085]
Approved by Old_Gray_Bear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2021-04-16 18:12 GMT
Find Nodes?
    Voting Booth?

    No recent polls found