Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: split and uninitialized variables

by jeffa (Bishop)
on Sep 03, 2004 at 15:26 UTC ( #388308=note: print w/replies, xml ) Need Help??


in reply to split and uninitialized variables

I prefer to store the list returned by split in an array or hash slice:

use strict; use warnings; use Data::Dumper; my @key = qw(x y z); my %hash; while (<DATA>) { chomp; @hash{@key} = split ','; print Dumper \%hash; } __DATA__ a,b,c a,b a,b,c,d a,,,

[UPDATE] No. Scalars are not the better choice simply because you know what the data that you are working on looks like. If you deal with multiple things, why not put them in a container so you only have to move one thing around? :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re^2: split and uninitialized variables
by podian (Scribe) on Sep 03, 2004 at 16:28 UTC
    That is what I usually do. But if you know what the data you are working on, then scalars will be better.
    One example could be "name, address-line1, address-line2"
    and line 2 might be or might not be present.
    So to summarise the replies, it would be better to initialize them in the next line.
    All the other solutions seem to have some problems!

      The ( '' ) x 3 solution doesn't.

      Another option:

      my ( $foo, $bar, $baz ); $_ .= '' for ( $foo, $bar, $baz ) = split /,/;

      Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://388308]
help
Chatterbox?
[davido]: Using the -i switch causes Perl to rename the input file, read from it, and write to a file using the original file's name. If there's an extension provided, as in -i.bak, it's easy to see where the input file is. Where is the input file temporarily....
[davido]: placed if there is no extension provided to the -i switch?
[davido]: Nevermind, found the answer.
[davido]: If no extension is supplied, and your system supports it, the original file is kept open without a name while the output is redirected to a new file with the original filename. When perl exits, cleanly or not, the original file is unlinked.
[haukex]: doc says "If no extension is supplied, and your system supports it, the original file is kept open without a name ..."

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2017-09-22 14:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (264 votes). Check out past polls.

    Notices?