Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Regarding arrays

by editi (Novice)
on May 23, 2007 at 08:33 UTC ( [id://616956]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Regarding arrays
by shmem (Chancellor) on May 23, 2007 at 08:45 UTC
    You need a paper clip, duct tape, a swiss army knife, a single aspirin pill and a chewing gum.

    I know what I mean. Why don't you?

    <update>

    This reeks after homework. What do you have to do to get that output from the two arrays? Can you formulate that task in words? can you write down that description in your post? can you try to do that yourself, present the code you have so far and tell us where you're stuck?

    Then do it.

    If you can't, then solving the problem for you won't help you in any way, because it would seem you're not fit for programming.

    </update>

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Regarding arrays
by blazar (Canon) on May 23, 2007 at 10:03 UTC
    monks I have a array Array1=(ME K LE K HT ME LL) Array2=(10 4 1 5 6 7 19)

    Not Perl!

    @Array1=qw(ME K LE K HT ME LL); @Array2=(10, 4, 1, 5, 6, 7, 19);
    i have to display the output like ME=17
    print "ME=17\n";
Re: Regarding arrays
by borisz (Canon) on May 23, 2007 at 08:54 UTC
    use Data::Dumper; my @a1 = qw(ME K LE K HT ME LL); my @a2 = qw(10 4 1 5 6 7 19); my %h; $h{ $a1[$_] } += $a2[$_] for ( 0 .. $#a1 ); print Dumper( \%h ); __OUTPUT__ $VAR1 = { 'HT' => 6, 'ME' => 17, 'LE' => 1, 'K' => 9, 'LL' => 19 };
    Boris
Re: Regarding arrays
by Random_Walk (Prior) on May 23, 2007 at 11:55 UTC
    perl -le'@a1=qw(ME K LE K HT ME LL);@a2=qw(10 4 1 5 6 7 19);map{$o{$_}+=shift@a2}@a1;print"$_=$o{$_}" for sort keys %o'

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Regarding arrays
by andreas1234567 (Vicar) on May 23, 2007 at 09:32 UTC
    Building on borisz's solution, except the output is ordered according to the first array. Each entry is written once only.
    my @a1 = qw(ME K LE K HT ME LL); my @a2 = qw(10 4 1 5 6 7 19); my %h; $h{ $a1[$_] } += $a2[$_] for ( 0 .. $#a1 ); for (@a1) { next if (!exists($h{$_})); print $_ ."=". delete($h{$_}); } __END__ ME=17 K=9 LE=1 HT=6 LL=19
    BTW: Is this homework?

    Andreas
    --
Re: Regarding arrays
by Fletch (Bishop) on May 23, 2007 at 14:32 UTC
    array1 = %w( ME K LE K HT ME LL ) array2 = [ 10, 4, 1, 5, 6, 7, 19 ] sums = Hash.new( 0 ) array1.zip( array2 ).each { |(k,v)| sums[k] += v } array1.each { |k| puts "#{k}=#{sums[k]}" }

    Update: The Ruby's because of the homework stench. This translates all but verbatim into Perl (presuming you know that pairwise { [ $a, $b ] } @Array1, @Array2; is the List::MoreUtils equivalent of Ruby's Array#zip). Just add sigils and shake!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-19 15:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found