Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

split giving warning

by Anonymous Monk
on Mar 22, 2004 at 04:57 UTC ( [id://338549]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks , I keep getting the following warning , and not sure why!!!
Use of implicit split to @_ is deprecated
for the followig pice of code:
my (%schds, %conts, %in_conts_not_schds); open( MILF, "INFO\\scheduled" ) or die "Can't open scheduled.txt\n"; while( <MILF> ) { split(/\s+/,$_); $schds{lc($_[0])}=1; } close MILF;

Replies are listed 'Best First'.
Re: split giving warning
by saintmike (Vicar) on Mar 22, 2004 at 05:08 UTC
    Remember the three commandments of the Perl religion:
    use warnings; use strict;

    Did I say three commandments? Yes, the third one is

    use diagnostics;

    when you're stumped with a message perl throws at you. In your case, with use diagnostics enabled, you'll get

    Use of implicit split to @_ is deprecated at ./t line 7 (#1) (D deprecated) It makes a lot of work for the compiler when you clobber a subroutine's argument list, so it's better if you assign the results of a split() explicitly to an array (or list).

    So ... just assign the list returned by split() to an array, and the warning goes away.

    -- saintmike

      The text that use diagnostics shows is also available via perldoc perldiag. You can also run the splain utility and type in the error message for it to do the lookup for you.
Re: split giving warning
by pbeckingham (Parson) on Mar 22, 2004 at 05:04 UTC

    Because the split call returns a list, and as you ignore the returned list, it gets stuffed into @_, the default array.

    This feature is deprecated, meaning that it will disappear someday, and the interpreter is informing you so that the code can be modified to run on future versions of Perl.

      In other words, do something like this:

      my @array = split /\s+/; $schds{ lc($array[0]) } = 1;

      --
      Allolex

Re: split giving warning
by davido (Cardinal) on Mar 22, 2004 at 07:21 UTC
    You've already got the answers as to what the problem is. Here's another solution that doesn't require a temporary named array:

    $schds{ lc( ( split( /\s+/, $_ ) )[0] ) } = 1 while <MILF>;

    With this snippet, split within the parenthesis provides a list that you can index into with a subscript (in this case, [0]. Then the first element is passed into lc, whos return value is used as the hash key for the hash named %schds.


    Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://338549]
Approved by arden
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-25 16:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found