Home > Archive > PERL Miscellaneous > May 2005 > a simple question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Raghavendra Mahuli 2005-05-30, 8:57 am |
| Hi all,
I have a simple question in perl... I have an array -
@a = (10,20,30,40);
Since '$' gives the scalar of a array , i expect "print $a" to give the no.
of elements in the array...
But it dosent work.. However if i copy it to a scalar and then print it, it
works.. Can u pls explain why it is so?
Here is the program listing:
@a = (10,20,30,40);
$x= @a;
print $a;
print $x;
thanx in advance,
regards,
raghu
| |
| Bernard El-Hagin 2005-05-30, 8:57 am |
| "Raghavendra Mahuli" <raghavendra.ma@in.bosch.com> wrote:
> Hi all,
> I have a simple question in perl... I have an array -
>
> @a = (10,20,30,40);
>
> Since '$' gives the scalar of a array , i expect "print $a" to
> give the no. of elements in the array...
> But it dosent work..
$a has nothing whatsoever to do with @a. They are completely different
variables.
--
Cheers,
Bernard
| |
| Brian McCauley 2005-05-30, 8:57 am |
|
Raghavendra Mahuli wrote:
> Subject: a simple question
Please put the subect of your post in the Subject of your post.
> Hi all,
> I have a simple question in perl... I have an array -
>
> @a = (10,20,30,40);
>
> Since '$' gives the scalar of a array ,
No it doesn't. I'm not sure what you mean. Do you mean $a is the same
as scalar(@a) ? No, that's not true.
> i expect "print $a" to give the no.
> of elements in the array...
No, @a and $a are two completely separate variables.
I have to confess this is rather messy in Perl5 - apparently it'll all
be different in Perl6.
In Perl5 @a is the array called 'a' and $a is a separate scalar variable
also called 'a'. Confusingly $a[0] is the first element of @a and has
nothing to do with $a. $#a is the last subscript of @a.
This is all explained in some detail in the perldata manual.
| |
| vnick@freenet.de 2005-05-30, 8:57 am |
| $x=@a is the evaluation of an array (a list) in a scaler context which
gives by definition the number of array elements whereas this 'print
"$a"' prints the scalar var (no evaluation within " ") $a which has
nothing to do with @a.
On the contrary you can define $a and @a at the same time not affecting
each other.
To get the number of elements of an array use
$#a + 1 or
scalar(@a) or
print @a ."\n"; (but not: print "@a\n"; !!!)
Hope that helps.
| |
| Raghavendra Mahuli 2005-05-30, 8:57 am |
| Thanx for all the responses....
"Brian McCauley" <nobull@mail.com> wrote in message
news:d7enr0$ml8$1@slavica.ukpost.com...
>
>
> Raghavendra Mahuli wrote:
>
>
> Please put the subect of your post in the Subject of your post.
>
> No it doesn't. I'm not sure what you mean. Do you mean $a is the same
> as scalar(@a) ? No, that's not true.
>
>
> No, @a and $a are two completely separate variables.
>
> I have to confess this is rather messy in Perl5 - apparently it'll all
> be different in Perl6.
>
> In Perl5 @a is the array called 'a' and $a is a separate scalar variable
> also called 'a'. Confusingly $a[0] is the first element of @a and has
> nothing to do with $a. $#a is the last subscript of @a.
>
> This is all explained in some detail in the perldata manual.
| |
| George 2005-05-30, 3:57 pm |
| Raghavendra Mahuli wrote:
> Hi all,
> I have a simple question in perl... I have an array -
>
> @a = (10,20,30,40);
>
> Since '$' gives the scalar of a array , i expect "print $a" to give
> the no. of elements in the array...
> But it dosent work.. However if i copy it to a scalar and then print
> it, it works.. Can u pls explain why it is so?
> Here is the program listing:
>
> @a = (10,20,30,40);
> $x= @a;
> print $a;
> print $x;
>
> thanx in advance,
> regards,
> raghu
In perl your best friend is
use strict;
use warnings;
| |
| Chris Mattern 2005-05-30, 3:57 pm |
| Raghavendra Mahuli wrote:
> Hi all,
> I have a simple question in perl... I have an array -
>
> @a = (10,20,30,40);
>
> Since '$' gives the scalar of a array
No, it doesn't. $a and @a are two different variables that have nothing
to do with each other.
> , i expect "print $a" to give the
> no. of elements in the array...
No, because $a has nothing to do with @a.
> But it dosent work.. However if i copy it to a scalar and then print it,
> it works.. Can u pls explain why it is so?
> Here is the program listing:
>
> @a = (10,20,30,40);
> $x= @a;
Evaluates @a in a scalar context and places the value in $x.
> print $a;
Prints a variable that has nothing to do with @a.
> print $x;
Prints the scalar context of @a that you used above.
What you need to do here is get the scalar context of @a in
the print statement. You don't need to do that when assigning
to $x because the fact that you are assigning to a scalar
forces scalar context. Try this:
print scalar(@a);
In fact, you could also say:
$x = scalar(@a);
but you don't need to because Perl recognizes that a conversion
to scalar must be done here.
>
> thanx in advance,
> regards,
> raghu
--
Christopher Mattern
"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
| |
| Tad McClellan 2005-05-30, 3:57 pm |
| vnick@freenet.de <vnick@freenet.de> wrote:
> $x=@a is the evaluation of an array (a list) in a scaler context
^^^^^^^^
[snip everything else, which was all accurate]
You should have left the parenthetical out.
$x=@a is the evaluation of an array in a scalar context.
It is NOT the evaluation of a list in scalar context because:
"There is no such thing as a list in scalar context"
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
| |
| Tad McClellan 2005-05-30, 3:57 pm |
| Raghavendra Mahuli <raghavendra.ma@in.bosch.com> wrote:
> Subject: a simple question
Please put the subject of your article in the Subject of your article.
Have you seen the Posting Guidelines that are posted here frequently?
> i expect "print $a" to give the no.
> of elements in the array...
What part of perl's docs led you to believe that?
> But it dosent work.. However if i copy it to a scalar and then print it, it
> works.. Can u pls explain why it is so?
See the "Context" section in
perldoc perldata
> $x= @a;
Here the name of the array is in *scalar* context.
> print $a;
That variable is not related to @a in any way, despite their
similar-looking names.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
| |
| Tad McClellan 2005-05-30, 3:57 pm |
| Brian McCauley <nobull@mail.com> wrote:
[snip]
> No, @a and $a are two completely separate variables.
>
> I have to confess this is rather messy in Perl5 -
Not if you think about it the way Randal presents it in "Learning Perl",
so let's review that for the lurkers.
As a general rule, when Perl has just one of something,
that's a _scalar_.
...
the "plural" in Perl is represented by lists and arrays.
Which I paraphrase into:
Dollar-sign means you want to access a single thing,
at-sign means you want to access (potentially) more
than one thing.
> apparently it'll all
> be different in Perl6.
apparently it'll all be a different mess in Perl6. :-)
> Confusingly $a[0] is the first element of @a
If you want to access a single (first) element, then it isn't really
very confusing, you use the sigil that means "a single thing".
> and has
> nothing to do with $a.
Now _that_ part can be confusing.
> $#a is the last subscript of @a.
And that flat out *is* confusing, but its the csh's fault, not perl's. :-)
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
| |
| Brian McCauley 2005-05-30, 3:57 pm |
| Tad McClellan wrote:
> Brian McCauley <nobull@mail.com> wrote:
>
>
> Not if you think about it the way Randal presents it in "Learning Perl",
I disagee. Randal does a good job of explaining and maybe even
partially justifying the mess but it's still a mess. :-)
> Which I paraphrase into:
>
> Dollar-sign means you want to access a single thing,
> at-sign means you want to access (potentially) more
> than one thing.
Yes that's a good summary but the number of elements in an array is a
single thing so it doesn't realy do much to explain the destinction
between scalar(@a) and $a which was the OP's problem.
|
|
|
|
|