Professional Web Applications Themes

Another Perl datatype headache ( scalars $, hashes %, and arrays ) - PERL Beginners

Hi everyone, after writing perl scripts for about 3 years now, I still have trouble with the basic datatypes. I know that variables that start with '$' are scalars. This covers Hashes ($HASH{$key}), Arrays ( $_[0] ), and regular scalar values ( $foobar ); The code I write as well other's code is still unreadable to me even though I have followed examples from the Camel book, many other Perl books from O'Reilly and online references. I have also used perldoc on many occasions. There are still some things that haven't sunk in, such as: If I want to add ...

  1. #1

    Default Another Perl datatype headache ( scalars $, hashes %, and arrays )

    Hi everyone,
    after writing perl scripts for about 3 years now, I still have trouble
    with the
    basic datatypes.
    I know that variables that start with '$' are scalars.
    This covers Hashes ($HASH{$key}), Arrays ( $_[0] ), and
    regular scalar values ( $foobar );

    The code I write as well other's code is still unreadable to me
    even though I have followed examples from the
    Camel book, many other Perl books from O'Reilly and online references.
    I have also used perldoc on many occasions.

    There are still some things that haven't sunk in, such as:

    If I want to add Hash keys to another Hash, I do the following:

    %HASH = ( 1 => 'one' ); #NO BRACES OR ELSE....
    %HASH2 = ( 2 => 'two' ); AGAIN, NO BRACES OR ELSE...
    HASH2{ keys %HASH } = "";
    #confusing, considering it's the symbol used for arrays

    To get the length of an array, it's $#array, not #array or #$array.
    Usually, I use scalar array;

    Problems with subroutines where the array is the first argument
    sub badsub()
    {
    my (array,$scalar) = _;
    #Pass Array last!
    #my ($scalar,array) = _:
    ...
    }

    I still don't know how to declare arrays using only '$' instead of ''

    anyway, Is it possible to write scripts using only '$' instead of other
    prefix symbols?
    In other words, a php-style script written in perl

    Thanks in advance.
    -gohaku

    Gohaku Guest

  2. #2

    Default Re: Another Perl datatype headache ( scalars $, hashes %, and arrays )

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    On Mon, 19 Jul 2004 12:59, gohaku wrote: 
    They do different things, afaik. $#array gives you the index of the last entry
    in the array, scalar array gives you the size. So in general,
    $#array+1==array
     
    I got stuck for a while on this one. If you do this:
    my a = (7,8,9);
    my $z = "hello";
    badsub(a, $z);
    it is turned into:
    badsub((7,8,9),"hello");
    which, as Perl doesn't nest lists, and arguments are just lists, is:
    badsub(7,8,9,"hello");
    you see why it gets messy now when you try to access it?

    There are a few ways around it:
    badsub(\a, $z); # ugly, but sometimes useful if you have more than one list
    you want to keep apart
    badsub(a, $z); # where badsub has a prototype: (\$)
    badsub($z, a); # better still
    I wrote a little bit about working with references here:
    http://www.kallisti.net.nz/PerlTips/ReferencesPassedToSubroutines
    when I got stuck. However, I've since learnt more that I need to add to that
    page. [Just done so]
     
    You can't, so far as I know. It doesn't make sense. signifies you want an
    array. $ signifies you want a scalar. Look at it this way: if you want a
    single value from something, prefix it with $. If you want a list, prefix it
    with . If you provide a list when something is expecting a single item, it
    does 'scalar list' which gives the size (someone correct me if I'm wrong
    here, please :)
     
    PHP works with them quite differently. In that, a variable starts with $ and
    may contain an array. It also doesn't have hashes like Perl (or rather, it
    doesn't have arrays like Perl, they are kind of like a mix of the two,
    similar to as if you didn't use arrays, but used hashs with '0', '1', '2' as
    the key). Note, my PHP knowledge isn't all that good, so I may not be totally
    accurate.

    - --
    Robin <net.nz> JabberID: <org>

    Hostes alienigeni me abduxerunt. Qui annus est?

    PGP Key 0x776DB663 = DD10 5C62 1E29 A385 9866 0853 CD38 E07A 776D B663
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.3 (GNU/Linux)

    iD8DBQFA+39EzTjgendttmMRAnOGAKCCEVl9ZkoP+sKSJP8VUR dBJuMW+QCgln+L
    tocP/YSbD3zRPtQEPnn+ARw=
    =fxZM
    -----END PGP SIGNATURE-----
    Robin Guest

  3. #3

    Default RE: Another Perl datatype headache ( scalars $, hashes %, and arrays )

    > I still don't know how to declare arrays using only '$' instead of ''

    You can't. But you can store a *reference* to an array in a scalar.

    This will work:

    # the backslash ("\") returns a reference to the
    # variable, so this doesn't actually pass the array,
    # it passes a reference (pointer sort of) to the array.
    goodsub(\array, $scalar);

    sub goodsub()

    {
    my ($array_ref,$scalar) = _;

    # turns the ref back to an actual array.
    my array = {$array_ref};

    # or use the array directly through the ref.
    # note that changes made through a ref will change
    # the original array.
    print $array_ref->[0];
    }
     

    No, not the way you intend. You could use only references, but that
    wouldn't make sense.

    Rob

    -----Original Message-----
    From: gohaku [mailto:net]
    Sent: Sunday, July 18, 2004 8:59 PM
    To: Perl Beginners
    Subject: Another Perl datatype headache ( scalars $, hashes %, and
    arrays )


    Hi everyone,
    after writing perl scripts for about 3 years now, I still have trouble
    with the
    basic datatypes.
    I know that variables that start with '$' are scalars.
    This covers Hashes ($HASH{$key}), Arrays ( $_[0] ), and
    regular scalar values ( $foobar );

    The code I write as well other's code is still unreadable to me
    even though I have followed examples from the
    Camel book, many other Perl books from O'Reilly and online references.
    I have also used perldoc on many occasions.

    There are still some things that haven't sunk in, such as:

    If I want to add Hash keys to another Hash, I do the following:

    %HASH = ( 1 => 'one' ); #NO BRACES OR ELSE....
    %HASH2 = ( 2 => 'two' ); AGAIN, NO BRACES OR ELSE...
    HASH2{ keys %HASH } = "";
    #confusing, considering it's the symbol used for arrays

    To get the length of an array, it's $#array, not #array or #$array.
    Usually, I use scalar array;

    Problems with subroutines where the array is the first argument
    sub badsub()

    {

    my (array,$scalar) = _;
    #Pass Array last!
    #my ($scalar,array) = _:
    ...
    }

    I still don't know how to declare arrays using only '$' instead of ''

    anyway, Is it possible to write scripts using only '$' instead of other
    prefix symbols?
    In other words, a php-style script written in perl

    Thanks in advance.
    -gohaku


    --
    To unsubscribe, e-mail: org
    For additional commands, e-mail: org
    <http://learn.perl.org/> <http://learn.perl.org/first-response>

    Rob Guest

  4. #4

    Default Re: Another Perl datatype headache ( scalars $, hashes %, and arrays )

    On Jul 18, 2004, at 7:59 PM, gohaku wrote:
     

    Howdy.
     

    Looks like you are doing good to me.
     

    I'm sorry to hear that. Sounds like you are doing most of the right
    things. Perhaps you just need to give it some more time.
     

    If it's confusing, let's do it a different way.

    for my $key (keys %HASH) {
    $HASH{$key} = "";
    }

    Shorter isn't always better. Use what you can understand.
     

    $#array is not the length of the array. It is the number (#) of the
    last scalar ($) held in the array or more simply, the last index that
    has been assigned.

    scalar(array) is the length and the way to go, so again I see no
    problem here.
     

    Again, you show the fix for the problem you mention. Looks like you
    understand a lot more than you give yourself credit for.

    If you are sure your sub is called:

    some_sub( array, $scalar )

    You could use:

    sub some_sub {
    my $scalar = pop _;
    my array = _;

    # ...
    }

    But I much prefer reversing the order, as you did above.
     

    I believe you are talking about references here. They would be a good
    solution to the problem above. Using them you can pass 5 arrays, 3
    hashes and 2 scalars to a sub in any order you like.

    However, they complicate things a little. If you're still having
    trouble grasping Perl without them, give it a little more time before
    running down that road.
     

    Whether it is or not, it's not the answer to your problems.

    Start trying to make sense of the world you find yourself in. There
    are rules. Try to understand why things are happening, not just that
    they are happening.

    $ is for scalars. Even with $hash{some_key} and $array[0] we're
    talking about one entry of the group, a scalar.

    is for when we are talking about an array as a whole:

    my array # create array

    scalar array # get length of array

    array = _ # copy array

    % is similar, for the hash as a whole. For example, we don't want the
    keys() of one entry of a hash, that doesn't make sense. We want the
    keys() of an entire hash, so we call it with:

    keys %some_hash

    With sub routines, everything passed is folded into an array _. We do
    that so subroutines can handle varying numbers of parameters with ease.
    Because of that, if we pass an array and a scalar, they are going to
    end up in _ together and we need to separate them back out on the
    inside. We've seen different ways to do that above, and references
    provide yet another way.

    Hopefully some of this makes sense and helps get you over the hump.
    Hang in there.

    James

    James Guest

  5. #5

    Default search and replace array variables contents

    Dear sir,


    I have two array variables. I want to find $a[1] and replace $b[1] in a
    file.
    ($a[1],$b[1] are array variables)
    How to find and replace the variable contents.

    for ($i=0;$i<$totalnum;$i++){
    s/$a[i]/$b[i]/g;
    }

    Is it possible to do search and replace or kindly suggest me an idea


    Thanking you

    Regards
    Baskaran NK

    Baskaran Guest

  6. #6

    Default Re: search and replace array variables contents

    On Tue, 2004-07-20 at 15:42, Baskaran wrote: 


    You have almost got it

    #!/usr/bin/perl


    # These are your arrays

    my a = ( .... );
    my b = ( .... );

    my $filecontent;
    if(open(IN,"FILENAME")){
    local($/)=undef;
    $filecontent = <IN>;
    close IN;
    } else {
    die "error opening file $!";
    }

    # Now do the substitue
    for ($i=0;$i<a;$i++){
    $filecontent =~ s/$a[i]/$b[i]/g;
    }

    # Write back to file
    open(OUT,">FILENAME") || die "blah";
    print OUT $filecontent;
    close OUT;

    #Bye

    ########################

    HTH
    Ram


    Ramprasad Guest

  7. #7

    Default Re: Another Perl datatype headache ( scalars $, hashes %, and arrays )

    On Jul 19, 2004, at 9:46 AM, James Edward Gray II wrote:
     
    >
    > Whether it is or not, it's not the answer to your problems.
    >
    > Start trying to make sense of the world you find yourself in. There
    > are rules. Try to understand why things are happening, not just that
    > they are happening.
    >
    > $ is for scalars. Even with $hash{some_key} and $array[0] we're
    > talking about one entry of the group, a scalar.
    >
    > ...
    > % is similar, for the hash as a whole. For example, we don't want the
    > keys() of one entry of a hash, that doesn't make sense. We want the
    > keys() of an entire hash, so we call it with:
    >
    > keys %some_hash
    >
    > ....
    >
    > Hopefully some of this makes sense and helps get you over the hump.
    > Hang in there.
    >
    > James
    >
    >[/ref]

    Thanks a lot for your advice James, I found what you said, especially:
     

    to be encouraging, yet thought-provoking.

    I'll have to keep the following quote in mind:
    "Without rules, there is chaos" ~ anonymous

    Gohaku Guest

Similar Threads

  1. arrays/hashes
    By Jeffrey in forum PERL Beginners
    Replies: 3
    Last Post: February 19th, 07:41 PM
  2. Replies: 0
    Last Post: November 13th, 06:58 PM
  3. #26229 [Csd->Opn]: getIterator() segfaults when it returns arrays or scalars.
    By adam at trachtenberg dot com in forum PHP Development
    Replies: 0
    Last Post: November 13th, 04:32 PM
  4. How to store arrays in hashes or objects?
    By Richard Heintze in forum PERL Beginners
    Replies: 2
    Last Post: October 29th, 05:29 PM
  5. performance arrays vs hashes
    By Thomas in forum PERL Miscellaneous
    Replies: 12
    Last Post: October 24th, 03:02 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139