perl sub and perl modules

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default perl sub and perl modules

    Hi all,


    I am new in perl and I am having some difficulties in getting used to it.
    I have an array of array:

    my @hap = (
    [qw/ a c g t g c/],
    [qw/ c g ? t a g/],
    [qw/ a c t t ? c/]);


    and as during the process I will need to print how this evolves during the
    process, I tried to place a print sub:


    sub MyPrint {
    my (@arr) =@_;

    for my $i (0..$#arr){
    for my $j (0..$#{$arr[$i]}){
    print $arr[$i][$j]."\t";
    }
    print "\n";
    }
    }


    if I call my print as reference:

    MyPrint(\@hap);

    I get:
    ARRAY(0x541c) ARRAY(0x7b944) ARRAY(0x7b884)

    instead if I pass the array as value:

    MyPrint(@hap);

    I get the answer I was trying to get.

    a c g t g c
    c g ? t a g
    a c t t ? c


    But As I think that passing as value (creates a variable that we are not
    going to use anymore ) I think the best idea was passing by reference,
    but..

    How I do that?


    Alternatively I was thinking to place My sub in a module. How this will
    work on that situation?


    Thanks in advance for you help

    P
    fabre Guest

  2. Similar Questions and Discussions

    1. Which Perl modules to use
      Which Perl modules should I use for the following task? I need to pop email messages off of a POP3 email server that requires an SSL connection. ...
    2. Perl modules on Mac OS X
      Hi fellows, I have a funny problem: When installing my OS (Mac OS 10.3), I did not realize that the 'make' utility was part of the developer tools...
    3. Re : Installing CPAN Perl Modules with Activestate Perl 5. v5.8
      Hi, In the process of trying to get perl modules installed, I downloaded over 300 Activestate specific perl modules and they work fine (of the ones...
    4. How Can I Install PERL Modules w/o Upgrading PERL?
      Shalom! I absolutely hate it when I install a required module for one piece of software or another, and suddenly it begins installing the...
    5. Perl modules
      I´m a Tripod user. (no flames, please). Now Tripod does not give access to some modules (I need vars and strict), but ask the users to upload them...
  3. #2

    Default Re: perl sub and perl modules

    fabre <pedro.fabre@gen.gu.se> wrote in comp.lang.perl.misc:
    > Hi all,
    >
    >
    > I am new in perl and I am having some difficulties in getting used to it.
    > I have an array of array:
    >
    > my @hap = (
    > [qw/ a c g t g c/],
    > [qw/ c g ? t a g/],
    > [qw/ a c t t ? c/]);
    >
    >
    > and as during the process I will need to print how this evolves during the
    > process, I tried to place a print sub:
    >
    >
    > sub MyPrint {
    > my (@arr) =@_;
    >
    > for my $i (0..$#arr){
    > for my $j (0..$#{$arr[$i]}){
    > print $arr[$i][$j]."\t";
    > }
    > print "\n";
    > }
    > }
    That's more complicated than it has to be. Don't use array indices in
    Perl unless you have to. Here is a simplified version:

    sub MyPrint {
    for ( @_ ) {
    print join( "\t", @$_), "\n";
    }
    }

    >
    >
    > if I call my print as reference:
    >
    > MyPrint(\@hap);
    > I get:
    >
    > ARRAY(0x541c) ARRAY(0x7b944) ARRAY(0x7b884)
    >
    > instead if I pass the array as value:
    >
    > MyPrint(@hap);
    >
    > I get the answer I was trying to get.
    >
    > a c g t g c
    > c g ? t a g
    > a c t t ? c
    >
    Sure. That calls MyPrint with a single scalar parameter, which is
    a listref. The way you layed out the routine, it expects a list
    (of listrefs).
    > But As I think that passing as value (creates a variable that we are not
    > going to use anymore ) I think the best idea was passing by reference,
    > but..
    >
    > How I do that?
    To make it work with a reference, change the loop control in "for"

    sub MyPrint {
    for ( @{ shift()} ) {
    print join( "\t", @$_), "\n";
    }
    }
    > Alternatively I was thinking to place My sub in a module. How this will
    > work on that situation?
    You would define the routine in a module exactly the way you do it
    in your main file.

    Anno
    Anno Siegel Guest

  4. #3

    Default Re: perl sub and perl modules

    But in the case I want to change the values to upper case or modify them,
    then I will need to use indexes in perl. right?


    sub toUpperCase {
    my (@arr) =@_;

    for my $i (0..$#arr){
    for my $j (0..$#{$arr[$i]}){
    $arr[$i][$j] =~ tr/[a-z]/[A-Z]/; # convert to upper case
    }
    }
    }

    that doesn't work either.

    how this will work passing as references?

    Thanks

    P

    PS: I cam from java and migrate to perl seems to be not an easy task!

    In article <bkc7j5$2un$1@mamenchi.zrz.TU-Berlin.DE>,
    [email]anno4000@lublin.zrz.tu-berlin.de[/email] (Anno Siegel) wrote:
    > fabre <pedro.fabre@gen.gu.se> wrote in comp.lang.perl.misc:
    > > Hi all,
    > >
    > >
    > > I am new in perl and I am having some difficulties in getting used to it.
    > > I have an array of array:
    > >
    > > my @hap = (
    > > [qw/ a c g t g c/],
    > > [qw/ c g ? t a g/],
    > > [qw/ a c t t ? c/]);
    > >
    > >
    > > and as during the process I will need to print how this evolves during the
    > > process, I tried to place a print sub:
    > >
    > >
    > > sub MyPrint {
    > > my (@arr) =@_;
    > >
    > > for my $i (0..$#arr){
    > > for my $j (0..$#{$arr[$i]}){
    > > print $arr[$i][$j]."\t";
    > > }
    > > print "\n";
    > > }
    > > }
    >
    > That's more complicated than it has to be. Don't use array indices in
    > Perl unless you have to. Here is a simplified version:
    >
    > sub MyPrint {
    > for ( @_ ) {
    > print join( "\t", @$_), "\n";
    > }
    > }
    >
    >
    > >
    > >
    > > if I call my print as reference:
    > >
    > > MyPrint(\@hap);
    > > I get:
    > >
    > > ARRAY(0x541c) ARRAY(0x7b944) ARRAY(0x7b884)
    > >
    > > instead if I pass the array as value:
    > >
    > > MyPrint(@hap);
    > >
    > > I get the answer I was trying to get.
    > >
    > > a c g t g c
    > > c g ? t a g
    > > a c t t ? c
    > >
    >
    > Sure. That calls MyPrint with a single scalar parameter, which is
    > a listref. The way you layed out the routine, it expects a list
    > (of listrefs).
    >
    > > But As I think that passing as value (creates a variable that we are not
    > > going to use anymore ) I think the best idea was passing by reference,
    > > but..
    > >
    > > How I do that?
    >
    > To make it work with a reference, change the loop control in "for"
    >
    > sub MyPrint {
    > for ( @{ shift()} ) {
    > print join( "\t", @$_), "\n";
    > }
    > }
    >
    > > Alternatively I was thinking to place My sub in a module. How this will
    > > work on that situation?
    >
    > You would define the routine in a module exactly the way you do it
    > in your main file.
    >
    > Anno
    fabre Guest

  5. #4

    Default Re: perl sub and perl modules

    fabre <pedro.fabre@gen.gu.se> wrote:

    [ don't top post ]
    > But in the case I want to change the values to upper case or modify
    > them, then I will need to use indexes in perl. right?
    No!

    It's much easier (and more efficient) to loop over the arrays directly,
    rather than looping over temporary lists of indices.

    sub to_upper {
    for my $ref (@_) {
    $_ = uc for @$ref;
    }
    }
    > PS: I cam from java and migrate to perl seems to be not an easy task!
    You just have to lose your taste for doing things the "hard way".

    --
    Steve
    Steve Grazzini Guest

  6. #5

    Default Re: perl sub and perl modules


    [ Please do not top-post. Please do not full-quote. ]


    fabre <pedro.fabre@gen.gu.se> wrote:
    > But in the case I want to change the values to upper case or modify them,
    > then I will need to use indexes in perl. right?

    Wrong.

    > sub toUpperCase {
    > my (@arr) =@_;
    >
    > for my $i (0..$#arr){
    > for my $j (0..$#{$arr[$i]}){
    > $arr[$i][$j] =~ tr/[a-z]/[A-Z]/; # convert to upper case

    tr/// does not respect locales, so it does NOT convert to upper case,
    it converts characters.

    Why are you asking tr/// to replace "[" with "[" and "]" with "]" ?

    > }
    > }
    > }
    >
    > that doesn't work either.

    It worked when I tried it, so I can't help you with that part.

    > PS: I cam from java and migrate to perl seems to be not an easy task!

    Perl has many operators that operate on lists (such as foreach), so
    you very seldom _need_ to do explicit indexing yourself.

    You are human, you may make a mistake.

    perl is a machine, it will do the right thing everytime.

    You should catch yourself whenever you find yourself doing explict
    indexing, you are likely thinking in some other language and there
    is a more Perlish way of accomplishing the same thing.

    You can rewrite your uppercasing to have perl do the indexing
    for you too:

    -----------------------------------------
    #!/usr/bin/perl
    use strict;
    use warnings;
    use Data::Dumper;

    my @hap = (
    [qw/ a c g t g c/],
    [qw/ c g ? t a g/],
    [qw/ a c t t ? c/]);

    toUpperCase(@hap);
    print Dumper \@hap;


    sub toUpperCase {
    my (@arr) =@_;

    foreach my $aref ( @arr ){
    foreach my $value ( @$aref ){
    $value = uc $value;
    }
    }
    }
    -----------------------------------------




    [ snip 90 lines of TOFU ]

    --
    Tad McClellan SGML consulting
    [email]tadmc@augustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

Posting Permissions

  • You may not post new threads
  • You may 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