accessing array

This code produces the following 3 lines:
foreach $twords( [at] topTypes)
{
$output.="<p>".commify_series( [at] $twords)."</p>";
}

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.="<p>".commify_series($topTypes[0])."</p>";
$output .= <<HTML;
It produces:
ARRAY(0x9045bf0)

If I try adding the [at] : $output.="<p>".commify_series( [at] $topTypes[0])."</
p>";
There's no output.

How do I just get the first line?


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
rodeored [ Do, 02 Juni 2011 00:23 ] [ ID #2060416 ]

Re: accessing array

On Wed, Jun 1, 2011 at 18:23, rodeored <info1 [at] reenie.org> wrote:

> If I try adding the [at] : $output.="<p>".commify_series( [at] $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 ' [at] ') is binding tighter than the subscripting operation
(the trailing '[0]'). Be a bit more explicit with the dereference:

use strict;
use warnings;

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

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

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


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 [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
John SJ Anderson [ Do, 02 Juni 2011 02:27 ] [ ID #2060417 ]

Re: accessing array

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

On Jun 1, 2011 8:16 PM, "rodeored" <info1 [at] reenie.org> wrote:
>
> This code produces the following 3 lines:
> foreach $twords( [at] topTypes)
> {
> $output.="<p>".commify_series( [at] $twords)."</p>";
> }
>
> 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 [at] $twords

> $output.="<p>".commify_series($topTypes[0])."</p>";
> $output .= <<HTML;
> It produces:
> ARRAY(0x9045bf0)
>

^^ is an array reference. Not an array.

> If I try adding the [at] : $output.="<p>".commify_series( [at] $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--
Shawn Wilson [ Do, 02 Juni 2011 02:31 ] [ ID #2060418 ]

Re: accessing array

On Thursday 02 Jun 2011 01:23:23 rodeored wrote:
> This code produces the following 3 lines:
> foreach $twords( [at] topTypes)
> {
> $output.="<p>".commify_series( [at] $twords)."</p>";
> }
>

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 [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Shlomi Fish [ Do, 02 Juni 2011 07:39 ] [ ID #2060420 ]

Re: accessing array

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

On Jun 2, 2011 1:40 AM, "Shlomi Fish" <shlomif [at] iglu.org.il> wrote:
>
> On Thursday 02 Jun 2011 01:23:23 rodeored wrote:
> > This code produces the following 3 lines:
> > foreach $twords( [at] topTypes)
> > {
> > $output.="<p>".commify_series( [at] $twords)."</p>";
> > }
> >
>
> 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 [at] twords contained something like:
"<div src=javascript.do_something>"
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--
Shawn Wilson [ Do, 02 Juni 2011 07:59 ] [ ID #2060422 ]
Perl » gmane.comp.lang.perl.beginners » accessing array

Vorheriges Thema: perl code for if-elsif-else logic
Nächstes Thema: whats the purpose of use Data::Dumper;