Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

First as atcroft pointed out, you most likely wanted to m// an array element, not the array (or the size of the array in fact).

Also, what if your array has less than 100 elements, in that case you would get error saying that you used unintialized value in m//. BTW, please use strict.

You probably want something like this: (your code indicated that you want to look at the first 100 elemnts only, I assume this is true)

use strict; use warnings; my @number = (1,2,3,9432, 5,6,7); my $count=0; for (0 .. (($#number < 99) ? $#number : 99)) { if ($number[$count]=~/^9432$/g) { print "match found\n"; } else { print "match failed\n"; } $count++; }

Now a little bit more on m// with array. the m// will actually be performed on scalar(@array), which is the number of elements:

use strict; use warnings; my @number1 = (2 .. 9432); my @number2 = (2 .. 9433); #I intentionally avoided the use of (1.. 94 +32) here, to remove the doubt whether it compares against the last el +ement of the array. my @number3 = (2 .. 9434); my $count=0; if (@number1 =~ /^9432$/g) { print "match found\n"; } else { print "match failed\n"; } if (@number2 =~ /^9432$/g) { print "match found\n"; } else { print "match failed\n"; } if (@number3 =~ /^9432$/g) { print "match found\n"; } else { print "match failed\n"; }

In reply to Re: help manipulating data in an array. by pg
in thread help manipulating data in an array. by nightfreightpilot

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-20 01:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found