in reply to Adding an element to an array

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @all_profile; my %homes_by_names; while (<DATA>){ chomp; my @f = split ',',$_; my %profile; $profile{ NAME } = $f[0];#$row->{'name'}; $profile{ ADDRESS } = $f[1];#$row->{'address'}; $profile{ HOME } = $f[2];#$row->{'home'}; # I will need to count the how many homes here ++$homes_by_names{ $profile{ NAME } }; push @all_profile, \%profile; } for (@all_profile){ $_->{'HOME COUNT'} = $homes_by_names{ $_->{ NAME } }; } print Dumper \@all_profile; __DATA__ John Doe, Main Street, House 1 John Doe, Main Street, House 1 John Doe, Main Street, House 1 Mary Lou, Central Street, House 1 Mary Lou, Central Street, House 1 Mary Lou, Central Street, House 1 Mary Lou, Central Street, House 1 Jane B,. Squere Rd., House 1 Maria D., Pat street, House B Maria D., Pat street, House B
poj

Replies are listed 'Best First'.
Re^2: Adding an element to an array
by Anonymous Monk on Oct 15, 2015 at 15:30 UTC
    Hi!
    I am trying to use your code but I am getting this error:
    Error: Global symbol "%row" requires explicit package name at... My data structure is like this:
    [ [ "John Doe", "Main Street", "House 1", ], [ "John Doe", "Main Street", "House 1", ], ... ]

    Trying to apply your code in here:
    my %homes_by_names; foreach my $row ( @data ) { $row{'HOME_COUNT'} = $homes_by_name{ $row{ HOME } }; }

    Thanks for looking!
      $row is a hashref. Try:
      $row->{'HOME_COUNT'} = $homes_by_name{ $row->{ HOME } };

              Software efficiency halves every 18 months, thus compensating for Moore's Law.

        I'm getting this:
        Not a HASH reference at ...
Re^2: Adding an element to an array
by Anonymous Monk on Oct 14, 2015 at 19:43 UTC
    Its getting there, I am getting an error as:
    Can't use string ("3") as an ARRAY ref while "strict refs"
    Any idea why?

      Witness the following:

      $ perl -Mstrict -Mdiagnostics -E 'my $a = "3"; say $a->[0];' Can't use string ("3") as an ARRAY ref while "strict refs" in use at - +e line 1 (#1) (F) You've told Perl to dereference a string, something which use strict blocks to prevent it happening accidentally. See "Symbolic references" in perlref. This can be triggered by an @ o +r $ in a double-quoted string immediately before interpolating a varia +ble, for example in "user @$twitter_id", which says to treat the conten +ts of $twitter_id as an array reference; use a \ to have a literal @ symbol followed by the contents of $twitter_id: "user \@$twitter_i +d". Uncaught exception from user code: Can't use string ("3") as an ARRAY ref while "strict refs" in use +at -e line 1.

      If you are getting this message, then your code is doing what my code demonstrates. It may take a more complex path to get there, but the result it the same; some variable that you think has an array reference in it actually has the string "3", and you are attempting to dereference it.


      Dave

      You will need to show the code that gives that error

      poj
        Fixed it , bad data, thank you!
        We can do this with sort and uniq commands in Unix. Keep all the data in file and try below commands
        cat 1.txt|sort |uniq -c
        Output would be 1 Jane B,. Squere Rd., House 1 3 John Doe, Main Street, House 1 2 Maria D., Pat street, House B 4 Mary Lou, Central Street, House 1 the numbers before name are the number of houses the person has I can still provide you the output in required format using awk