accessing array

accessing array

am 02.06.2011 00:23:23 von rodeored

This code produces the following 3 lines:
foreach $twords(@topTypes)
{
$output.="

".commify_series(@$twords)."

";
}

Type 7 and Type 8

Type 9, Type 5, and Type 4

Type 2

I only want the first line. But when I try:
$output.="

".commify_series($topTypes[0])."

";
$output .= < It produces:
ARRAY(0x9045bf0)

If I try adding the @: $output.="

".commify_series(@$topTypes[0])." p>";
There's no output.

How do I just get the first line?


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: accessing array

am 02.06.2011 02:27:31 von John SJ Anderson

On Wed, Jun 1, 2011 at 18:23, rodeored wrote:

> If I try adding the @: $output.="

".commify_series(@$topTypes[0])." > p>";
> There's no output.
>
> How do I just get the first line?

What you have there is an array of array references. (Aside: this
would be a great place to use Data::Dumper, to cue off a recent thread
on this list...)

Your final effort isn't working because the dereferencing operation
(the leading '@') is binding tighter than the subscripting operation
(the trailing '[0]'). Be a bit more explicit with the dereference:

use strict;
use warnings;

my @topTypes = (
[ 'Type7' , 'Type8' ] ,
[ 'Type4' , 'Type5' , 'Type6' ] ,
[ 'Type2' ] ,
);

# like so:
print commify_series( @{ $topTypes[0] } );

# too lazy to write the real routine
sub commify_series { join ',' , @_ }


Incidentally, if you were running with 'use strict' and 'use
warnings', your previous attempt would have told you something like:

$ ./try.pl
Global symbol "$topTypes" requires explicit package name at ./try.pl line 14.

which is an indication that you're not really accomplishing what
you're trying to do.

chrs,
john.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: accessing array

am 02.06.2011 02:31:33 von Shawn Wilson

--0022158df33757a9cd04a4afc265
Content-Type: text/plain; charset=ISO-8859-1

On Jun 1, 2011 8:16 PM, "rodeored" wrote:
>
> This code produces the following 3 lines:
> foreach $twords(@topTypes)
> {
> $output.="

".commify_series(@$twords)."

";
> }
>
> Type 7 and Type 8
>
> Type 9, Type 5, and Type 4
>
> Type 2
>
> I only want the first line. But when I try:

You're redefining the $output variable each iteration leaving you with the
last element. You might try something like:
last if defined @$twords

> $output.="

".commify_series($topTypes[0])."

";
> $output .= < > It produces:
> ARRAY(0x9045bf0)
>

^^ is an array reference. Not an array.

> If I try adding the @: $output.="

".commify_series(@$topTypes[0])." > p>";
> There's no output.
>

That's why you want to literate over the list and stop at the first defined
value.

> How do I just get the first line?
>

BTW, use strict; use warnings

--0022158df33757a9cd04a4afc265--

Re: accessing array

am 02.06.2011 07:39:06 von Shlomi Fish

On Thursday 02 Jun 2011 01:23:23 rodeored wrote:
> This code produces the following 3 lines:
> foreach $twords(@topTypes)
> {
> $output.="

".commify_series(@$twords)."

";
> }
>

I should note that there's a risk of HTML-injection / Cross-site-scripting
(XSS) attack here:

http://en.wikipedia.org/wiki/Cross-site_scripting

Regards,

Shlomi Fish

--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Chuck Norris/etc. Facts - http://www.shlomifish.org/humour/bits/facts/

"My name is Inigo Montoya. You forced my father to write XSLT. Prepare to die!
And be thankful I don't force you to write XSLT."

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: accessing array

am 02.06.2011 07:59:17 von Shawn Wilson

--20cf300255b06b073504a4b4563a
Content-Type: text/plain; charset=ISO-8859-1

On Jun 2, 2011 1:40 AM, "Shlomi Fish" wrote:
>
> On Thursday 02 Jun 2011 01:23:23 rodeored wrote:
> > This code produces the following 3 lines:
> > foreach $twords(@topTypes)
> > {
> > $output.="

".commify_series(@$twords)."

";
> > }
> >
>
> I should note that there's a risk of HTML-injection / Cross-site-scripting
> (XSS) attack here:
>
> http://en.wikipedia.org/wiki/Cross-site_scripting
>

You don't mean that if @twords contained something like:
"
"
could be a bad thing? :)

All joking aside, this is one of many reasons I use tt on the back end and
esapi on the front end. I probably don't test as much as I should and might
get got. But it is good practice to find apis that were built with security
in mind and use them.

.... and for my next trick, I will redo RSA's ECC PKI in 100% perl :)

--20cf300255b06b073504a4b4563a--