Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

array help

by Anonymous Monk
on Dec 13, 2002 at 23:50 UTC ( #219782=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to multiply each numerical element in a large array by a number input by the user. I'm pretty new at this and could use some help. Does anyone have any advice?

Replies are listed 'Best First'.
Re: array help
by graff (Chancellor) on Dec 14, 2002 at 02:44 UTC
    ehdonhon is right -- you're always better off here if you include something you've tried, especially if you tell us what you got from it, so we can see how it differed from what you say you want (this sometimes helps us get a better idea of what you really want).

    In fact, I'm only guessing now that you might want something like this:

    @another_array = map { $_ * $number_from_stdin } @large_array;
    or maybe you'd be more comfortable with something less compact:
    foreach my $val ( @large_array ) { push @another_array, $val * $number_from_stdin; }
    which does the same thing, but may be less mystifying to a larger audience of programmers who may be unfamiliar with Perl.

    So, have you been reading any relevant books or documentation? You might find that helpful as well...

Re: array help
by ehdonhon (Curate) on Dec 14, 2002 at 02:25 UTC

    Hello Anonymous Monk,
    I think you'll find that generally you will get better answers if you show that you've already done some work on your own. Perhaps you should post what you have already tried?

    Otherwise, somebody might think that you are just trying to get someone else to figgure out your homework problem for you.

Re: array help
by dpuu (Chaplain) on Dec 14, 2002 at 00:10 UTC
    You question is a little unclear: Do you have one scaling factor, or one per element of the array. I think you are wanting 1 scale facter for the entire array. So you might try:
    my @source = (1..10); my $scale = <>; # get user input my @result = map { $_ * $scale } @source; print "original: @source\n"; print "scaled by $scale: @result\n";
    --Dave
Re: array help
by batkins (Chaplain) on Dec 14, 2002 at 04:39 UTC
    if you want to scale each element:

    @result = map { print "Enter scaling factor:"; $_ * int(<STDIN>) } @ar +ray;

    if you want to scale the whole thing by one factor:

    @result = map { $_ * $factor } @array;
Re: array help
by Anonymous Monk on Dec 14, 2002 at 01:41 UTC
    Here is a nice little one-liner I hack up just for kicks.
    perl -e '@nums=(1..100); $user=<>; print "original\n"; foreach ((0+@nu +ms)) { print "@nums";} print "\n\n"; print "scaled: "; foreach my $nu +m (@nums) { print $user*$num . " ";}print "\n";'
    Happy Hacking GNU/HDE

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2022-07-03 11:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?