Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default case and functions

    Yo.....

    I read in Learning Perl that there are no such constructs like a case statement. Is there
    something similar or did I misread this? Also what about functions and
    function calls, do these exits or does the subroutines replace these?

    thanks

    Derek B. Smith
    OhioHealth IT
    UNIX / TSM / EDM Teams
    614-566-4145

    DBSMITH@OhioHealth.com Guest

  2. Similar Questions and Discussions

    1. #23026 [Com]: Make Zend case-sensitive (classes, functions, remove case-insensitive)
      ID: 23026 Comment by: nvivo at mandic dot com dot br Reported By: mfischer@php.net Status: Open Bug Type: ...
    2. Case conversions
      On Friday, August 15, 2003, at 04:20 PM, Scott Taylor wrote: Maybe something like: s/^(\w+), ?(\w+)$/ucfirst(lc $2) . ' ' . ucfirst(lc $1)/e...
    3. using case with classes
      Ok, this did not work like I expected (because I know too little about Ruby): def test( obj ) case obj.class when Array then puts "Array" when...
    4. #10743 [Com]: class functions & PHP core functions inconsistently clash ;)
      ID: 10743 Comment by: destes at ix dot netcom dot com Reported By: jjones at net-conex dot com Status: Open...
    5. Case Checks
      When I couldn't sleep this morning, this came across my head: class Check def initialize(sym, args) @args=args instance_eval "def ===(obj);...
  3. #2

    Default RE: case and functions

    There is no 'case' statement in Perl but it is easy to mimic such a construct.

    Tony Esposito
    Oracle Developer, Enterprise Business Intelligence
    XO Communications
    Plano, TX* 75074
    Work Phone: 972-516-5344
    Work Cell: 972-670-6144
    Email: [email]anthony.esposito@xo.com[/email]*


    -----Original Message-----
    From: [email]DBSMITH@OhioHealth.com[/email] [mailto:DBSMITH@OhioHealth.com]
    Sent: Tuesday, January 06, 2004 4:08 PM
    To: [email]beginners@perl.org[/email]
    Subject: case and functions

    Yo.....

    I read in Learning Perl that there are no such constructs like a case statement. Is there
    something similar or did I misread this? Also what about functions and
    function calls, do these exits or does the subroutines replace these?

    thanks

    Derek B. Smith
    OhioHealth IT
    UNIX / TSM / EDM Teams
    614-566-4145
    Anthony Esposito Guest

  4. #3

    Default RE: case and functions

    > Yo.....

    What's up dog? :)
    >
    > I read in Learning Perl that there are no such constructs
    > like a case statement. Is there
    Yes, there is.
    Do you mean:
    if(this) { do this }
    elsif(that) { do that }
    else { do the other }
    > something similar or did I misread this? Also what about
    > functions and
    > function calls, do these exits or does the subroutines replace these?
    Functions and subroutines are *basically* the same thing.
    Perldoc perlsub I believe addresses this.

    HTH
    DMuey
    >
    > thanks
    Dan Muey Guest

  5. #4

    Default Re: case and functions

    From: [email]DBSMITH@OhioHealth.com[/email]
    > I read in Learning Perl that there are no such constructs like a case
    > statement. Is there something similar or did I misread this?
    Ask the FAQ, run

    perldoc -q "case statement"
    > Also
    > what about functions and function calls, do these exits or does the
    > subroutines replace these?
    Subroutines are both what some languages call procedures and
    functions.

    Jenda
    ===== [email]Jenda@Krynicky.cz[/email] === [url]http://Jenda.Krynicky.cz[/url] =====
    When it comes to wine, women and song, wizards are allowed
    to get drunk and croon as much as they like.
    -- Terry Pratchett in Sourcery

    Jenda Krynicky Guest

  6. #5

    Default Re: case and functions



    [email]Jenda@Krynicky.cz[/email] wrote:
    >From: [email]DBSMITH@OhioHealth.com[/email]
    >
    >
    >>I read in Learning Perl that there are no such constructs like a case
    >>statement. Is there something similar or did I misread this?
    >>
    >>
    >
    >Ask the FAQ, run
    >
    > perldoc -q "case statement"
    >
    >
    >
    >>Also
    >>what about functions and function calls, do these exits or does the
    >>subroutines replace these?
    >>
    >>
    >
    >Subroutines are both what some languages call procedures and
    >functions.
    >
    >Jenda
    >===== [email]Jenda@Krynicky.cz[/email] === [url]http://Jenda.Krynicky.cz[/url] =====
    >When it comes to wine, women and song, wizards are allowed
    >to get drunk and croon as much as they like.
    > -- Terry Pratchett in Sourcery
    >
    >
    >
    Have you checked into Switch.pm ? It provides a switch/case construct
    similar to a C switch.

    HTH,

    Chuck

    Chuck Fox Guest

  7. #6

    Default Re: case and functions

    People of the Perl,

    thanks for the info....I am familiar with perls subroutines so I will use
    these and this name instead of functions. Also, my intention was to avoid
    the if , then ,elif, then constructs, mentioned by Dan, for certain
    situations which is why I asked about cases so I will read the perldoc.

    thanks



    Derek B. Smith
    OhioHealth IT
    UNIX / TSM / EDM Teams
    614-566-4145





    "Jenda Krynicky" <Jenda@Krynicky.cz>
    01/06/2004 05:18 PM


    To: [email]beginners@perl.org[/email]
    cc:
    Subject: Re: case and functions


    From: [email]DBSMITH@OhioHealth.com[/email]
    > I read in Learning Perl that there are no such constructs like a case
    > statement. Is there something similar or did I misread this?
    Ask the FAQ, run

    perldoc -q "case statement"
    > Also
    > what about functions and function calls, do these exits or does the
    > subroutines replace these?
    Subroutines are both what some languages call procedures and
    functions.

    Jenda
    ===== [email]Jenda@Krynicky.cz[/email] === [url]http://Jenda.Krynicky.cz[/url] =====
    When it comes to wine, women and song, wizards are allowed
    to get drunk and croon as much as they like.
    -- Terry Pratchett in Sourcery


    --
    To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    <http://learn.perl.org/> <http://learn.perl.org/first-response>





    DBSMITH@OhioHealth.com Guest

  8. #7

    Default Re: case and functions

    >>>>> "DBSMITH" == DBSMITH <DBSMITH@OhioHealth.com> writes:

    DBSMITH> Also, my intention was to avoid the if , then ,elif, then
    DBSMITH> constructs, mentioned by Dan, for certain situations

    What is the source of your fear? Sounds like a phobia to me.

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
    Randal L. Schwartz Guest

  9. #8

    Default Re: case and functions

    [email]DBSMITH@OhioHealth.com[/email] wrote:
    > People of the Perl,
    >
    > thanks for the info....I am familiar with perls subroutines so I will use
    > these and this name instead of functions. Also, my intention was to avoid
    > the if , then ,elif, then constructs, mentioned by Dan, for certain
    > situations which is why I asked about cases so I will read the perldoc.
    >
    > thanks
    >
    > Derek B. Smith
    Don't give up quite so quickly. At least in Perl 5.8, there is abuilt-in
    Switch module, as Chuck Fox has also pointed out.

    Joseph

    R. Joseph Newton Guest

  10. #9

    Default Re: case and functions





    You can do something like this:

    my $flip = "something";

    SWITCH: {
    ( $flip =~ /^0$/ || $flip =~ /^1$/ || $flip =~ /^2$/ ) && do {
    $local_pref = "FL"; last SWITCH; };

    ( $flip =~ /^3$/ || $flip =~ /^4$/ || $flip =~ /^5$/ ) && do {
    $local_pref = "FU"; last SWITCH; };

    ( $flip =~ /^6$/ || $flip =~ /^7$/ ) && do {
    $local_pref = "FA"; last SWITCH; };

    ( $flip =~ /^8$/ || $flip =~ /^9$/ ) && do {
    $local_pref = "FS"; last SWITCH; };

    } # end switch


    __________________

    William Ampeh (x3939)
    Federal Reserve Board

    William Ampeh Guest

  11. #10

    Default Array Manipulations

    Hello Everyone,

    I am a Perl newbie trying to learn as much Perl as I can. I am trying to
    combine specific array elements into one single array element so that I can
    write
    to an Excel cell, where all the data will fit.

    For instance I have,

    array[0] = "F1: blue";
    array[1] = "F2: green";
    array[2] = "F3: red";
    array[3] = "F1: purple";
    array[4] = "F2: brown";
    array[4] = "F1: red";
    array[5] = "F2: pink";
    array[6] = "F3: blue";
    array[7] = "F4: white";

    With the above information, I want to put all the F's in order before it
    starts over at 1 again. For instance, I want to store array[0], array[1],
    and array[2] into a specific array element and then store array[3], array[4]
    into another specific array element, then I start over again with array[5].
    Each time I encounter an F1, I will store all the particular elements into
    one single array element. Is this possible?

    Thanks for your time.

    binhqnguyen@bellsouth.net Guest

  12. #11

    Default Re: case and functions


    On Jan 6, 2004, at 2:08 PM, [email]DBSMITH@OhioHealth.com[/email] wrote:
    > Yo.....
    what Up.
    > I read in Learning Perl that there are no such constructs
    > like a case statement. Is there something similar or did I misread
    > this?
    As folks have already pointed to both
    the classic perlfaq, and implied that
    one should consult

    perldoc Switch

    which if you are not on perl5.8 can be
    downloaded from the CPAN from
    <http://search.cpan.org/~dconway/Switch-2.09/Switch.pm>

    > Also what about functions and function calls,
    > do these exits or does the subroutines replace these?
    [..]

    This of course brings us to the more interesting
    side of the swtich/case statement problem - namely
    what are you trying to do with them, and have you
    thought about the classic 'dispatching table' type
    of solution - say something like

    my %process = (
    start => \&do_start,
    stop => \&do_stop,
    test => \&do_test
    );

    if ( defined( $process{$ARGV[0]}))
    {
    $process{$ARGV[0]}(@ARGV);
    } else {
    print "issued invalid command: $ARGV[0]\n";
    }

    sub do_start { ... }
    sub do_stop { ... }
    sub do_test { ... }

    you will notice that this is similar to the classic
    init script most folks have seen as say

    #!/bin/sh

    case "$1" in
    "start") do_start ;;
    "stop") do_stop ;;
    "test") do_test ;;
    *) echo "i do not know how to to $1" ;;
    esac

    So it's sorta a case of what exactly were you trying
    to do with the case like statement???


    ciao
    drieux

    ---

    Drieux Guest

  13. #12

    Default Re: Array Manipulations

    [email]binhqnguyen@bellsouth.net[/email] wrote:
    >
    > Hello Everyone,
    Hello,
    > I am a Perl newbie trying to learn as much Perl as I can. I am trying to
    > combine specific array elements into one single array element so that I can
    > write
    > to an Excel cell, where all the data will fit.
    >
    > For instance I have,
    >
    > array[0] = "F1: blue";
    > array[1] = "F2: green";
    > array[2] = "F3: red";
    > array[3] = "F1: purple";
    > array[4] = "F2: brown";
    > array[4] = "F1: red";
    > array[5] = "F2: pink";
    > array[6] = "F3: blue";
    > array[7] = "F4: white";
    >
    > With the above information, I want to put all the F's in order before it
    > starts over at 1 again. For instance, I want to store array[0], array[1],
    > and array[2] into a specific array element and then store array[3], array[4]
    > into another specific array element, then I start over again with array[5].
    > Each time I encounter an F1, I will store all the particular elements into
    > one single array element. Is this possible?
    Is this what you want?

    $ perl -le'
    use Data::Dumper;
    @array = ( "F1: blue",
    "F2: green",
    "F3: red",
    "F1: purple",
    "F2: brown",
    "F1: red",
    "F2: pink",
    "F3: blue",
    "F4: white" );

    my ( $index, @new ) = -1;

    push @{ $new[ /^F1:/ ? ++$index : $index ] }, $_ for @array;

    print Dumper \@new;
    '
    $VAR1 = [
    [
    'F1: blue',
    'F2: green',
    'F3: red'
    ],
    [
    'F1: purple',
    'F2: brown'
    ],
    [
    'F1: red',
    'F2: pink',
    'F3: blue',
    'F4: white'
    ]
    ];



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139