Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Global array afftected by map inside of a subroutine

by Lady_Aleena (Deacon)
on Dec 14, 2011 at 21:07 UTC ( #943615=note: print w/ replies, xml ) Need Help??


in reply to Re: Global array afftected by map inside of a subroutine
in thread Global array afftected by map inside of a subroutine

ikegami...List::MoreUtils's apply did exactly what I needed for the array. Thank you! Now all I need to figure out is why my grep is breaking.

I isolated the code that I am working on, but grep is grepping everything. I've never had this problem with grep before.

my @filesize_names = qw(bit nibble byte kilobyte megabyte gigabyte ter +abyte petabyte exabyte zettabyte yottabyte); my $word = 'fubar'; $word =~ s/s$//; my @short_sizes = apply {$_ =~ s/^(\w)\w{1,}$/$1b/} @filesize_names; print Dumper(\@filesize_names); print Dumper(\@short_sizes); if (grep(lc $word,@short_sizes)) { # even 'fubar' greps print "$word grepped!"; $word = $filesize_names[firstidx { $_ eq lc $word } @short_sizes]; } print $word;
Have a cookie and a very nice day!
Lady Aleena


Comment on Re^2: Global array afftected by map inside of a subroutine
Select or Download Code
Re^3: Global array afftected by map inside of a subroutine
by BrowserUk (Pope) on Dec 14, 2011 at 21:36 UTC
    grep is grepping everything. I've never had this problem with grep before.

    You are using:

    grep( lc $word, @short_sizes )

    grep passes through any value where the expression (first argument) is true.

    Your grep expression is lc $word. If $word has any value except undef, '' or 0, then lc $word will also have a value, and therefore be true, and everything in @short_sizes will pass through.

    Since if( grep( ...,... ) ) { places grep in a scalar context, the result will be the number of items passed through, which will be the same as the number of elements in @short_sizes. So, unless it is empty, the if condition will be true.

    Effectively, if (grep(lc $word,@short_sizes)) {

    is the same as:if( defined( $word ) && @short_sizes > 0 ) {

    which almost certainly isn't what you intend.


    So, what are you trying to achieve with that construct?

    If you are trying to check if any of the values in @short_sizes matches the lower cased value of $word, the you would need:

    if( grep( $_ eq lc $word, @short_sizes ) ) {

    But that's just my guess as to your intent.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      BrowserUk...you guessed correctly. Thank you!

      Have a cookie and a very nice day!
      Lady Aleena
Re^3: Global array afftected by map inside of a subroutine
by kejohm (Hermit) on Dec 14, 2011 at 21:59 UTC

    Based on your code sample, might I suggest a hash lookup table, eg:

    # Untested my @filesize_names = qw(kilobyte megabyte gigabyte terabyte petabyte e +xabyte zettabyte yottabyte); my %short_sizes = map { m/^(\w)/; $1 . 'b' => $_ } @filesize_names; my $word = 'kb'; if ( exists $short_sizes{lc $word} ) { say $short_sizes{lc $word}; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://943615]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2014-12-26 03:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (164 votes), past polls