Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Converting output from numeric values to text

by OnTheEdge (Novice)
on Mar 28, 2002 at 21:23 UTC ( #155107=perlquestion: print w/ replies, xml ) Need Help??
OnTheEdge has asked for the wisdom of the Perl Monks concerning the following question:

I need to ask a question of you'all here...I have looked on this site and seen similar type problems, but nothing has helped me resolve this problem.

I am very new to perl--this is my first script. This script is a part of a larger one. I am having problems converting the default output of $Month from 2, to Mar or what ever the month is. Below is my code. Could someone please tell me what I am doing wrong. I am not getting anything in the output file.

my $file; my $path ='\/a\/notify\/'; my $rslt = 'result.txt'; my @files; $ext = "\.log"; ($Second, $Minute, $Hour, $DayOfMonth, $Month, $Year, $WeekDay, $DayOfYear, $IsDST) = localtime(time); open(OUT, ">>$rslt"); if ($Month =0) { my $New_mo = 'Jan'; } if ($Month =1) { my $New_mo = 'Feb'; } if ($Month =2) { my $New_mo = 'Mar'; } if ($Month =3) { my $New_mo = 'Apr'; } if ($Month =4) { my $New_mo = 'May'; } if ($Month =5) { my $New_mo = 'Jun'; } if ($Month =6) { my $New_mo = 'Jul'; } if ($Month =7) { my $New_mo = 'Aug'; } if ($Month =8) { my $New_mo = 'Sep'; } if ($Month =9) { my $New_mo = 'Oct'; } if ($Month =10) { my $New_mo = 'Nov'; } if ($Month =11) { my $New_mo = 'Dec'; } print OUT $New_mo; close (OUT);

If you need further clarification, please let me know! I am eternally grateful for any help you may be able to give me! Thank you in advance!

~OnTheEdge

Comment on Converting output from numeric values to text
Download Code
Re: Converting output from numeric values to text
by wardk (Deacon) on Mar 28, 2002 at 21:30 UTC
    try
    if ( $Month eq 'Jan' ) { $Month = 0; }

    PS: this is my "most typical goof", so I caught it easily :)

    UPDATE: doh! if ( $Month == 0 ) { $Month = 'Jan'; } ....nevermind

Re: Converting output from numeric values to text
by cacharbe (Curate) on Mar 28, 2002 at 21:31 UTC
    *shrug*

    You should be using ==, but how about something a little more direct:

    my @mnths = qw(jan feb mar apr may jun jul aug sep oct nov dec); my ($month) = (localtime(time))[4]; my $New_mo = $mnths[$month]; print $New_mo;
    C-.

    update:I didn't even see the scope issue for the other two ideas and that was actually the reason nothing was getting into the output file. Even if you lost the "my" in your structures, however, you'd still get the wrong data in the file.

      Of course, you could also use one of the various Date::x modules as well.
      use Date::Calc qw(Month_to_Text); $string = Month_to_Text(((localtime(time))[4]+1)); print $string;
      C-.
Re: Converting output from numeric values to text
by RMGir (Prior) on Mar 28, 2002 at 21:34 UTC
    Your immediate problem is that "my" creates a _new_, _lexical_ $New_mo in each of your if blocks. $New_mo reverts to its old value once you exit the block.

    You'd have to write this, instead:

    my $New_mo; # ==, not = if ($Month == 0) { # no my! $New_mo = 'Jan'; } if ($Month == 1) { $New_mo = 'Feb'; } # ...
    An even better approach would be to create an array of month names, and use the $Month value as an index into that array, as the perldoc -f localtime entry would show you.
    my @monthNames=qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); my $New_mo = $monthNames[$month];
    Make sense?
    --
    Mike

    (Edit: Use == instead of =)

    (Edit again: I was SURE perldoc -f localtime used that as an example...)

Re: Converting output from numeric values to text
by Kanji (Parson) on Mar 28, 2002 at 21:35 UTC

    You're tripping over a scoping issue thanks to declaring your variable inside your if blocks.

    Declaring my $New_mo = ""; before your ifs and removing my from all the assignments in the ifs will fix your problem.

    However, there's a much simpler solution to be had using arrays and indicies...

    my @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); my $new_mo = $months[$month];

    Update: D'oh ... can't believe I missed the whole =/== thing! Don't I feel silly...

        --k.


Re: Converting output from numeric values to text
by chr0 (Sexton) on Mar 28, 2002 at 21:35 UTC
    if ($Month =0)
    should be
    if ($Month ==0)
    But I'd do it with an array instead, something like:
    my @Month_name = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); my $New_mo = $Month_name[$Month]
    ---
    perl -le'$$=substr(%{*::}->{_},2,1);print+(map{$$++for(1..$_);$$}(2,5,10)),$|'
Re: Converting output from numeric values to text
by rbc (Curate) on Mar 28, 2002 at 21:35 UTC
    try == instead of =

    ... and ...

    use strict;
    and -w

    ... and ...

    you just need to my $New_mo once.

    if ($Month == 7) { $New_mo = 'Aug'; }
Re: Converting output from numeric values to text
by Zaxo (Archbishop) on Mar 28, 2002 at 21:43 UTC

    For the immediate problem,

    sub int2mon { (Jan=>Feb=>Mar=>Apr=>May=>Jun=>Jul=>Aug=>Sep=>Oct=>Nov=>Dec=>)[shi +ft] }
    will do the job, but you should be working with one of the Date modules. That link will list several.

    The code above just indexes an anonymous array of strings with the first argument given the sub.

    After Compline,
    Zaxo

Re: Converting output from numeric values to text
by PrimeLord (Pilgrim) on Mar 28, 2002 at 22:09 UTC
    In the spirit of TIMTOWTDI you could also just build a hash with your info.

    my %months = qw( 0 => Jan 1 => Feb 2 => Mar );


    Then is $Month was 0

    print "$months{$month}\n"; Output = Jan
      Couldn't .... resist .... temptation:
      my %month = map { $_ => 1 } qw(jan feb mar apr may jun jul aug sep oct + nov dec);
      Or a hash slice:
      my %month; my @month = qw(jan feb mar apr may jun jul aug sep oct nov dec); @month{@month} = (1) x @month;
      UDPATE:
      sorry about that - please allow me to fix it:
      my @month = qw(jan feb mar apr may jun jul aug sep oct nov dec); my %month = map { $_ => $month[$_] } (0..$#month); ... $m_name = $month{$m_number};

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
        I don't think your code produces what the original poster OnTheEdge wanted. Yours would result in a hash with the month names as keys and 1 as values for each.

        He wants to convert month numbers into month names.

        $m_name = qw(jan feb mar apr may jun jul aug sep oct nov dec)[$m_numbe +r];
        /prakash
Re: Converting output from numeric values to text
by OnTheEdge (Novice) on Mar 29, 2002 at 15:21 UTC
    Thanks to everyone for their help...I still have to learn about using modules and need to learn more about using arrays, so all the comments here were useful. I found that when I made the simple changes, it ran just as I wanted it to.

    Another quick question not related to coding...Does anyone know of a good CBT or WBT for PERL? Thanks again.

    ~OnTheEdge

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (14)
As of 2014-07-22 21:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (128 votes), past polls