Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Required guidance to simplify this program

by vyeddula (Acolyte)
on Jun 04, 2013 at 05:07 UTC ( [id://1036891]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks.This program is to count the repetitions of elements in an array.I wrote something with my rookie knowledge but not getting the expected output.Need your help

#!/usr/bin/perl -w use strict; my @array=qw(John Sue Larry Mary John Mary Larry John Joe Lisa John Ma +ry); my $i; my $lastindex=$#array; my $count=0; foreach (@array) { for($i=0;$i<=$lastindex;$i++) { if($_ eq $array[$i]) { $count++; } } print"$_ is repeated for $count times \n"; $count=0; }

This is my code and output is

John is repeated for 4 times

Sue is repeated for 1 times

Larry is rep......for 2 times

Mary is rep........for 3 times

John is rep.........for 4 times

Mary is rep..........for 3 times

It went so on for each element.My question is how to delete the redundancy of elements.Where can i improve this program.Thanks for your time

Replies are listed 'Best First'.
Re: Required guidance to simplify this program
by 2teez (Vicar) on Jun 04, 2013 at 05:47 UTC

    You got to look at or learn to use HASH.
    Since you know how to use a for loop.
    The following could help:

    #!/usr/bin/perl -w use strict; my @array = qw(John Sue Larry Mary John Mary Larry John Joe Lisa John +Mary); my %names; # create a hash variable to use for (@array) { $names{$_}++; } for ( keys %names ) { $names{$_} <= 1 ? print $_, ' is repeated for ', $names{$_}, ' time', $/ : print $_, ' is repeated for ', $names{$_}, ' times', $/; }
    Hope it helps.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

      hi 2teez.Thanks for your response.

      for (@array) { $names{$_}++; }

      I would like to know what exactly this piece of code dose

      You created a hash and inside the foreach loop i didn't get $names{$_}++ part.Please clarify.Thanks

        For each name in the array, if the name already exists in the hash as key, then increment the value by 1; but if not, then add the name to the hash with the initial value of 1.
        Hope it helps.

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me
Re: Required guidance to simplify this program
by vinoth.ree (Monsignor) on Jun 04, 2013 at 05:52 UTC

    Use grep and hash method to count the number of occurrence of elements in array as below.

    use strict; use warnings; use Data::Dumper; my %count; my @array = qw(John Sue Larry Mary John Mary Larry John Joe Lisa John +Mary); grep {++$count{$_}} @array; print Dumper \%count;

    All is well

      Using grep and map in a void context is generally discouraged. A for statement modifier is usually clearer, and almost always faster.

      use strict; use warnings; use Data::Dumper; my @array = qw(John Sue Larry Mary John Mary Larry John Joe Lisa John +Mary); my %count; $count{$_}++ for @array; print Dumper \%count;
      package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

      Be that as it may, function map can also be used, but their usage i.e map and grep , like this is NOT efficient, because you are throwing away their return values.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-20 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found