Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Its just avoiding grep; again a trivial soln.

cool, I know that u r c00l and all, but could u plz stop im-talking? Many find it plain annoying and it hinders communication between people here. It is appropriate where it is appropriate, that is in IMs et simila, in which case your primary goal is speed and not clarity. But here it's just the opposite.

my $x=1; foreach(@arr){print $x++ if (/foo/)}

Initialization and "efficiency" issues apart, which were duly pointed out by davido, just reasoning solely in terms of user interaction, what benefit could come of incrementally printing the counter at each iteration? You're only interested in the final value anyway. Not to mention the /foo/ gotcha mentioned several times in this thread.

But it can be done using spl variable of reg ex also, if I am right?? Any takers?

cool, I know that u r c00l and all, but could u plz stop im-talking?

my $str=join ' ',@arr;

That is just like my $str="@arr"; that is, unless you've changed $". And if you haven't then it's a very convenient idiom. If you have, then you should have done so locally in a block anyway, unless yours is a very very special situation.

$str=~ /foo/;

That is just like "@arr" =~ /foo/; no need for an intermediate variable.

print $&; #### In place of $&; we can use that for no #### for no. of matches.

I understand what you mean, but:

  • your match is not a global one (you have to use the /g modifier for that), so the number of matches will always be at most one;
  • no, there's not such a variable and no need for it, since a match in global context will return the list of all the matches (or of all the captures if capturing parens are there) and one can use that list in scalar context to get the number of them.

In reply to Re^2: Word frequency in an array by blazar
in thread Word frequency in an array by monkeybus

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: (2)
As of 2024-04-26 06:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found