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

I am trying to pop the last element of the array as you can see in this test code but I am getting this error:
Can't use string ("010101") as an ARRAY ref while "strict refs" in use at ...
It should be a better way or where am I doing it wrong?
Thanks for looking!
#!/usr/bin/perl use strict; use warnings; use Data::Dump 'pp'; ... calc($data); sub calc { my $data = shift; foreach my $account ( keys %{$data} ) { my $number = $data->{$account}; my $zip = pop @{ $number->{zip}[1][4]}; pp $zip; insert_zip($zip); } } =data ( [ "Name", "Add", "Date", "email", ], [ "Joe Doe", "100 - Main St.", "12/31/2014", "X", "010101", ], [ "Carl Tet, "200 - Test Street", "12/31/2010", "C", "010101", ], [ "Mary G", "127 _ Rol Ro Street", "11/22/2014", "A", "010101", ], [ "Matt Carr, "2 - New Street", "12/15/2004", "B", "010101", ], ) =cut

Replies are listed 'Best First'.
Re: Issue with POP in array ref.
by choroba (Cardinal) on Sep 15, 2015 at 15:15 UTC
    Your code doesn't compile. You use %{$data}, but the data you show in the POD are an array. From the error, it seems you tried to dereference the zip code as an array ref:
    use strict; my $data = [ '10010' ]; print @{ $data->[0][1] };

    Use Data::Dumper to see what data structures your variables contain.

    use Data::Dumper; print Dumper($data); print Dumper($data->[0]);

    pop needs an array as the parameter, not a string.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Issue with POP in array ref.
by SuicideJunkie (Vicar) on Sep 15, 2015 at 15:47 UTC

    All of the above replies seem to be focused on making the pop work. However the impression I get is that you don't want to pop at all.

    Your data structure doesn't seem to be at all related to the rest of the code, but putting that aside: why would you want to REMOVE the zip code from an array with a pop? I suspect you just want to assign the zip code to $zip, no popping involved.

Re: Issue with POP in array ref.
by trippledubs (Deacon) on Sep 15, 2015 at 15:12 UTC
    Your code doesn't compile so I can't test, but it looks like you are trying to pop the last element of the array, instead of popping the array which returns the last element. So, in some code that compiles and runs, if you're trying this
    pop @{ $number->{zip}[1][4]};
    try:
    pop @{ $number->{zip}[1]};
Re: Issue with POP in array ref.
by stevieb (Canon) on Sep 15, 2015 at 15:17 UTC

    That doesn't appear to be the proper data (its not a hash reference, it's an array of arrays). To further, there are missing double-quotes within it.

    Please show us a chunk of the real data that you're using, or the output of use Data::Dumper; print Dumper $data;.

Re: Issue with POP in array ref.
by RichardK (Parson) on Sep 15, 2015 at 15:28 UTC

    Could it simply be that this doesn't have 5 elements? i.e. there's nothing at $arr[4]

    [ "Name", "Add", "Date", "email", ],