Loading ...
Sorry, an error occurred while loading the content.
 

Single assignment to a mixture of data types

Expand Messages
  • hooyar66
    I know this must be easy but Perl s syntax has confounded me again... I would expect the code below to assign the value of $string as  C  b
    Message 1 of 7 , Aug 30, 2007
      I know this must be easy but Perl's syntax has confounded me again... I
      would expect the code
      below to assign the value of $string as 'C' b\
      ut instead all values are assigned to the @array\
      .

      How can I assign to both a string and array in one line - ideally
      without using references.

      Thanks
      NJH

      #!c:/perl/bin/perl.exe

      use warnings;
      use strict;
      use diagnostics;

      my (@array,$string) = (('A','B'),'C');
      print "My \@array is: @array\nMy \$string is: $string\n";

      Produces:
      My @array is: A B C
      My $string is:




      [Non-text portions of this message have been removed]
    • skarlso777
      Hi! In my knowledge it is so that you have to turn it around. So don t use ... Perl flushes everything at one times. This also goes for passing parameters.
      Message 2 of 7 , Aug 30, 2007
        Hi!

        In my knowledge it is so that you have to turn it around. So don't use
        :> my (@array,$string) = (('A','B'),'C');
        Because it will always be ABC, but use it so:
        > my ($string, @array) = ('C', ('A','B'));

        Perl flushes everything at one times. This also goes for passing
        parameters.

        Gergely.


        --- In perl-beginner@yahoogroups.com, "hooyar66" <pcbcad@...> wrote:
        >
        > I know this must be easy but Perl's syntax has confounded me again... I
        > would expect the code
        > below to assign the value of $string as 'C' b\
        > ut instead all values are assigned to the @array\
        > .
        >
        > How can I assign to both a string and array in one line - ideally
        > without using references.
        >
        > Thanks
        > NJH
        >
        > #!c:/perl/bin/perl.exe
        >
        > use warnings;
        > use strict;
        > use diagnostics;
        >
        > my (@array,$string) = (('A','B'),'C');
        > print "My \@array is: @array\nMy \$string is: $string\n";
        >
        > Produces:
        > My @array is: A B C
        > My $string is:
        >
        >
        >
        >
        > [Non-text portions of this message have been removed]
        >
      • hooyar66
        ... use ... again... I ... Thanks for the reply Gergely - the example I gave was very simple however I really need to use an assignment with multiple strings
        Message 3 of 7 , Aug 30, 2007
          --- In perl-beginner@yahoogroups.com,
          "skarlso777" <Gergely_Brautigam@...> wrote:
          >
          > Hi!
          >
          > In my knowledge it is so that you have to turn it around. So don't
          use
          > :> my (@array,$string) = (('A','B'),'C');
          > Because it will always be ABC, but use it so:
          > > my ($string, @array) = ('C', ('A','B'));
          >
          > Perl flushes everything at one times. This also goes for passing
          > parameters.
          >
          > Gergely.
          >
          >
          > --- In perl-beginner@yahoogroups.com, "hooyar66" <pcbcad@> wrote:
          > >
          > > I know this must be easy but Perl's syntax has confounded me
          again... I
          > > would expect the code
          > > below to assign the value of $string as 'C' b\
          > > ut instead all values are assigned to the @array\
          > > .
          > >
          > > How can I assign to both a string and array in one line - ideally
          > > without using references.
          > >
          > > Thanks
          > > NJH
          > >
          > > #!c:/perl/bin/perl.exe
          > >
          > > use warnings;
          > > use strict;
          > > use diagnostics;
          > >
          > > my (@array,$string) = (('A','B'),'C');
          > > print "My \@array is: @array\nMy \$string is: $string\n";
          > >
          > > Produces:
          > > My @array is: A B C
          > > My $string is:
          > >
          > >
          > >
          > >
          > > [Non-text portions of this message have been removed]
          > >
          >

          Thanks for the reply Gergely - the example I gave was very simple
          however I really need to use an assignment with multiple strings and
          arrays, so this would be a better example:

          my ($string, @array1, @array2) = ('A',('B','C'),('D','E'));
          print "My \$string is: $string\nMy \@array1 is: @array1\nMy \@array2
          is: @array2\n";

          which produces:
          My $string is: A
          My @array1 is: B C D E
          My @array2 is:

          Is there a way to assign multiple arrays in a single line? I actually
          need to assign my values from the return of a subroutine, something
          like:

          my ($string, @array1, @array2) = &assign_vals;
          print "My \$string is: $string\nMy \@array1 is: @array1\nMy \@array2
          is: @array2\n";

          sub assign_vals{
          return ('A',('B','C'),('D','E'));
          }

          Thanks for any help
          NJH
        • skarlso777
          ... Hi! The problem is that arrays are greedy. So they swallow up pretty much everything they come into contact with. So if you have to return two or more
          Message 4 of 7 , Aug 30, 2007
            --- In perl-beginner@yahoogroups.com, "hooyar66" <pcbcad@...> wrote:
            >
            > --- In perl-beginner@yahoogroups.com,
            > "skarlso777" <Gergely_Brautigam@> wrote:
            > >
            > > Hi!
            > >
            > > In my knowledge it is so that you have to turn it around. So don't
            > use
            > > :> my (@array,$string) = (('A','B'),'C');
            > > Because it will always be ABC, but use it so:
            > > > my ($string, @array) = ('C', ('A','B'));
            > >
            > > Perl flushes everything at one times. This also goes for passing
            > > parameters.
            > >
            > > Gergely.
            > >
            > >
            > > --- In perl-beginner@yahoogroups.com, "hooyar66" <pcbcad@> wrote:
            > > >
            > > > I know this must be easy but Perl's syntax has confounded me
            > again... I
            > > > would expect the code
            > > > below to assign the value of $string as 'C' b\
            > > > ut instead all values are assigned to the @array\
            > > > .
            > > >
            > > > How can I assign to both a string and array in one line - ideally
            > > > without using references.
            > > >
            > > > Thanks
            > > > NJH
            > > >
            > > > #!c:/perl/bin/perl.exe
            > > >
            > > > use warnings;
            > > > use strict;
            > > > use diagnostics;
            > > >
            > > > my (@array,$string) = (('A','B'),'C');
            > > > print "My \@array is: @array\nMy \$string is: $string\n";
            > > >
            > > > Produces:
            > > > My @array is: A B C
            > > > My $string is:
            > > >
            > > >
            > > >
            > > >
            > > > [Non-text portions of this message have been removed]
            > > >
            > >
            >
            > Thanks for the reply Gergely - the example I gave was very simple
            > however I really need to use an assignment with multiple strings and
            > arrays, so this would be a better example:
            >
            > my ($string, @array1, @array2) = ('A',('B','C'),('D','E'));
            > print "My \$string is: $string\nMy \@array1 is: @array1\nMy \@array2
            > is: @array2\n";
            >
            > which produces:
            > My $string is: A
            > My @array1 is: B C D E
            > My @array2 is:
            >
            > Is there a way to assign multiple arrays in a single line? I actually
            > need to assign my values from the return of a subroutine, something
            > like:
            >
            > my ($string, @array1, @array2) = &assign_vals;
            > print "My \$string is: $string\nMy \@array1 is: @array1\nMy \@array2
            > is: @array2\n";
            >
            > sub assign_vals{
            > return ('A',('B','C'),('D','E'));
            > }
            >
            > Thanks for any help
            > NJH
            >

            Hi!

            The problem is that arrays are greedy. So they swallow up pretty much
            everything they come into contact with.

            So if you have to return two or more arrays best it's using references
            like:


            sub function {
            my $scalar;
            my @array1;
            my @array2;
            ..
            .
            .

            return (\$scalar, \@array1, \@array2);



            }

            Then use this like:
            my ($first, $second, $third ) = function();


            Notice i did not use @second i used $second. Because this will contain
            the reference to the array you have to cast it into array to use it.

            Hope that helps,
            Gergely.
          • hooyar66
            ... don t ... passing ... ideally ... simple ... and ... @array2 ... actually ... something ... @array2 ... much ... references ... contain ... it. ... I
            Message 5 of 7 , Aug 30, 2007
              --- In perl-beginner@yahoogroups.com,
              "skarlso777" <Gergely_Brautigam@...> wrote:
              >
              > --- In perl-beginner@yahoogroups.com, "hooyar66" <pcbcad@> wrote:
              > >
              > > --- In perl-beginner@yahoogroups.com,
              > > "skarlso777" <Gergely_Brautigam@> wrote:
              > > >
              > > > Hi!
              > > >
              > > > In my knowledge it is so that you have to turn it around. So
              don't
              > > use
              > > > :> my (@array,$string) = (('A','B'),'C');
              > > > Because it will always be ABC, but use it so:
              > > > > my ($string, @array) = ('C', ('A','B'));
              > > >
              > > > Perl flushes everything at one times. This also goes for
              passing
              > > > parameters.
              > > >
              > > > Gergely.
              > > >
              > > >
              > > > --- In perl-beginner@yahoogroups.com, "hooyar66" <pcbcad@>
              wrote:
              > > > >
              > > > > I know this must be easy but Perl's syntax has confounded me
              > > again... I
              > > > > would expect the code
              > > > > below to assign the value of $string as 'C' b\
              > > > > ut instead all values are assigned to the @array\
              > > > > .
              > > > >
              > > > > How can I assign to both a string and array in one line -
              ideally
              > > > > without using references.
              > > > >
              > > > > Thanks
              > > > > NJH
              > > > >
              > > > > #!c:/perl/bin/perl.exe
              > > > >
              > > > > use warnings;
              > > > > use strict;
              > > > > use diagnostics;
              > > > >
              > > > > my (@array,$string) = (('A','B'),'C');
              > > > > print "My \@array is: @array\nMy \$string is: $string\n";
              > > > >
              > > > > Produces:
              > > > > My @array is: A B C
              > > > > My $string is:
              > > > >
              > > > >
              > > > >
              > > > >
              > > > > [Non-text portions of this message have been removed]
              > > > >
              > > >
              > >
              > > Thanks for the reply Gergely - the example I gave was very
              simple
              > > however I really need to use an assignment with multiple strings
              and
              > > arrays, so this would be a better example:
              > >
              > > my ($string, @array1, @array2) = ('A',('B','C'),('D','E'));
              > > print "My \$string is: $string\nMy \@array1 is: @array1\nMy
              \@array2
              > > is: @array2\n";
              > >
              > > which produces:
              > > My $string is: A
              > > My @array1 is: B C D E
              > > My @array2 is:
              > >
              > > Is there a way to assign multiple arrays in a single line? I
              actually
              > > need to assign my values from the return of a subroutine,
              something
              > > like:
              > >
              > > my ($string, @array1, @array2) = &assign_vals;
              > > print "My \$string is: $string\nMy \@array1 is: @array1\nMy
              \@array2
              > > is: @array2\n";
              > >
              > > sub assign_vals{
              > > return ('A',('B','C'),('D','E'));
              > > }
              > >
              > > Thanks for any help
              > > NJH
              > >
              >
              > Hi!
              >
              > The problem is that arrays are greedy. So they swallow up pretty
              much
              > everything they come into contact with.
              >
              > So if you have to return two or more arrays best it's using
              references
              > like:
              >
              >
              > sub function {
              > my $scalar;
              > my @array1;
              > my @array2;
              > ..
              > .
              > .
              >
              > return (\$scalar, \@array1, \@array2);
              >
              >
              >
              > }
              >
              > Then use this like:
              > my ($first, $second, $third ) = function();
              >
              >
              > Notice i did not use @second i used $second. Because this will
              contain
              > the reference to the array you have to cast it into array to use
              it.
              >
              > Hope that helps,
              > Gergely.
              >

              I already got things to work by using references - I was just hoping
              that I could avoid them to keep things nice and simple... thanks!
            • skarlso777
              ... No problem. Sadly there is no other way. At least no other i know off... :)) Gergely.
              Message 6 of 7 , Aug 30, 2007
                --- In perl-beginner@yahoogroups.com, "hooyar66" <pcbcad@...> wrote:
                >
                > --- In perl-beginner@yahoogroups.com,
                > "skarlso777" <Gergely_Brautigam@> wrote:
                > >
                > > --- In perl-beginner@yahoogroups.com, "hooyar66" <pcbcad@> wrote:
                > > >
                > > > --- In perl-beginner@yahoogroups.com,
                > > > "skarlso777" <Gergely_Brautigam@> wrote:
                > > > >
                > > > > Hi!
                > > > >
                > > > > In my knowledge it is so that you have to turn it around. So
                > don't
                > > > use
                > > > > :> my (@array,$string) = (('A','B'),'C');
                > > > > Because it will always be ABC, but use it so:
                > > > > > my ($string, @array) = ('C', ('A','B'));
                > > > >
                > > > > Perl flushes everything at one times. This also goes for
                > passing
                > > > > parameters.
                > > > >
                > > > > Gergely.
                > > > >
                > > > >
                > > > > --- In perl-beginner@yahoogroups.com, "hooyar66" <pcbcad@>
                > wrote:
                > > > > >
                > > > > > I know this must be easy but Perl's syntax has confounded me
                > > > again... I
                > > > > > would expect the code
                > > > > > below to assign the value of $string as 'C' b\
                > > > > > ut instead all values are assigned to the @array\
                > > > > > .
                > > > > >
                > > > > > How can I assign to both a string and array in one line -
                > ideally
                > > > > > without using references.
                > > > > >
                > > > > > Thanks
                > > > > > NJH
                > > > > >
                > > > > > #!c:/perl/bin/perl.exe
                > > > > >
                > > > > > use warnings;
                > > > > > use strict;
                > > > > > use diagnostics;
                > > > > >
                > > > > > my (@array,$string) = (('A','B'),'C');
                > > > > > print "My \@array is: @array\nMy \$string is: $string\n";
                > > > > >
                > > > > > Produces:
                > > > > > My @array is: A B C
                > > > > > My $string is:
                > > > > >
                > > > > >
                > > > > >
                > > > > >
                > > > > > [Non-text portions of this message have been removed]
                > > > > >
                > > > >
                > > >
                > > > Thanks for the reply Gergely - the example I gave was very
                > simple
                > > > however I really need to use an assignment with multiple strings
                > and
                > > > arrays, so this would be a better example:
                > > >
                > > > my ($string, @array1, @array2) = ('A',('B','C'),('D','E'));
                > > > print "My \$string is: $string\nMy \@array1 is: @array1\nMy
                > \@array2
                > > > is: @array2\n";
                > > >
                > > > which produces:
                > > > My $string is: A
                > > > My @array1 is: B C D E
                > > > My @array2 is:
                > > >
                > > > Is there a way to assign multiple arrays in a single line? I
                > actually
                > > > need to assign my values from the return of a subroutine,
                > something
                > > > like:
                > > >
                > > > my ($string, @array1, @array2) = &assign_vals;
                > > > print "My \$string is: $string\nMy \@array1 is: @array1\nMy
                > \@array2
                > > > is: @array2\n";
                > > >
                > > > sub assign_vals{
                > > > return ('A',('B','C'),('D','E'));
                > > > }
                > > >
                > > > Thanks for any help
                > > > NJH
                > > >
                > >
                > > Hi!
                > >
                > > The problem is that arrays are greedy. So they swallow up pretty
                > much
                > > everything they come into contact with.
                > >
                > > So if you have to return two or more arrays best it's using
                > references
                > > like:
                > >
                > >
                > > sub function {
                > > my $scalar;
                > > my @array1;
                > > my @array2;
                > > ..
                > > .
                > > .
                > >
                > > return (\$scalar, \@array1, \@array2);
                > >
                > >
                > >
                > > }
                > >
                > > Then use this like:
                > > my ($first, $second, $third ) = function();
                > >
                > >
                > > Notice i did not use @second i used $second. Because this will
                > contain
                > > the reference to the array you have to cast it into array to use
                > it.
                > >
                > > Hope that helps,
                > > Gergely.
                > >
                >
                > I already got things to work by using references - I was just hoping
                > that I could avoid them to keep things nice and simple... thanks!
                >

                No problem. Sadly there is no other way. At least no other i know
                off... :))

                Gergely.
              • merlyn@stonehenge.com
                ... hooyar66 I already got things to work by using references - I was just hoping hooyar66 that I could avoid them to keep things nice and simple... thanks!
                Message 7 of 7 , Aug 30, 2007
                  >>>>> "hooyar66" == hooyar66 <pcbcad@...> writes:

                  hooyar66> I already got things to work by using references - I was just hoping
                  hooyar66> that I could avoid them to keep things nice and simple... thanks!

                  Perl doesn't have "lists of lists", so you must delve into references to
                  make lists of arrayrefs to get what you want.

                  A function is invoked in either scalar context (single item returned) or list
                  context (one flat list of items returned). That's all you get.

                  --
                  Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
                  <merlyn@...> <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!
                Your message has been successfully submitted and would be delivered to recipients shortly.