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

Hello,
Can I combine this

my $maid = $fields[0]; my $maid_number =~ /(^\d*)/;
into this ?
my $maid = $fields[0]=~ /(^\d*)/;

Replies are listed 'Best First'.
Re: variable declaration shortcut question
by ikegami (Patriarch) on Feb 03, 2009 at 15:29 UTC

    The following will extract the leading digits from $fields[0] and place them in $maid:

    my ($maid) = $fields[0] =~ /^(\d*)/;

    In scalar context, the match op would return a value indicating whether the match was successful or not. That's useless to you since the pattern will always match. In list context, the match operator returns a list of a all the captures.

    The parens around $maid are necessary to select the list assignment operator which places the match operator in list context.

      That's it! Thanks I!
Re: variable declaration shortcut question
by hbm (Hermit) on Feb 03, 2009 at 16:25 UTC

    lomSpace, Beyond your question, you seem to be doing some odd stuff.

    1. When you say my @maid_id = split(/_/, $komp_dir);, do you really mean to split $komp_dir? That variable is unchanging within both loops, and so should certainly not be reprocessed with every pass. Ditto for the other variables in the nested loop.
    2. The next line has another problem: my $maid = $fields[0]; Don't you mean my $maid = $maid_id[0];?
    3. Your braces in ${maid} are unnecessary and inconsistent with, say, $count.

      Ah... no use strict... no likely to work.

      Thanks for pointing that out. I am modifying that code.
Re: variable declaration shortcut question
by Anonymous Monk on Feb 03, 2009 at 15:04 UTC
    my $maid_number =~ /(^\d*)/; is nonsense, so combine what to accomplish what(sample input, expected output).

      I am splitting the name of a file to capture the digits at the
      beginning of its name.

      "11232L_rex_hav_1.abi"
      I need to parse the numbers at the beginning of the filename to pass into my hash.
      my %maids_hash{}; opendir (my $dhs, $seqdir); opendir (my $dhk, $komp_dir); while(defined($seqfile = readdir($dhs))) { foreach my $maids_dir(@komp){ my @maid_id = split(/_/, $komp_dir); my $maid = $fields[0]; #$maid =~ /(^\d*)/; my $count = ${maid} ; if ($count == null){ ${maid}=1; }else{ ${maid}=$count+1; } $maids_hash{$maids_dir} = $maid; } foreach my $maid ( keys %maids_hash ){ if ($maids_hash{$maid}==4){ #call routine to do your copy logic } }
      I vaguely remember doing something in the past like:
      my $maid = $fields[0]=~ /(^\d*)/;
      Has my memory failed me?
Re: variable declaration shortcut question
by dave_the_m (Monsignor) on Feb 03, 2009 at 15:28 UTC
    I think you want
    (my $maid = $fields[0]) =~ /(^\d*)/;

    Dave.

Re: variable declaration shortcut question
by Bloodnok (Vicar) on Feb 03, 2009 at 15:52 UTC
    Not quite, what you can do is ... (my $maid = $fields[0]) =~ /(^\d*)/; my ($maid) = $fields[0]) =~ /(^\d*)/; - as per Recipe 6.1 in the Cookbook.

    Update:

    Thanx to ikegami twice over - wholly misplaced paren

    A user level that continues to overstate my experience :-))
      $ perl -e'my ($maid = $fields[0]) =~ /(^\d*)/;' Can't declare scalar assignment in "my" at -e line 1, near ") =~" Execution of -e aborted due to compilation errors.
      Now both parens are misplaced. Code posted earlier by the OP shows that he wants the captured digits in $maid.
      I am reading it right now!
      Thanks!