hitesh has asked for the wisdom of the Perl Monks concerning the following question:

I am newbie to perl and need help with array syntax: I have a file which has following: regionid~staffid~fname~lname~title|| I need to create a array which holds all the staffid (ie. second parameter). Can someone help me with this? Thanx. -Hitesh

Replies are listed 'Best First'.
Re: Syntax for array?
by I0 (Priest) on Jul 10, 2001 at 05:46 UTC
    while(<>){ push @array,(split/~/)[1]; }
      And an explanation, as you're a newbie:

      while(<>) {

      This bit reads every line from your file (if you've specified your file name on the command line, e.g. "./myscript.pl filename.txt")

      push @array,(split /~/)[1];

      split /~/ takes the current line, and splits it into an array on the ~ character.

      (split /~/)[1] takes the second element of that array. That's staffid, the bit you want. Similarly print( ('a','n','d')[1] ) would print "n".

      push @array, ... just adds this to your array.

      dave hj~