Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: My 'perldar' tells me there is a 'better' solution for this list operation

by OfficeLinebacker (Chaplain)
on Nov 04, 2006 at 18:15 UTC ( [id://582257]=note: print w/replies, xml ) Need Help??


in reply to My 'perldar' tells me there is a 'better' solution for this list operation

Hey, thanks for all the helpful responses.

Thanks in particular to the link to hash slices. The part I found particularly helpful was

"If you're confused about why you use an '@' there on a hash slice instead of a '%', think of it like this. The type of bracket (square or curly) governs whether it's an array or a hash being looked at. On the other hand, the leading symbol ('$' or '@') on the array or hash indicates whether you are getting back a singular value (a scalar) or a plural one (a list)."

I think that using "map" as a variable name in an example using the "map" function is a bit confusing.

As far as when to declare variables, I do try to use the smallest lexical scope possible. I also often have a series of what I call "global" variables at the beginning and top level of the program declared with "my." The main purpose is a) saving typing (if certain strings are used several times in the program I will use a variable with a short name; for example if I know I will be using directory and/or file base names of "temp" I might declare 'my $t = "temp";' or somesuch) b) the other obvious reason of making the code more maintainable (if the directory I want to use for something changes, I only have to change it in one spot near the beginning of the program). Both are fraught with danger though as sometimes I'll see a variable used and have to scroll back up to remind myself what it is. This discussion is probably as old as the concept of scoping; further comments(or nudges in the right direction) are welcome.

In this particular case I download four reports in CSV format and I define a (global) hash with reportname=>["wantedfield1","wf2",etc] pairs. The full list of available column headers is generated dynamically by reading in the first line of each report. Then, the field map is generated via the process discussed in this thread. This means hopefully the next time they change the reports (which seems to constitute simply adding fields), it won't be a major rewrite.

johngg, I don't get your method at all.

One last question: it seems a couple of the responses used qw(Field1 Field2 Field3) as opposed to ("Field1","Field2","Field3"). What's the difference?

Thanks again and ++ all responses in this thread regardless.

T.

_________________________________________________________________________________

I like computer programming because it's like Legos for the mind.

Replies are listed 'Best First'.
Re^2: My 'perldar' tells me there is a 'better' solution for this list operation
by Hofmator (Curate) on Nov 06, 2006 at 08:49 UTC
    it seems a couple of the responses used qw(Field1 Field2 Field3) as opposed to ("Field1","Field2","Field3"). What's the difference?

    Short answer, there is no difference :).

    Long answer, see perlop.

    qw/STRING/
          Evaluates to a list of the words extracted out of
          STRING, using embedded whitespace as the word 
          delimiters.  It can be understood as being roughly
          equivalent to:
    
              split(' ', q/STRING/);
    
          the difference being that it generates a real list
          at compile time.  So this expression:
    
              qw(foo bar baz) 
    
          is semantically equivalent to the list:
    
              'foo', 'bar', 'baz'
    
          Some frequently seen examples:
    
              use POSIX qw( setlocale localeconv )
              @EXPORT = qw( foo bar baz );
    

    -- Hofmator

    Code written by Hofmator and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-03-29 13:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found