Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^5: simple array question

by tw (Acolyte)
on Jan 03, 2011 at 12:36 UTC ( [id://880177]=note: print w/replies, xml ) Need Help??


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

Thanks for the encouragement Marshall

You have partly anticipated my next hurdle

I am ok with using foreach to loop through all the elements but am struggling with returning particular values....for example if I only wanted to return the first value of @another_compass into a new array. i.e.

NW W SW

have tried a number of things similar to this...

my @first_value = (); for my $compass_row (@another_compass) { push (@first_value, shift); }
but obviously not correct, I used a split in place of shift with a slightly different problem so I thought shift would work. Although i assume you would need a different solution if the second value was required. Any pointers will be appreciated.

Replies are listed 'Best First'.
Re^6: simple array question
by Marshall (Canon) on Jan 03, 2011 at 13:37 UTC
    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.

      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!

        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.

Re^6: simple array question
by Marshall (Canon) on Jan 03, 2011 at 14:10 UTC
    Split is used to separate a single string into multiple items.
    #!/usr/bin/perl -w use strict; my $compass_string = "NW N X NE"; my @headings = split(/\s+/,$compass_string); foreach my $heading (@headings) { print "$heading\n"; } __END__ prints: NW N X NE

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-23 16:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found