Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: split and uninitialized variables

by Arunbear (Parson)
on Sep 03, 2004 at 15:08 UTC ( #388301=note: print w/replies, xml ) Need Help??

in reply to split and uninitialized variables

You can do
my ($x, $y, $z) = ('', '', ''); ($x, $y, $z) = split(',', $_);
my ($x, $y, $z) = map { $_ || '' } split(',', $_);
to ensure the variables are initialised.

Update: Oops! I really need a new brain.

use strict; use warnings; $_ = "a,0,b"; my ($x, $y, $z) = split(',', $_); foreach ($x, $y, $z) { $_ = '' unless defined($_) } print "x = $x\ny = $y\nz = $z\n"; __END__ x = a y = 0 z = b

Replies are listed 'Best First'.
Re^2: split and uninitialized variables
by ikegami (Pope) on Sep 03, 2004 at 15:31 UTC

    Your first solution

    my ($x, $y, $z) = ('', '', ''); ($x, $y, $z) = split(',', $_);

    won't work. Any values assigned in the first line will get overwritten in the second line:

    $_ = 'a,b'; my ($x, $y, $z) = ('', '', ''); ($x, $y, $z) = split(',', $_); print(defined($z)?'defined':'undefined', "\n"); # prints undefined.

    Your second solution

    my ($x, $y, $z) = map { $_ || '' } split(',', $_);

    erases any '0'. Try:

    my ($x, $y, $z) = map { defined($_)?$_:'' } (split(',', $_))[0..2];

    Personally, I'd go with the simple my ($x, $y, $z) = (split(',', $_), ('') x 3); solution mentioned elsewhere.

    Update: Added missing [0..2] as pointed out by Roy Johnson. Thanks.

      Actually, the map solution doesn't work, because split doesn't return three elements, so you'd need to make it
      my ($x, $y, $z) = map { defined($_) ? $_ : '' } (split(/,/, $_))[0..2] +;

      Caution: Contents may have been coded under pressure.
Re^2: split and uninitialized variables
by Fletch (Chancellor) on Sep 03, 2004 at 15:33 UTC

    Your first one's not going to work.

    main::(-e:1): 0 DB<1> $_ = "a,b" DB<2> x ($x,$y,$z) = ('default') x 3 0 'default' 1 'default' 2 'default' DB<3> x ($x,$y,$z) = split( ',', $_ ) 0 'a' 1 'b' 2 undef
Re^2: split and uninitialized variables
by antirice (Priest) on Sep 03, 2004 at 15:33 UTC

    Both methods have problems:

    > perl -wl my ($x,$y,$z) = ('','',''); ($x,$y,$z) = split(',','a,b'); print "$x $y $z"; __END__ Use of uninitialized value in concatenation (.) or string at - line 3. a b > perl -wl my ($x, $y, $z) = map { $_ || '' } split(',', 'a,0,b'); print "$x $y $z"; __END__ a b

    The second one is easily fixed with:

    perl -wl my ($x, $y, $z) = map defined $_ ? $_ : '', (split(',', 'a,0,b'))[0..2 +]; print "$x $y $z"; __END__ a 0 b

    Update: Doh! Thanks !1 (silly lurker).

    The first rule of Perl club is - use Perl
    ith rule of Perl club is - follow rule i - 1 for i > 1

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://388301]
[stonecolddevin]: o/ planetscape
[Sinistral]: I think that just the sight of his username now causes a downvote storm. I agree, and the gratuitous use of formatting does make reading hard. I've given him +1 on things where there seemed to be actual good advice, but I think the big
[Sinistral]: nail in coffin was the rant against a Schwarzian Transform
[stonecolddevin]: at best, from what i've seen, his knowledge is accurate up to maybe 2002. the sheer volume of words is mostly worthy of a downvote in most cases though i think
[erix]: ITYM Schwartzian Transform :P
[erix]: hey planetscape :)
[planetscape]: howdy stonecolddevin, erix
[Sinistral]: eric Aye. I thought I typed that wrong. All hail Randall! I listen to him on FLOSS Weekly
[Discipulus]: i think his vacuus, trombon like, absence of meaning, joined with a ostentatious style, after many years irritates the most
[erix]: easy to get wrong -- I guess that was sund's point, too :P

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (10)
As of 2017-06-22 20:56 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (530 votes). Check out past polls.