Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Hash Question

by perlnoobster (Sexton)
on Jan 09, 2013 at 10:59 UTC ( #1012428=perlquestion: print w/ replies, xml ) Need Help??
perlnoobster has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I'm fairly new to perl and was wondering if someone could help me, I have the following hash
foreach (<$bh>) { my @text=split(/\t/); $bHash{$text[0]} = $text[11]; }
I want to change it so that the $text[11] could also be $text[12],$text[14],$text[15],$text[19] is this possible? if so how? Thank you!

Replies are listed 'Best First'.
Re: Hash Question
by jethro (Monsignor) on Jan 09, 2013 at 11:17 UTC

    That's what variables were invented for

    my $inp; print "What column do you want ? "; do { $inp= <>; chomp($inp); } until ($inp=~/^\d+$/ and $inp>=11 and $inp<=19); foreach (<$bh>) { my @text=split(/\t/); $bHash{$text[0]} = $text[$inp]; }

    In this case the user is asked to type in a column number between 11 and 19

Re: Hash Question
by Anonymous Monk on Jan 09, 2013 at 11:03 UTC

    I want to change it so that the $text11 could also be $text12,$text14,$text15,$text19 is this possible? if so how?

    Yes, whatever you mean by that, it is possible, but its not clear what you mean

      Hello Anonymous Monk, Sorry, I've made a mistake! This is the coding that needs adjusting:    if ($bHash{$text[3]} eq $text[11]) { I need it to be adjusted so that it does the following function:  if ($bHash{$text[3]} eq $text[11]|$text12|$text14|$text15|$text19 ) can this be fixed? if so how? Thank you!
        You probably mean
        for my $value (@text[11, 12, 14, 15, 19]) { if ($bHash{ $text[3] } eq $value) { $bHash{ $text[0] } = $value; last; } }
        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        What does that mean?

        You're making up syntax and you're hoping I know what you mean by it -- I can venture a guess, but the point is, you need to ask a better question

        Here's my guess:

        >perl -wMstrict -le "use List::MoreUtils qw(any); ;; my $s = 'B a B b B c B d'; ;; my @text = split /\s+/, $s; ;; if (any { $text[0] eq $text[ $_ ] } 2, 4, 6, 7) { print 'something was eq'; } " something was eq

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1012428]
Approved by marto
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2016-08-28 08:05 GMT
Find Nodes?
    Voting Booth?
    The best thing I ever won in a lottery was:

    Results (392 votes). Check out past polls.