Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
$pi = split('\t', $PAR1_info[0]);

Split returns an array. When you assign an array to a scalar, the array is evaluated in a scalar context and an array evaluated in a scalar context gives the number of elements in the array, not the first element of the array.

There are, as usual with Perl, several ways to get the first element. Here are a couple:

my ($pi) = split(/\t/, $PAR1_info[0]);

my $pi = (split(/\t/, $PAR1_info[0])[0];

Note also that split takes a regular expression (pattern) as its first argument, not a string.

Edit: Some days I should stay away from my keyboard....

As dave_the_m points out, what I said about split returning an array is incorrect. In fact (as is generally the case with functions) what split does depends on the context in which it is evaluated. Split does various things differently when evaluated in scalar context rather than list or void context. Of relevance here, from split:

Splits the string EXPR into a list of strings and returns the list in list context, or the size of the list in scalar context.

And, while split is documented to take a pattern, that pattern can be a string.

In reply to Re^3: Assigning Variables to String Elements by ig
in thread Assigning Variables to String Elements by ccelt09

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (5)
    As of 2020-10-21 13:17 GMT
    Find Nodes?
      Voting Booth?
      My favourite web site is:

      Results (217 votes). Check out past polls.