Results 1 to 3 of 3

Thread: Variables and data structure in Perl

  1. #1
    Join Date
    Sep 2010
    Posts
    73

    Variables and data structure in Perl

    In the script in perl, the data are held in temporary variables, they behave roughly as environment variables, but are not the same thing. The main type of variable in Perl is the scalar. A scalar is a string or a number, which does not need to be converted as in C. Moreover, all scalars can be integers or floating point numbers, which perl will try to be as full, if possible, to improve the speed of execution.

    Each variable has a name, which always begins with a special character, indicating its type, followed by the name of at least one letter. All variable names are case-sensitive, so $variabile is not equal to $Variabile and $VaRiAbIlE. The special character that is talked about is the dollar sign ( $ ), and is used both when assigning a variable, and when you use its value.

    Here are some examples:

    Assign the integer 1 to the scalar variable $ blah
    $ Blah = 1;

    Assign the string "1" for $ blah, which is equivalent to over
    $ Blah = 1;

    Ops worth $ 2
    Ops $ bla = $ +1;

    Show "hello" (\ n is a carriage return)
    $ Blah = "hello \ n";
    print $ blah;

    Within a shell script, the character $ is used only when you request the value of a variable, not when you assign:
    bla = 1
    echo $ bla

    In the script, the strings are enclosed in quotes ( '' ) or double quotes ( "" ), except that in double quotes ( "" ) variables are interpolated, while single quotes ( '' ) variables will be considered literally.

  2. #2
    Join Date
    Sep 2010
    Posts
    73

    Re: Variables and data structure in Perl

    Whenever an ambiguity can arise as long as may be the name of a variable, $variabile can be written as ${variabile}.

    View "uff"
    $ Bla = "eeek!"
    $ Blah = "phew!"
    print "$ blabla \ n";

    shows "eeek! bla"
    print "$ (bla bla) \ n"

    $ Msg contains "eeek! You scared me"
    $ Bla = "eeek!"
    $ Msg = "$ bla You scared me";

    $ MSG2 contains "$ bla me scared
    $ MSG2 = '$ bla You scared me';

    In perl, as well as in shell scripting (and unlike C) variables need not be declared before being used: The first time you use a variable, you declare. Groped to read a nonexistent value leads to an empty string, in numeric context, is interpreted as 0.

    The variables whose name begins (after the canonical $ ) with a special character, which is not a letter, are considered 'sensitive' (the list can be found by typing man perlvar with perl5). These variables change some behaviors of perl depending on its value. One of these is the variable $_ , which is left free to allow the user to input data to be temporary, and is also useful in another respect: many functions operate on $_ if it has not provided any value.

    displays the contents of $ _
    $ _ = "Bla bla bla \ n";
    print;

    Not all functions take $_ as a parameter by default, even if this works with all the functions where it makes sense to take the variable $_ . If in doubt, you can use a small script a few lines to see if that affects the function can work with $_ .

  3. #3
    Join Date
    Sep 2010
    Posts
    73

    Re: Variables and data structure in Perl

    Other important variables are reserved:


    Another type is the array, an array is a numbered list of scalars. The array is identified by the at sign ( @ ) in the same ways that dollar sign ( $ ) indicates the scale.

    All arrays are numbered from 0: the first element take index 0, the second one and so on. The elements are placed in an array as a list separated by commas and parentheses. The n-th element of the generic array @array is defined as $array[n] . In this case, you use $ instead of @ because it refers to a scalar, not the entire array.

    A few examples follow. Let us first define a variable $var :
    $ Var = 1;

    Let us now see how to define an array. An element is defined with a value of $var :
    @ Array = ("bla", 34 "uff", $ var);

    Now we take the second value of the array and assign the variable $second, renumbered 34 :
    $ Seconds = $ array [1];

    Now we define a new array @eek , that takes a subset of the elements of @array , as defined above:
    Eek @ = @ array [0, 2];

    @eek becomes ("bla", "uff") , since it takes the first and third value of @array .

    Let's see how to connect two arrays

    Urka @ = (@ array, @ eek);

    This is NOT an array of arrays, since arrays are always an array of scalars. What happens is that all values of @array and @eek are copied @urka, which contains ("bla", 34, "uff", 1, "bla", "uff").

    There are functions that operate on arrays, adding elements to the end, remove, removing those that satisfy a given condition, orders etc.. To find the cardinality of an array, you use the hash sign ( # ) after the at sign. For example, the expression @#array is the number of elements of @array minus one. To empty the array, this is taken as -1.

    @ Array = ("bla" "bla" "bla" "bla");
    print $ # array, "\ n";


    Note: The pound sign ( # ) in this case ( $#array ) does not introduce a comment.

Similar Threads

  1. Program Structure in Perl
    By Iker in forum Software Development
    Replies: 5
    Last Post: 27-01-2011, 04:22 PM
  2. What is bubble sort in data structure
    By punguzhali in forum Software Development
    Replies: 3
    Last Post: 04-01-2011, 03:49 AM
  3. What do you mean by Merge sort in data structure
    By fLUTE in forum Software Development
    Replies: 3
    Last Post: 04-01-2011, 02:12 AM
  4. What do you mean by stacks in Data structure
    By Shiva$m in forum Software Development
    Replies: 3
    Last Post: 04-01-2011, 12:20 AM
  5. Choice of data structure
    By Hashim in forum Software Development
    Replies: 5
    Last Post: 02-11-2009, 07:27 PM

Tags for this Thread

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
  •  
Page generated in 1,716,559,101.72051 seconds with 17 queries