Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
P is for Practical
 
PerlMonks  

Concatenate array element and scalar

by user111 (Initiate)
on Feb 27, 2004 at 15:01 UTC ( [id://332361]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

I have array
@some = (a,b,c,d)
And I have a scalar that is in the loop so each time its value is different.
E.g
First time in loop
$TEST = 1
Second time in loop
$TEST=2
etc.


I need to write the program that will concatenate these 2.
I need to have a new array that will like this:


my @new;

$new [0] = a 1 $new [1] = a 2 $new [2] = b 1 $new [3] = b 2 $new [4] = c 1


etc…
Right now I have array that looks like this:

$new[0] = a $new[1] = 1 $new[2] = a $new[3] = 2 $new[4] = b
etc.

Please help.
I am using push to put this array in @result. And then I am putting $test values in. I am trying to have this new array where the first element will be $ne +w[0] and first value of $test. for ($k=0;$k<$num_a;$k++){ $num_b = @lad; for ($y=0;$y<$num_b;$y++){ $lad[$y]=~/^(.*?)=(.*?)$/; $test = $2; chop($test); push(@result,$new[$k]); push(@result,$test); } } print "RESULT = @result\n";
UPDATE: Limbic~Region, thanks a lot. That works.

Replies are listed 'Best First'.
Re: Concatenate array element and scalar
by rchiav (Deacon) on Feb 27, 2004 at 15:27 UTC
    What exactly have you tried to solve this and how is it failing? If you can provide the code, we can more than likely get you moving in the right direction.
Re: Concatenate array element and scalar
by Limbic~Region (Chancellor) on Feb 27, 2004 at 15:36 UTC
    user111,
    Assuming your array is NOT what you are looping over, this is quite simple.
    my @array = qw(a b c d e); my @new_array; for my $variable ( 1 .. 5) { push @new_array, "$_ $variable" for @array; } print "$_\n" for sort @new_array;
    If that's not what you want you will need to explain what you want more clearly.

    Cheers - L~R

    Update: The original node was updated after I posted to include sample code, you want to change:

    push(@result,$new[$k]); push(@result,$test); # to push @result , "$new[$k] $test";
Re: Concatenate array element and scalar
by NovMonk (Chaplain) on Feb 27, 2004 at 15:37 UTC
    Welcome, Brother (or Sister):

    It would help if we could see the code you are using to get @new now. It seems to me like something like (in a loop)

    $new[0] = $some[0].$TEST[0] (etc...)

    should work, but I'm prettty new at this myself.

    One bit of advice that might help you much more from me is to click on "Need Help??" above and read all the "Welcome to the Monastery" links, specifically about how to ask questions in the most helpful way (for you and the helpful Perlmonks.)

    Best of luck. And hang in there on this learning stuff. It is worth it.

    NovMonk

      Welcome, Brother (or Sister)

      Welcome, Monker *cough* I mean Monk :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://332361]
Approved by Paladin
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.