how to find no of elements in array/hash/array passed to subroutine without reference? 
Author Message
 how to find no of elements in array/hash/array passed to subroutine without reference?

Hi all,

I googled for above question but could not find required answer. Well

passed to a subroutine/function. Since the sub routine *modifies* the

reference.

$arr1[0] = "zero";
$arr1[1] = "one";
$arr1[2] = "two";

$arr2[0] = "A";
$arr2[1] = "B";

$hash{'a'} = "AA";
$hash{'b'} = "BB";


sub mysubroutine
{

Quote:
}


subroutine is called like this ...


thanks in advance,
Bhaskar



Wed, 23 Nov 2005 08:28:10 GMT  
 how to find no of elements in array/hash/array passed to subroutine without reference?
Bhaskar Kurapati wrote at Sat, 07 Jun 2003 00:28:10 -0700:

Quote:
> I googled for above question but could not find required answer. Well I

> passed to a subroutine/function. Since the sub routine *modifies* the


?!

If a reference of anything is passed to a subroutine, the subroutine can
change the anything that is referenced on.
What do you really mean ?

Greetings,
Janek



Wed, 23 Nov 2005 08:02:35 GMT  
 how to find no of elements in array/hash/array passed to subroutine without reference?

Quote:

> I googled for above question but could not find required

> %hash. These three will be passed to a subroutine/function.



It sounds like all you really want is:

    (a) to pass more than one hash/array to a subroutine, and
    (b) to be able to modify a local copy of each

And you know how to do (a) by passing references -- so the
remaining question is:

   "How can I make a copy of a hash/array?"

Much simpler... :-)


    my %copy = %$ref_to_original;

    sub foo {

        my %h = %{ shift() };
    }


--
Steve



Wed, 23 Nov 2005 09:49:32 GMT  
 how to find no of elements in array/hash/array passed to subroutine without reference?

Quote:

> I googled for above question but could not find required answer. Well

> passed to a subroutine/function. Since the sub routine *modifies* the

> reference.

It took me a while to understand what you mean with that sentence. I guess
what you mean is the sub modifies them internally but you don't want the
original data changed.

Well, trivial: make a local copy and work on that data.
It's just the same as passing by value, only now your are creating the copy
explicitely while with passing by value the interpreter creates the copy for
you.

Quote:
> sub mysubroutine
> {


my %hash = %{$_[1]};

Quote:
}

And la voila, you got local copies of your argument data.
Quote:

> }


> subroutine is called like this ...


> thanks in advance,
> Bhaskar



Wed, 23 Nov 2005 14:28:05 GMT  
 how to find no of elements in array/hash/array passed to subroutine without reference?

Quote:

> Are you sure you really want to _pass_ them? Maybe you want:

> mysubroutine();

> sub mysubroutine
> {

> In other words,

"use global variables to communicate with the subroutine instead of
subroutine arguments".

Quote:
> provided that the variables are either package
> globals or 'my' declared on 'the package level' (i.e. not in some
> other subroutine, for instance), you can well access them from inside
> your subroutine.

Bad idea. Bad design. Bad programming.

Global variables should be used _very_ sparingly.

Action-at-a-distance leads to hard to debug programs,
global variables lead to action-at-a-distance.

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Wed, 23 Nov 2005 15:02:48 GMT  
 how to find no of elements in array/hash/array passed to subroutine without reference?

Quote:


>>Are you sure you really want to _pass_ them? Maybe you want:

>>mysubroutine();

>>sub mysubroutine
>>{


<snip>

Quote:
>>In other words,

> "use global variables to communicate with the subroutine instead of
> subroutine arguments".

>>provided that the variables are either package
>>globals or 'my' declared on 'the package level' (i.e. not in some
>>other subroutine, for instance), you can well access them from inside
>>your subroutine.

> Bad idea. Bad design. Bad programming.

If done in a routine fashion: Yes. In this case, considering the
background OP has given: I'm not able to tell. Are you?

/ Gunnar

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



Wed, 23 Nov 2005 17:35:22 GMT  
 how to find no of elements in array/hash/array passed to subroutine without reference?

If you mean that the subroutine makes changes that you DO want to persist
after the sub returns, passing by reference is exactly what you WANT to
do.

If you mean the subroutine makes changes that you DON'T want happening,
you're already out of luck:

#!/usr/bin/perl -w

use Data::Dumper;

%hash = ( a => 'Aa', );

print "Before:\n";


print "After:\n";

sub mysubroutine
{

Quote:
}

sub dumpr
{

Quote:
}

__END__

Results:

Before:
$VAR1 = [
          'zero'
        ];
$VAR1 = {
          'a' => 'Aa'
        };
$VAR1 = [
          'A'
        ];
After:
$VAR1 = [
          'ZERO'
        ];
$VAR1 = {
          'a' => 'AA'
        };
$VAR1 = [
          'A'
        ];

If you change the subroutine to

sub mysubroutine
{


Quote:
}

Results are:
Before:
$VAR1 = [
          'zero'
        ];
$VAR1 = {
          'a' => 'Aa'
        };
$VAR1 = [
          'A'
        ];
After:
$VAR1 = [
          'zero'
        ];
$VAR1 = {
          'a' => 'Aa'
        };
$VAR1 = [
          'A'
        ];

But this ONLY works for hasshes and arrays one level deep.  Any deeper


In that case, use Storable.pm.

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---



Wed, 23 Nov 2005 19:03:26 GMT  
 how to find no of elements in array/hash/array passed to subroutine without reference?
Almost forget the original question:

If you insist on not passing references AND you know that you are always
passing an array, a hash and an array, in thatb order, then pass the
lengths first, eg. something like (NOT TESTED):

# NOT TESTED!


$L2 = 2 * scalar keys %hash;

# scalar functions above probably unneeded due to context.

# NOT TESTED!





        # knock yourself out.
        # if original arrays and hash containd no references, # all changes are
        # local

Quote:
}

__END__

but this seriously sucks.  it breaks easily.  Use Storable to copy if
needed and pass references.

replies to bob AT bob-n DOT com

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---



Wed, 23 Nov 2005 19:15:20 GMT  
 how to find no of elements in array/hash/array passed to subroutine without reference?
Told you this wasn't tested, this should be


sub mysubroutine
{



        # knock yourself out.
        # if original arrays and hash containd no references, # all changes are
        # local

Quote:
}





>    # knock yourself out.
>    # if original arrays and hash containd no references, # all changes are
>    # local
> }

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---


Wed, 23 Nov 2005 19:18:27 GMT  
 how to find no of elements in array/hash/array passed to subroutine without reference?
Quote:


> sub mysubroutine
> {

don't you also need something like
   my $L1 = shift;
   my $L2 = shift;
   my $L3 = shift;

Quote:



>   # knock yourself out.
>   # if original arrays and hash containd no references, # all changes are
>   # local
> }



Wed, 23 Nov 2005 19:32:03 GMT  
 how to find no of elements in array/hash/array passed to subroutine without reference?
Damn straight.  Since I NEVER plan on doing anything like this myself, I
didn't test it, as I noted.  But you are absolutely correct.

Quote:

> don't you also need something like
>    my $L1 = shift;
>    my $L2 = shift;
>    my $L3 = shift;


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---


Wed, 23 Nov 2005 19:37:28 GMT  
 how to find no of elements in array/hash/array passed to subroutine without reference?

Quote:

> Damn straight.  Since I NEVER plan on doing anything like this myself, I
> didn't test it, as I noted.  But you are absolutely correct.


> > don't you also need something like
> >    my $L1 = shift;
> >    my $L2 = shift;
> >    my $L3 = shift;

> ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
> http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
> ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

Hi All,

Thanks for giving reply. Sorry for posting a confusing question. What
i want is exaclty what 'Steve Grazzini' and 'Jrgen Exner' understood.
I got answers from them.

regards,
Bhaskar Kurapati



Fri, 25 Nov 2005 05:38:57 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. URGENT: Hash in Hash, Array in Array, Array in Hash, Hash in Array

2. Q: passing array/hash from one subroutine to another subroutine

3. passing an array inside a hash to a subroutine

4. Problem passing arrays and hashes to subroutines.

5. Help - passing hash array into a subroutine

6. Anonymous array, delete element from array if found:

7. hash of values and array of hashes of values and array of hashes

8. performing subroutine on a strange set of elements from array

9. Can array elements be subroutines?

10. array/hash to file to array/hash

11. How to access the element of array passed by ref

12. Simple Perl5 question (hash-array of hash-array refs)

 

 
Powered by phpBB® Forum Software