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

Using split we can assign value to array like this,
@abc = split(';',$abc);
is it possible to assign two spliced thing to two variable
rather than to array??? may be
$a, $b = split(';',$abc);

Any Suggestion?Please
Cheers

Replies are listed 'Best First'.
Re: using split
by Anonymous Monk on Jul 23, 2009 at 02:43 UTC
    I've seen you write
    my( $foo, $bar ) = @_;
    This is no different
    ( $a, $b ) = split(';',$abc, 2);
      This is no different

      There are many differences between your two examples, but they do both have a list of variables on the left of the assignment operator and they both assign values to each variables.

      See List value constructors for more information.

      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: using split
by bichonfrise74 (Vicar) on Jul 23, 2009 at 04:52 UTC
    Try this...
    #!/usr/bin/perl use strict; my $abc = 'a;b;c;d'; my ($a, $b) = (split(';',$abc))[0,1]; print "$a and $b";
Re: using split
by svenXY (Deacon) on Jul 23, 2009 at 06:58 UTC
    Hi,
    just a side note: $a and $b are bad variable names as they are used for sort and stuff...
    Regards,
    svenXY
Re: using split
by imrags (Monk) on Jul 23, 2009 at 04:52 UTC
    Try this:
    Update: Added 2 lines more and modified the var names as per svenXY!
    $abc = 'i am going to split line1; and then line2; and then line3; and + assign to variables'; my ($no1,$no2,$no3) = (split/\;/,$abc); print "$no1\n","$no2\n","$no3\n"; my ($d,$e) = (split /\;/,$abc)[0,2]; # or anything [1,2] print $d,"\n",$e;
    Raghu