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

I saw somewhere and now can't find (now that I could use it) something that worked along the lines of

@identity eq (name,billy,state,colorado,age,21); print $name\n; print $state\n; print $age\n;
and it would give
billy colorado 21
this may not be much like it at all but i can't seem to find it. anyone know what I'm talkin about and care to share? It is appreciated!

20031221 Edit by Corion: Removed stray <br> tags from within code

Replies are listed 'Best First'.
Re: Multiple variables & values in one line
by davido (Cardinal) on Dec 21, 2003 at 07:57 UTC
    Your syntax is a little messed up, but here's probably what you're looking for:

    my %identity = qw/name billy state colorado age 21/; print $identity{name} , "\n", $identity{state}, "\n", $identity{age} , "\n";

    If your interest in Perl lasts more than another hour or two, you would really be doing yourself a favor by picking up Learning Perl (the Llama book), published by O'Reilly & Assoc. It's a fantastic resource that will introduce you to Perl's rich syntax, while teaching how to think "the Perlish way".

    perlintro might also really be helpful to you at first, and the best part is it's free. ;)

    Good luck. Asking questions here in the monastery can be a helpful supplement as you study the other resources.


    Dave

Re: Multiple variables & values in one line
by grinder (Bishop) on Dec 21, 2003 at 10:45 UTC

    You might have seen a hash slice. If you have an array of names that look like:

    my @fields = qw( name state age );

    then you could assign all the corresponding fields in the %identity hash with the following assignment:

    @identity{ @fields } = qw( billy colorado 21 );

    Even without the array, it's still a nice technique for making things line up:

    @identity{ qw/ name state age / } = qw/ billy colorado 21 /;

    I only use that sort of construct if I'm modifying the existing keys of a hash. When creating a hash, I'd probably make a list with the fat comma:

    my %identity = ( name => 'billy', state => 'colorado', age => 21, );

    The trailing comma in the list is fine. This is a syntactic nicety that helps you add and remove list pairs in the code without have to worry about getting the last line right (which is probably the most frequent error I make when composing SQL statements).

    Note that the fat comma list is not necessary, as shown by davido. Nonetheless, I would be inclined to use whitespace to help the eye line up the pairs.

    my %identity = qw/ name billy state colorado age 21 /;

    update: corrected the hash slice syntax to use curly braces instead of parentheses. /me slaps head.

Re: Multiple variables & values in one line
by Roger (Parson) on Dec 22, 2003 at 01:50 UTC
    Another method(s) that I use sometimes ...
    my ($name, $state, $age) = qw/ billy colorado 21 /; print "$name\n$state\n$age\n";
    or
    my ($name, $state, $age) = ( 'billy', 'colorado', 21 ); print "$name\n$state\n$age\n";
Multiple variables & values in one line
by einerwitzen (Sexton) on Dec 22, 2003 at 15:38 UTC
    The goal of it is actually be able to load it from an outside source. Kind of like...

    @test eq ('billy', 'colorado', '21'); my ($name, $state, $age) = (@test); print "$name\n$state\n$age\n";
    I am trying this because I couldn't figure out how to use the require() command. If you think that would work better an example of that would be good.