Beginner need help on perl issues

Discussion in 'Perl Misc' started by Petterson Mikael, May 27, 2004.

  1. Hi,

    What is the meaning of

    my ($common,$local) = @_; in sub?

    sub insert_or_update
    {
    my ($common,$local) = @_;

    .....

    }

    Also I have a regular expression looking like the following ( see
    below).
    I does it on a file containing key=value.
    Can anyone explain what is done?

    foreach (@common_lines)
    {
    if (($common_key,$common_value) = /^\s*([^=#]+?)\s*=\s*(.*)$/)
    {
    $common_map{"$common_key"} = "$common_value";
    }
    }

    All hints appreciated.

    BR

    //Mikael
    Petterson Mikael, May 27, 2004
    #1
    1. Advertising

  2. Petterson Mikael wrote:
    > What is the meaning of
    >
    > my ($common,$local) = @_; in sub?
    >
    > sub insert_or_update
    > {
    > my ($common,$local) = @_;


    If you pass arguments to that sub by calling it like:

    insert_or_update('arg1', 'arg2');

    those arguments will become elements in the @_ array. I leave it to
    you to guess the rest. ;-)

    Suggested reading:

    http://www.perldoc.com/perl5.8.4/pod/perlintro.html

    http://www.perldoc.com/perl5.8.4/pod/perlsub.html

    > Also I have a regular expression looking like the following ( see
    > below).
    > I does it on a file containing key=value.
    > Can anyone explain what is done?
    >
    > foreach (@common_lines)
    > {
    > if (($common_key,$common_value) = /^\s*([^=#]+?)\s*=\s*(.*)$/)
    > {
    > $common_map{"$common_key"} = "$common_value";
    > }
    > }


    It populates the hash %common_map with the key/value pairs.

    Note that throwing out random questions like those, without showing
    that you have made own efforts to find the answers, is not a
    reasonable way to learn basic Perl. If you want to learn Perl, this is
    a suitable starting point:

    http://learn.perl.org/

    And before posting to this newsgroup again, please study the posting
    guidelines:

    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

    Good luck!

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, May 27, 2004
    #2
    1. Advertising

  3. Petterson Mikael

    Greg Bacon Guest

    In article <>,
    Petterson Mikael <> wrote:

    : What is the meaning of
    :
    : my ($common,$local) = @_; in sub?

    Try it out. Consider the following code:

    @_ = ('apples', 'oranges', 'bananas');

    my ($common,$local) = @_;

    print "common = $common\n",
    "local = $local\n";

    It produces the following output:

    common = apples
    local = oranges

    As you can see, it copies the first two items off the @_ array.

    Read the perldata manpage. This is documented in the section on
    lists.

    : Also I have a regular expression looking like the following ( see
    : below). I does it on a file containing key=value.
    : Can anyone explain what is done?
    :
    : foreach (@common_lines)
    : {
    : if (($common_key,$common_value) = /^\s*([^=#]+?)\s*=\s*(.*)$/)
    : {
    : $common_map{"$common_key"} = "$common_value";
    : }
    : }

    Piece by piece:

    ^ - "At the beginning of the line..."

    \s* - "...optional whitespace followed by..."

    ( - "...(and remember what matches here)..."

    [^=#]+? - "...match one or more chars that aren't = or #..."

    ) - "...return this value..."

    \s* - "...followed by optional whitespace..."

    = - "...followed by an equals sign..."

    \s* - "...followed by optional whitespace..."

    ( - "...(and remember what matches here)...

    .* - "...whatever's left on the line..."

    ) - "...also return this value if we match..."

    $ - "...followed by end of line."

    On a match, the regular expression match operator, i.e., /.../,
    returns the portions of the input text -- in this case, each element of
    @common_lines in turn -- that matched the parenthesized subexpressions,
    i.e., ([^=#]+?) and (.*) in this case.

    Hope this helps,
    Greg
    --
    Calculating the cost per MW received is left as an exercise for those
    readers who know how. I don't think that includes you, or you wouldn't
    have posted this.
    -- Paul Robichaux in hsv.general
    Greg Bacon, May 27, 2004
    #3
  4. Petterson Mikael <> writes:

    Please put the subject of your post in the Subject: of your post.
    Think of it this way: suppose you were searching for answers to your
    questions in some newsgroup archive like groups.google.com. Would you
    be able to figure out that this post was relevant, just by looking at
    the Subject: line?

    > What is the meaning of
    >
    > my ($common,$local) = @_; in sub?


    Read 'perlintro' (with 'perldoc perlintro'). You can probably stop
    doing everything else you're doing with Perl until you finish that,
    it's that basic.

    > Also I have a regular expression looking like the following ( see
    > below).
    > I does it on a file containing key=value.
    > Can anyone explain what is done?


    You'll want to read 'perlretut' (for "Perl Regex Tutorial") with
    'perldoc perlretut'. This one's pretty simple, so I'll help you
    decode it, but you'll really want to read perlretut

    > foreach (@common_lines)
    > {
    > if (($common_key,$common_value) = /^\s*([^=#]+?)\s*=\s*(.*)$/)


    Let's write it out the long way:

    if (my ($common_key,$common_value) = /^\s*

    This says there is optional whitespace at the beginning of the line. \s
    matches any white space character, and * means there are zero or more of them.
    If you wanted to guarantee there was at least one whitespace character here,
    then you'd use + instead of *
    ([^=#]+?)

    Followed by a number of characters that are not = or #. The parens put
    these matched characters into $1 (because it's the first set of parens)

    \s*
    Followed by optional whitespace

    =

    a literal = character

    \s*

    more optional whitespace

    (.*)

    and capture everything after the whitespace into $2 (since it's the

    second set of parens
    $/x))

    and $ matches the end of the string. I added /x because /x says to
    ignore whitespace in the regex, and you could just rip out my comments
    and have an exact equivalent to the regex you quoted.

    Now, the bits on the left hand side, as written, are:

    ($common_key, $common_value) = /...regex.../;

    You should always put 'use warnings;' and 'use strict;' at the top of
    all your Perl programs. This requires you to declare your variables,
    so you'd write instead,

    my ($common_key, $common_value) = /...regex.../;

    The fact that the variables $common_key and $common_value are in
    parentheses means that they are in list context. Perl does different
    things sometimes depending on if an operator like // is in scalar
    context or list context.

    Now, I don't want to give away all the fun, and besides you could
    probably stand to learn the practice of looking up the docs on your
    own, so why don't you search perlretut for what happens when // is
    evaluated in list context, and let us know what you find out? You
    should be able to find the documentation easily, but if you can't
    understand it, post back, and we'll help you work through it.

    -=Eric
    --
    Come to think of it, there are already a million monkeys on a million
    typewriters, and Usenet is NOTHING like Shakespeare.
    -- Blair Houghton.
    Eric Schwartz, May 27, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    550
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. Rensjuh
    Replies:
    7
    Views:
    938
    Mabden
    Sep 2, 2004
  3. Anime Junkie

    Beginner issues

    Anime Junkie, Dec 24, 2006, in forum: Ruby
    Replies:
    4
    Views:
    83
    Anime Junkie
    Dec 24, 2006
  4. william nelson

    Beginner's Beginner

    william nelson, Apr 11, 2011, in forum: Ruby
    Replies:
    7
    Views:
    191
    7stud --
    Apr 12, 2011
  5. George Monappallil

    Perl script issues - Need help

    George Monappallil, Jan 21, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    78
    Tad McClellan
    Jan 21, 2005
Loading...

Share This Page