Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Assign scalar value to array

by paramreddy (Initiate)
on Oct 09, 2013 at 14:17 UTC ( #1057552=perlquestion: print w/ replies, xml ) Need Help??
paramreddy has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, Hope all of you having good time. I am new to perl and i am trying to learn perl. Here i want to know whether we can assign scalar value to array. example $num=12345; and i want 12345 should be assigned to @a @a = qw\1 2 3 4 5\ Can you help me in this? Thanks in advance, Parameshwar

Comment on Assign scalar value to array
Re: Assign scalar value to array
by ikegami (Pope) on Oct 09, 2013 at 14:21 UTC
Re: Assign scalar value to array
by Anonymous Monk on Oct 09, 2013 at 22:01 UTC
    $num = 12345; # empties the array @a = ($num); # or $a[0] = $num; print '$a[0] is ', $a[0], "\n";
Re: Assign scalar value to array
by flyby (Novice) on Oct 09, 2013 at 23:24 UTC

    Just some fun:

    #!/usr/bin/perl -w my @a; my $num = "12345"; # most likely the best way to do it: @a = split(//,$num); foreach (@a) { print "$_\n"; }; # but this is kind of fun: @a = (); #clear array my $l = length($num); for (my $x = "0"; $x < $l; $x++ ) { push(@a, substr($num, $x, 1)); }; foreach (@a) { print "$_\n"; }; # random: @a = (); #clear array for (my $x = "0"; $x < $l; $x++ ) { my $rand = rand($l); push(@a, substr($num, $rand, 1)); }; foreach (@a) { print "$_\n"; };

    -Matt

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1057552]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2014-12-25 13:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls