in reply to Re^5: simple array question
in thread simple array question

One very powerful operation in Perl is the "list slice". The code below shows a general application of that concept.

With a list slice, you can say: "I want the first element of an array, the last element of an array, and the the second element of an array", and assign those things to variables on the left hand side of the equals sign. WOW!

Here, [ 0,-1,1 ] look like indicies, but they aren't really the same thing. For example, [ -1 ], what kind of index is that? None. That -1 means the last thing in the list.

I am currently working with a database output that has hundreds of things on a line. I can assign the 89th thing, the 21st thing to Perl variables. my ($name,$address) = (@stuff)[ 88, 20 ]. The other things don't matter and I don't have to assign say, $zip_code to something that I am not going to use later in the code.

#!/usr/bin/perl -w use strict; my @compass = ( ["NW", "N", "X", "NE"], ["W", "center", "X", "E"], ["SW", "S", "X","SE"], ); foreach my $compass_row (@compass) { my ($first, $last, $second) = (@$compass_row)[0,-1,1]; my ($only_first) = (@$compass_row)[0]; print "only the first thing: $only_first\n"; print " first=$first last=$last second=$second\n"; } __END__ Prints: only the first thing: NW first=NW last=NE second=N only the first thing: W first=W last=E second=center only the first thing: SW first=SW last=SE second=S
Oh, of course $first and $only_first are the same. I assigned different variables to emphasize that there is no connection between the first and second list slice statements.

Replies are listed 'Best First'.
Re^7: simple array question
by tw (Acolyte) on Jan 03, 2011 at 15:33 UTC

    yeah wow, really cool and powerful. I had heard perl is really good for that sort of thing. I'm learning it initially to do a transformation on csv files with an unusual layout to import into a database. I've used sed, awk and some simple scripting before but no serious programming and the perl code can be so compact I can have trouble decoding it. I have a bit to learn.

    I can see how you're example works and can execute it. Been trying to apply it to another example but I've got a feeling my array might be a bit messed up - not getting positive results yet.

    Thanks for your time.

      I'm learning [Perl] initially to do a transformation on csv files with an unusual layout to import into a database.

      I encourage you to use the CPAN module Text::CSV. Do not try to write a CSV parser yourself in Perl. (It would be wise to heed this advice even if you weren't a Perl novice as you've told us you are.)

      Since your first adventure with the Perl programming language is a database application, consider using DBI and DBD::CSV.

      Welcome to the Perl community!

        Thanks Jim for the welcome and the advice. I've been very pleasantly surprised by the amount of help i've received from the monks after only one day.

        Have had a look at Text::CSV and Text::CSV_XS but did try doing a simple one on my own but i am running into trouble with it so i will take you're good advice.

      perl code can be so compact I can have trouble decoding it.
      Yes, sometimes you can see some incredibly cryptic looking Perl code! That doesn't mean that it is "good code", even if the code actually does work. Space things out and certainly don't hesitate to use multiple small steps instead of one complex one. Fewer characters of code does not necessarily result in faster code, actually the inverse is often true!

      Besides of course producing working code, the single most important goal is to write code that is clearly understandable. It is more likely to "work" and will certainly be easier to modify later. Save the super tricky looking stuff for some obfuscated code contest.

      Right from the start, you are tackling some difficult concepts with data structures that contain references to other data structures. There is a certain amount of de-referencing syntax that you will have to learn, but it is actually easier than equivalent implementations in C.

      Perl can be a wonderfully expressive language that is easy to read, often much, much easier to read than the equivalent C code. Or it can look like a cryptic mess.

      When I write code using list slice, I usually order the names of the variables on the left hand side to correspond to the order that they will be used in the subsequent code. I adjust the index values to the slice accordingly. List slice can be used with the various CSV parsers, well actually anything that produces a list. This is an important technique to clarify and un-clutter the code. This avoids all these declarations like "DEFINE Date 3", etc.